Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction:
2.
getElementsByTagName:
3.
getElementsByClassName:
4.
getElementById:
5.
querySelector():
6.
querySelectorAll():
7.
Frequently Asked Questions:
8.
Key Takeaways:
Last Updated: Mar 27, 2024

JS DOM Selectors

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction:

The main theme of this article is to style the elements in javascript. We have different methods to select elements in javascript. You can also apply style on HTML elements to change the visual presentation of HTML documents dynamically using JavaScript. In the following section, we'll discuss the various methods of selecting DOM’s in JavaScript.

 

 

DOM Selectors, as the name suggests are used to select HTML elements within a document using JavaScript. There are 5 ways in which you can select elements in a DOM using selectors.

  • getElementsByTagName()
  • getElementsByClassName()
  • getElementById()
  • querySelector()
  • Queryselectorall()

Also Read, Javascript hasOwnProperty

getElementsByTagName:

This returns all the elements that matches the specified TagName.

 

 

  • document.getElementsByTagName('h1') returns a collection of items matching the tag name h1. And since we got only one h1 element, the list contains only one elements.
  • Using [0] as index, we can access the first element in the list which is <h1 class="heading">DOM Selectors</h1>.
  • document.getElementsByTagName('li') returns a list of 5 elements as we have five li tags in our page.
  • And any individual element can be selected by using it’s index such as document.getElementsByTagName('li')[0].

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

getElementsByClassName:

This method returns all the specified elements that match the specified class name.

 

 

In the previous section, the selection is based on the tag name, but here the selection is based on the class name.

 

getElementById:

In this, the elements are selected based on the Ids. This may look similar to the tagname and Class name but this sector only returns the first matched element while ignoring the remaining.

 

 

querySelector():

This one returns the first element that matches the specified CSS selector in the document.

 

 

querySelectorAll():

This method returns all the elements that matches the specified CSS selector in the document.

 

 

  • document.querySelectorAll('.heading') returns a list of all elements that matches the specified CSS selector. Since we have only one element under the class name .heading, the list contains one element. And it can be accessed by it’s index.
  • Similarly, document.querySelectorAll('#item') returns a list of 3 items that matches the CSS selector. Individual elements are accessed by it’s index.


Must Read Fibonacci Series in JavaScript

Frequently Asked Questions:

  1. What are the DOM selectors?
    DOM Selectors, as the name suggests is used to select HTML elements within a document using JavaScript. There are 5 ways in which you can select elements in a DOM using selectors.
     
  2. What are the javascript selectors?
    Javascript uses the CSS syntax to select and manipulate HTML elements. Selectors are used to "find" (select) HTML elements based on their tag name, id, classes, types, attributes, values of attributes and much more. A list of all selectors can be found in our CSS Selector Reference.
     
  3. What is querySelector()?
    The querySelector() method returns the first element that matches a specified CSS selector(s) in the document.
     
  4. How many types of DOM selectors are there in javascript?
    We have five types of DOM selectors in javascript. 
    (i)getElementsByTagName()
    (ii)getElementsByClassName()
    (iii)getElementById()
    (iv)querySelector()
    (v)querySelectorAll()

 

Key Takeaways:

In this blog, we have learned about the different types of DOM selectors in javascript. This followed by analyzing their features with different code snippets. The main theme of the blog is to understand the implementation of DOM selectors in javascript.

Recommended Reading:

To know more about exploring the tutorials of javascript at CodingNinjas.

Previous article
JAVASCRIPT DOM STYLING
Next article
Understanding the innerHTML Property in JavaScript DOM
Live masterclass