Languages
[Edit]
EN

JavaScript - get element by id

2 points
Created by:
JoanneSenior
1070

In this article, we would like to show you how to get an element handle by id in JavaScript.

1. document.getElementById method example

Edit

First example

Edit

Result:

document.querySelectorAll usage example
document.querySelectorAll usage example

Second example

Edit

2. document.querySelector method example

Edit

Result:

document.querySelectorAll usage example
document.querySelectorAll usage example

Note: document.querySelector method uses css selectors - it means each id name should be prefixed with hash.

3. document.querySelectorAll method example

Edit

Writing web page it is possible to define few elements with same id attribute - this aproach is strongly not recommended because of id was designed to be unique.

Result:

document.querySelectorAll usage example
document.querySelectorAll usage example

Alternative titles

  1. JavaScript - get element by id name
  2. JavaScript - get element by id value
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