Selecting Elements in JavaScript

Javascript 8 min min read Updated: Mar 09, 2026 Intermediate
Selecting Elements in JavaScript
Intermediate Topic 2 of 15

Selecting elements is one of the most important tasks when working with the JavaScript DOM. Before JavaScript can modify or interact with any HTML element, it must first locate or select that element from the webpage.

JavaScript provides several built-in methods that allow developers to access elements based on their ID, class name, tag name, or CSS selectors. Once an element is selected, it can be modified, styled, or used to respond to user actions.

Why Selecting Elements is Important

When building interactive web pages, JavaScript often needs to update content, change styles, or respond to events. Selecting elements allows JavaScript to target specific parts of the webpage.

Key Point: JavaScript must first select an element before it can modify or interact with it.

getElementById()

The getElementById() method selects a single element based on its unique ID attribute. Since IDs should be unique in an HTML document, this method always returns one element.

javascript let heading = document.getElementById("title"); console.log(heading);
Output

The HTML element with ID "title" is selected

getElementsByClassName()

The getElementsByClassName() method selects all elements that share the same class name. It returns a collection of elements.

javascript let items = document.getElementsByClassName("menu-item"); console.log(items);
Output

A list of elements with class "menu-item"

Key Point: This method returns an HTMLCollection containing multiple elements.

getElementsByTagName()

The getElementsByTagName() method selects elements based on their HTML tag name.

javascript let paragraphs = document.getElementsByTagName("p"); console.log(paragraphs);
Output

All paragraph elements on the page

querySelector()

The querySelector() method selects the first element that matches a CSS selector. It is one of the most flexible ways to access elements.

javascript let element = document.querySelector(".menu-item"); console.log(element);
Output

The first element with class "menu-item"

querySelectorAll()

The querySelectorAll() method selects all elements that match a CSS selector. It returns a NodeList containing all matched elements.

javascript let elements = document.querySelectorAll(".menu-item"); console.log(elements);
Output

A list of all elements with class "menu-item"

Key Point: querySelectorAll() supports powerful CSS selectors, making it very flexible.

Summary of Element Selection Methods

  • getElementById() – selects an element by ID
  • getElementsByClassName() – selects elements by class
  • getElementsByTagName() – selects elements by tag name
  • querySelector() – selects the first element using a CSS selector
  • querySelectorAll() – selects all elements using a CSS selector

Conclusion

Selecting elements is the foundation of DOM manipulation in JavaScript. By using different selection methods, developers can access and control specific elements within a webpage.

Once an element is selected, JavaScript can modify its content, change styles, or respond to user interactions.

In the next tutorial, you will learn about DOM Manipulation in JavaScript, where we will explore how to change content, attributes, and styles dynamically.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators