views:

87

answers:

2

Hi All,

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

Much appreciated

+9  A: 

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.

Yi Jiang
`<label>Name <input type="text" name="name" /></label>` would do the thing too :)
Anpher
Hi Anpher, that is how the markup structure is currently although this doesn't work...I have a label wrapped around input text field.I want the cursor to jump to the input text field when I click the label
Nasir
FYI: IE6 doesn't support the version without `for`. And the `for` attribute refers to the `id` of the `input` not its `name`.
RoToRa
A: 

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();
});
fearofawhackplanet