Snorreri
Snorreri

Reputation: 119

setting focus on shown input field

I am trying to set focus to the input text field with id="message-add" when it is showed using jQuery. I think i should use 'live()' or 'delegate' and the function focus() to get it to work? Which should be used and how should it be written?

$("#message-add").show();
$("#message-add").focus();

Upvotes: 1

Views: 6973

Answers (2)

Jason Gennaro
Jason Gennaro

Reputation: 34855

Here is one way to do it, if the input is hidden, which is what I think the Q is stating?

<button>Show</button>
<input id="message-add" />

$('#message-add').hide();

$('button').click(function(){
    $('#message-add').show().focus();    
});

http://jsfiddle.net/jasongennaro/FhB8u/

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

$("#message-add").focus; // Add Parens .focus()

This option should work just fine, but you need to add the parens after the method call.

You may need to do this if you want it on page load:

$(document).ready(function(){
   $("#message-add").focus();
});

Here is a working example: http://jsfiddle.net/TnHyN/

Upvotes: 0

Related Questions