Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some reasons for jquery .focus() not working?

Tags:

jquery

focus

Some thoughts are that the ELEMENT_ID.focus() is inside divs that are hidden at certain times.

This should be an easy problem to solve -- but I'm struggling :(

***code works fine -- the text field isn't being focused on upon page loading up.

STEP1 [SOLVED] JAVASCRIPT:

$("#goal-input").focus();  $('#goal-input').keypress(function(event){  var keycode = (event.keyCode ? event.keyCode : event.which);   if(keycode == '13') { etc, etc, etc } 

HTML

<input type="text" id="goal-input" name="goal" /> 

[STEP2] JAVASCRIPT:

 if (goal) {           step1.fadeOut('fast', function() {           step1.hide();           step2.fadeIn('fast');  etc, etc 

HTML:

  <div id="step-2">         <div class="notifications">         </div>         <input type="text" id="name"   name="name" placeholder="Name" />                <script type="text/javascript">               $(function(){               $("#name").focus();               });             </script> 

Why doesn't step 2 work? :(

like image 361
dngoo Avatar asked Mar 07 '12 05:03

dngoo


People also ask

How does focus work in jQuery?

The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it). The focus() method triggers the focus event, or attaches a function to run when a focus event occurs. Tip: This method is often used together with the blur() method.

Is Focus deprecated in jQuery?

If jQuery UI is not loaded, calling the . focus() method may not fail directly, as the method still exists. However, the expected behavior will not occur. This method is deprecated.

Which jQuery method can be a proper replacement for focus () in order to run the element's focus event handler without setting the focus on the element?

focus() on elements that are visible. To run an element's focus event handlers without setting focus to the element, use . triggerHandler( "focus" ) instead of . focus() .

How do you focus an element in jQuery?

The focus() is an inbuilt method in jQuery which is used to focus on an element. The element get focused by the mouse click or by the tab-navigating button. Here selector is the selected element. Parameter: It accepts an optional parameter “function” which specifies the function to run when the focus event occurs.


2 Answers

I had problems triggering focus on an element (a form input) that was transitioning into the page. I found it was fixable by invoking the focus event from inside a setTimeout with no delay on it. As I understand it (from, eg. this answer), this delays the function until the current execution queue finishes, so in this case it delays the focus event until the transition has completed.

setTimeout(function(){     $('#goal-input').focus(); }); 
like image 97
Nick F Avatar answered Sep 24 '22 06:09

Nick F


You need to either put the code below the HTML or load if using the document load event:

<input type="text" id="goal-input" name="goal" /> <script type="text/javascript"> $(function(){     $("#goal-input").focus(); }); </script> 

Update:

Switching divs doesn't trigger the document load event since everything already have been loaded. You need to focus it when you switch div:

if (goal) {       step1.fadeOut('fast', function() {           step1.hide();           step2.fadeIn('fast', function() {                 $("#name").focus();           });       }); } 
like image 24
jgauffin Avatar answered Sep 24 '22 06:09

jgauffin