JQuery onclick function to focus input

I want the jQuery function to mean that the user clicks on some text on the page where he focuses the input.

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

It did nothing. Nothing is registered in Firebug.

Any ideas?

Wonderful

+6
source share
3 answers

If you have an id for the input element, you do not need javascript for this. You can use the for attribute with a label tag pointing to the identifier of the input element. try it

Demo work

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

If you click on "Name Text", the focus will be set in the input field.

+16
source

http://jsfiddle.net/HenryGarle/LXngq/

 <span> <br> Span <br> <input id="InputToFocus" type="text"> </span> $('span').click(function(){ $('#InputToFocus').focus(); }); 

It seems to be working fine. This is probably the problem with your selectors. Could you place the structure of your HTML surrounding the input?

+15
source

I think $ ('# header.search input') returns a bunch of INPUT, not just one, so jQuery may not know which one should be focused. You will need to decide which one to focus on.

0
source

Source: https://habr.com/ru/post/896501/


All Articles