JavaScript DOM - Part 7 - Query Selectors - Power to Grab anything [video + article]
Tags: javascript, webdev, html, codenewbie
Published: 2020-05-23 17:33:33

> 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:
{% link https://dev.to/tharunshiv/javascript-dom-part-6-get-elements-by-tagname-video-article-331 %}

{% youtube gPKDxjiZZZs %}

> **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 `.length`and 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 `.length`and 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_

```html
<p id="ts">The paragraph to be grabbed</p>
```

_JavaScript_

```javascript
let para = document.querySelector('#ts'); // notice the '#'
console.log(para.innerText);
```

_Output_
```
The paragraph to be grabbed
```

_Example: Grabbing an element by class name_

_HTML_

```html
<p class="sp">The paragraph to be grabbed</p>
<p class="sp">This wont be grabbed</p>
```

_JavaScript_

```javascript
let para = document.querySelector('.sp'); // notice the '.'
console.log(para.innerText);
```

_Output_
```
The paragraph to be grabbed
```

> 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_

```html
<p>The paragraph to be grabbed</p>
<p>This wont be grabbed</p>
```

_JavaScript_

```javascript
let para = document.querySelector('p'); // mention the element
console.log(para.innerText);
```

_Output_
```
The paragraph to be grabbed
```

> 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,

{% user developertharun %}