DEV Community

Cover image for JavaScript DOM - Part 7 - Query Selectors - Power to Grab anything [video + article]
Tharun Shiv
Tharun Shiv

Posted on • Edited on

JavaScript DOM - Part 7 - Query Selectors - Power to Grab anything [video + article]

This is going to be a multi-part Video + article tutorial series on JavaScript DOM. You're reading Part 7

You can read Part 6 here:

Considering Subscribing to my YouTube Channel if you like the video content: https://youtube.com/c/developerTharun

Recap

  • An ID used as a unique identifier for elements. No two elements on a page should have the same ID.
  • A Class is used as an identifier for any number of elements. So, a number of elements can have the same class.
  • document.getElementById('id') is used to get the element by Id.
  • document.getElementsByClassName('class') is used to get the elements by className and returns us a HTMLCollection which is like an array. We can get the length of this using .lengthand index it to get the individial elements.
  • document.getElementsByTagName('tagname') is used to get the elements by tag name and returns us a HTMLCollection which is like an array. We can get the length of this using .lengthand index it to get the individual elements.

Query Selector

Instead of using different methods to grab elements by referring to their Id, Class Name, Tag Name, You can use the Query Selector to grab any one of them. We will look at more of this in the examples below.

Example: Grabbing an element by ID

HTML

<p id="ts">The paragraph to be grabbed</p>
Enter fullscreen mode Exit fullscreen mode

JavaScript

let para = document.querySelector('#ts'); // notice the '#'
console.log(para.innerText);
Enter fullscreen mode Exit fullscreen mode

Output

The paragraph to be grabbed
Enter fullscreen mode Exit fullscreen mode

Example: Grabbing an element by class name

HTML

<p class="sp">The paragraph to be grabbed</p>
<p class="sp">This wont be grabbed</p>
Enter fullscreen mode Exit fullscreen mode

JavaScript

let para = document.querySelector('.sp'); // notice the '.'
console.log(para.innerText);
Enter fullscreen mode Exit fullscreen mode

Output

The paragraph to be grabbed
Enter fullscreen mode Exit fullscreen mode

Wait, the output is not incorrect, the QuerySelector returns us the first element that it matches. Ouch! There is a solution to this, and it is QuerySelectorAll which we will read in the next article.

Example: Grabbing an element by tag name

HTML

<p>The paragraph to be grabbed</p>
<p>This wont be grabbed</p>
Enter fullscreen mode Exit fullscreen mode

JavaScript

let para = document.querySelector('p'); // mention the element
console.log(para.innerText);
Enter fullscreen mode Exit fullscreen mode

Output

The paragraph to be grabbed
Enter fullscreen mode Exit fullscreen mode

Here again, the output is not incorrect, the QuerySelector returns us the first element that it matches. Ouch! There is a solution to this, and it is QuerySelectorAll which we will read in the next article.

So this is Query Selector where you grab elements by ID using the # and the ID of the element, you grab the elements by Class Name using the . and the Class of the element, you grab the elements by Tag Name by using the tag name of the elements.

In the next article, we will look at the most popularly used QuerySelectorAll.

Considering Subscribing to my YouTube Channel if you like the video content: https://youtube.com/c/developerTharun 😊

Written by,

Top comments (8)

Collapse
 
venkat121998 profile image
venkat anirudh

Great article illustrating the usage of QuerySelector 👍

Collapse
 
developertharun profile image
Tharun Shiv

Thank you

Collapse
 
chandrika56 profile image
Jaya chandrika reddy

Interesting, eager to see what QuerySelectorAll can do.

Collapse
 
developertharun profile image
Tharun Shiv

Thank you 😊 That's the next article

Collapse
 
uma_bcc profile image
umamaheswari.v

Good article, examples helped clarify my misconception.

Collapse
 
developertharun profile image
Tharun Shiv

Thank you

Collapse
 
praveenreddy1798 profile image
praveenreddy1798

Nice examples

Collapse
 
developertharun profile image
Tharun Shiv

Thank you