Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How to move the cursor position into a input text box by clicking a label tag?

like image 945
Nasir Avatar asked Dec 22 '22 23:12

Nasir


2 Answers

Use the for attribute. No need for any Javascript.

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

The browser will do the magic all by itself.

like image 91
Yi Jiang Avatar answered Dec 28 '22 08:12

Yi Jiang


Don't know why Yi Jiang's answer wouldn't be working, but it's trivial in jQuery if you prefer to do it that way

$('#myLabel').click(function() {
    $('#myTextBox').focus();
});
like image 36
fearofawhackplanet Avatar answered Dec 28 '22 09:12

fearofawhackplanet