Languages
[Edit]
EN

JavaScript - focus HTML input field on label click

3 points
Created by:
user4838
698

In this article, we would like to show you how to focus input field on label click using JavaScript.

Quick solution:

or:

 

Practical examples

Edit

By using JavaScript ,we are able to get input element focus by calling its focus() method.

It is good to consider 2 cases:

1. label before input

Edit

In this section, we use nextElementSibling property that lets to get next element to the label.

2. label after input

Edit

In this section, we use previousElementSibling property that lets to get previous element to the label.

 

See also

Edit
  1. HTML - focus input field on label click

References

Edit
  1. Element.nextElementSibling - Web APIs | MDN
  2. Element.previousElementSibling - Web APIs | MDN
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join