tags:

views:

73

answers:

1

I have the following jQuery line:

$('#newStep2 input#name').focus().css('background','Red');

Where I am trying to set the focus of a textbox (#name). I am only setting the background to debug to make sure it works.

Well, the background turns red, but the input is not receiving the focus. Um?

EDIT

HTML, per request:

 <fieldset id="newStep2">
    <h3>2. Name It</h3>            
    <label class="scrollTo">Name:</label>
    <input type="text" name="campaign.name" id="name" />
    <p class="step"><a href="javascript:" class="but_next_step">
         Update and Continue</a></p>
 </fieldset>
+1  A: 

from the jquery docs for focus():

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.

To call the focus method, either use the each() method to invoke the focus method on each matched element, or use get(index) to get the nth matched element and call the focus method. So either:

$('#newStep2 input#name').each(function() { this.focus();});

or

$('#newStep2 input#name').get(0).focus();
spot
assuming this is in a dom ready block, wouldn't what he has work?
Colin
Colin: Good point. Yeah, it should. I have an app that uses such a method in the ready event handler -- $('form:first :input:first').focus(). Frankly, I don't think it should matter. Just wanted to go with what the docs say.
spot