JQuery onclick function to focus input

I want a JQuery function that means if the user clicks on some text on the page it focuses an input.

$(document).ready(function(){
$('#header.search span').click(function(){
    $('#header.search input').focus();
});
});

That did not do anything. On Firebug nothing is registered.

Any ideas?

———————–

If you have id to the input element then you dont need javascript to do this. You can use forattribute with a label tag pointing to the input element id. Try this

Working demo

<label for="input1">Name</label>
<input id="input1" type="text" />

If you click on Name text the focus will be set into the input field.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: