javascript - JQuery thickbox with iframe. Set focus to input field - Stack Overflow

Hi I had a jquery thickbox modal popup on my application. (iframe) Everything works great but I want t

Hi I had a jquery thickbox modal popup on my application. (iframe)
Everything works great but I want to set the focus to a specific input field.

The Iframe loads a normal aspx page so I thought I'd do a $(document).ready(..focus);
I put that script in the IFrame code

However this does not work. I believe that after the "ready" some other code is executed by the thickbox so that it loses focus again. (for instance.. I CAN set the value of the input field so the mechanism does work..

Can anybody help me set the focus ? Below is my calling code..

<a href="page.aspx?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=550&width=700" title="Add" class="thickbox">Add</a>

Hi I had a jquery thickbox modal popup on my application. (iframe)
Everything works great but I want to set the focus to a specific input field.

The Iframe loads a normal aspx page so I thought I'd do a $(document).ready(..focus);
I put that script in the IFrame code

However this does not work. I believe that after the "ready" some other code is executed by the thickbox so that it loses focus again. (for instance.. I CAN set the value of the input field so the mechanism does work..

Can anybody help me set the focus ? Below is my calling code..

<a href="page.aspx?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=550&width=700" title="Add" class="thickbox">Add</a>
Share Improve this question edited Jun 24, 2009 at 6:42 Julian de Wit asked Jun 23, 2009 at 7:05 Julian de WitJulian de Wit 3,0942 gold badges30 silver badges30 bronze badges 2
  • Is the $(document).ready(..focus) in the aspx page that the iframe loads? – Tim Banks Commented Jun 23, 2009 at 16:38
  • Yes, i did put it in the iframe – Julian de Wit Commented Jun 24, 2009 at 6:42
Add a ment  | 

2 Answers 2

Reset to default 4

ThickBox displays the iframe's container by calling a method in the onload event of the iframe element. Since the iframe is hidden on the parent page until after the iframe's content is loaded you cannot set the focus simply using $(document).ready(..focus);. The easiest way I've found to get around this is to use setTimeout to delay the function call that sets the focus until after the iframe is displayed:

jQuery(document).ready(function() {        
    setTimeout(function(){
        $('#YourElementID').focus();   
    },200);
});

Note: You might have to adjust the milliseconds value you pass to setTimeout.

From docs.jquery.:

Triggers the focus event of each matched element.

This causes all of the functions that have been bound to the focus event to be executed. Note that this does not execute the focus method of the underlying elements.

That means, your code should more likely be: $('#input_field').get(0).focus ()

The difference is, that you use in my example the DOM element's own focus element, whereas in yours you use jQuery's.

That still doesn't work, but is perhaps a step in the right direction.

Cheers,

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745143677a4613547.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信