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.
source share