Horje
querySelector() vs. getElementById()

In JavaScript, both document.querySelector() and document.getElementById() are methods used to select elements from the DOM. They serve similar purposes but have differences in their usage and capabilities.

What is document.querySelector()?

The document.querySelector() is a method that allows to the select a single element in the DOM using a CSS selector. It returns the first element that matches the specified CSS selector within the document.

Syntax:

document.querySelector(selector)

Example: In this example, document.querySelector(‘.text’) selects the first element with the class “text” and logs its text content to the console.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>querySelector Example</title>
</head>
<body>
    <div id="container">
        <p class="text">Hello, World!</p>
    </div>
    <script>
        const element = document.querySelector('.text');
        console.log(element.textContent);
    </script>
</body>
</html>

Output: The output will be in the console of your browser.

Hello, World!

What is document.getElementById()?

The document.getElementById() is a method that allows to select a single element in the DOM using its unique ID attribute. It returns the element with the specified ID.

Syntax:

document.getElementById(id)

Example: In this example, document.getElementById(‘message’) selects the element with ID “message” and logs its text content to the console.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>getElementById Example</title>
</head>
<body>
    <div id="container">
        <p id="message">Hello, World!</p>
    </div>
    <script>
        const element = document.getElementById('message');
        console.log(element.textContent);
    </script>
</body>
</html>

Output: The output will be in the console of your browser.

Hello, World!

Difference Between JavaScript document.querySelector() and document.getElementById()

Characteristics

document.querySelector()

document.getElementById()

Selection Method

Uses CSS selectors

Uses element ID

Returns

First matched element

Single element by ID

Selector Type

Any CSS selector

Only ID selector

Performance

May be slower

Faster

Compatibility

Compatible with the modern browsers

Compatible with the all browsers

Conclusion

Both document.querySelector() and document.getElementById() are essential methods for the selecting elements from the DOM in JavaScript. While querySelector() offers more flexibility with the CSS selectors getElementById() is optimized for the selecting elements by their IDs providing better performance. Choose the appropriate method based on the specific requirements and compatibility needs.




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript RegExp u Modifier JavaScript RegExp u Modifier
JavaScript RegExp s Modifier JavaScript RegExp s Modifier
What Do Multiple Arrow Functions Mean in JavaScript? What Do Multiple Arrow Functions Mean in JavaScript?
How to Convert a JSON String into an SQL Query? How to Convert a JSON String into an SQL Query?
How to Escape a String in JavaScript? How to Escape a String in JavaScript?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15