views:

29

answers:

3

I have a button that will add show a form on the page. how can I move the focus to the first field of the form when that button is clicked?

simple example:

HTML:

<form style="display:none;" id="newForm">
   <input type="text" id="firstField">
</form>
<input type="button" id="showForm" value="add new">

jQuery:

 $("#showForm").click(function(){
     $("#newForm").show();
     //move focus??
});
+4  A: 

It might be this:

$("#newForm input:first").focus();
Fosco
A: 

Try this:

$('input#firstfield').focus();
Rakward
+1  A: 

A quicker lookup would simply be.

$('#firstField').focus()

However if you removed the ID from your element then this would be better but slightly slower.

$('#newForm input:first').focus();
Paul Dragoonis