How to move the cursor to an input text box by clicking a label label?

How to move the cursor position in the input text box by clicking the label label?

+4
source share
2 answers

Use the for attribute. No need for javascript.

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

The browser will do the magic on its own.

+10
source

I don’t know why Yi Jiang answer will not work, but it is trivial in jQuery if you prefer to do it this way

 $('#myLabel').click(function() { $('#myTextBox').focus(); }); 
+1
source

All Articles