codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

58 / 65 topics
57JavaScript DOM Introduction58JavaScript Selecting DOM Elements59JavaScript Changing HTML & CSS60JavaScript Events61JavaScript addEventListener62JavaScript and JSON63JavaScript Fetch API64JavaScript Local Storage65JavaScript Regular Expressions
Tutorials/JavaScript/JavaScript Selecting DOM Elements
🌐JavaScript

JavaScript Selecting DOM Elements

Updated 2026-05-12
30 min read

JavaScript Selecting DOM Elements

In this tutorial, you'll learn how to select DOM elements using three common methods in JavaScript: getElementById, querySelector, and querySelectorAll. Understanding these methods is crucial for manipulating HTML content and styling dynamically. Whether you're building a simple web page or a complex web application, selecting the right elements at the right time is essential.

Introduction

DOM (Document Object Model) manipulation is a fundamental part of JavaScript development. The DOM represents the structure of an HTML document as a tree of nodes, where each node is an object representing a part of the document. Selecting these nodes allows you to interact with and modify the content, attributes, and styles of your web page.

In this tutorial, we'll explore three primary methods for selecting DOM elements:

  1. getElementById: Retrieves a single element by its unique ID.
  2. querySelector: Selects the first element that matches a specified CSS selector.
  3. querySelectorAll: Selects all elements that match a specified CSS selector and returns them as a NodeList.

Each method has its own use cases, and understanding when to use which method will help you write more efficient and effective JavaScript code.

Core Content

1. getElementById

The getElementById method is the simplest way to select an element in the DOM. It requires the unique ID of the element you want to select. This method returns a single element or null if no element with that ID exists.

Example: Basic Usage

HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>getElementById Example</title>
7</head>
8<body>
9 <div id="myElement">Hello, World!</div>
10 <script src="script.js"></script>
11</body>
12</html>
JavaScript
1// script.js
2const element = document.getElementById('myElement');
3console.log(element.textContent); // Output: Hello, World!
Output

Example: Selecting by Tag and Attribute

HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>querySelector Example</title>
7</head>
8<body>
9 <input type="text" name="username" value="JohnDoe">
10 <script src="script.js"></script>
11</body>
12</html>
JavaScript
1// script.js
2const inputElement = document.querySelector('input[name="username"]');
3console.log(inputElement.value); // Output: JohnDoe
Output

Example: Iterating Over NodeList

JavaScript
1// script.js
2const elements = document.querySelectorAll('.myClass');
3for (let i = 0; i < elements.length; i++) {
4 console.log(elements[i].textContent);
5}
Output
Hello, World!
Another Element

Tips

  • Use querySelectorAll when you need to select multiple elements and perform operations on each one.
  • Remember that the returned NodeList is live, meaning it updates automatically if the DOM changes.

Practical Example

Let's create a simple web application that allows users to add items to a list. We'll use getElementById, querySelector, and querySelectorAll to manipulate the DOM elements.

HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>DOM Manipulation Example</title>
7 <style>
8 ul {
9 list-style-type: none;
10 padding: 0;
11 }
12 li {
13 margin: 5px 0;
14 }
15 </style>
16</head>
17<body>
18 <h1>To-Do List</h1>
19 <input type="text" id="newItemInput">
20 <button id="addButton">Add Item</button>
21 <ul id="itemList"></ul>
22
23 <script src="script.js"></script>
24</body>
25</html>
JavaScript
1// script.js
2document.getElementById('addButton').addEventListener('click', function() {
3 const newItemInput = document.getElementById('newItemInput');
4 const newItemText = newItemInput.value.trim();
5
6 if (newItemText !== '') {
7 const itemList = document.getElementById('itemList');
8 const newListItem = document.createElement('li');
9 newListItem.textContent = newItemText;
10 itemList.appendChild(newListItem);
11
12 // Clear the input field
13 newItemInput.value = '';
14 }
15});

In this example, we use getElementById to select the button and input elements. When the button is clicked, we retrieve the value from the input field using getElementById, create a new list item element using document.createElement, set its text content, and append it to the unordered list using appendChild.

Summary

MethodDescription
getElementByIdSelects a single element by its unique ID.
querySelectorSelects the first element that matches a specified CSS selector.
querySelectorAllSelects all elements that match a specified CSS selector and returns them as a NodeList.
  • Use getElementById for quick selection of a single, specific element.
  • Use querySelector when you need to select a single element based on complex criteria.
  • Use querySelectorAll when you need to select multiple elements and perform operations on each one.

What's Next?

Now that you know how to select DOM elements, the next step is learning how to change their HTML content and CSS styles. In the next tutorial, we'll explore methods like innerHTML, textContent, style, and more. Stay tuned!


PreviousJavaScript DOM IntroductionNext JavaScript Changing HTML & CSS

Recommended Gear

JavaScript DOM IntroductionJavaScript Changing HTML & CSS