Languages
[Edit]
EN

JavaScript - get element handle by id

10 points
Created by:
Rubi-Reyna
677

In this article, we're going to have a look at how to get element by id in JavaScript

We can achieve it by using one of the methods available by default in JavaScript:

  • document.getElementById('element-id')
  • document.querySelector('#element-id')
  • document.querySelectorAll('#element-id')

Below we can see runnable examples of each of those methods. Click on it to test how it works.

1. using document.getElementById

Edit

2. using document.querySelector

Edit

3. using document.querySelectorAll

Edit

Note:

The document.querySelectorAll works with duplicated ids but in practice, you shouldn't use duplicated ids

Alternative titles

  1. JavaScript - getting element handle with getElementById, querySelector and querySelectorAll
  2. JavaScript - get HTML element handle by id
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