JavaScript Selectors

Introduction to Selectors

JavaScript selectors are a fundamental part of interacting with the Document Object Model (DOM). They allow you to target and manipulate HTML elements in your web page. Selectors can be used to find elements by their tag names, classes, IDs, attributes, and more.


Basic Selectors

You can select elements using various methods, including:

Try yourself
        
            
// Selecting elements by tag name
const headings = document.getElementsByTagName('h1'); // Returns an HTMLCollection

// Selecting elements by class name
const elementsWithClass = document.getElementsByClassName('my-class'); // Returns an HTMLCollection

// Selecting an element by ID
const uniqueElement = document.getElementById('unique-id'); // Returns an Element

// Using querySelector to select elements by ID, class, and more
const elementById = document.querySelector('#my-id'); // Selects element with ID 'my-id'
const elementsByClass = document.querySelectorAll('.my-class'); // Selects all elements with class 'my-class'
const firstParagraph = document.querySelector('p'); // Selects the first paragraph element

        
    

Combining Selectors

You can combine selectors to target elements more precisely. For instance, you can use CSS-style selectors, such as:

Try yourself
        
            
// Combining selectors using querySelector and querySelectorAll
const element = document.querySelector('tag.class'); // Returns the first matching element
const elements = document.querySelectorAll('.class1.class2'); // Returns a NodeList

        
    

Attribute Selectors

You can select elements based on their attributes. For example, to select all elements with a specific data attribute, you can use:

Try yourself
        
            
// Selecting elements based on attributes
const elementsWithAttribute = document.querySelectorAll('[data-attribute="value"]'); // Returns a NodeList

        
    

Pseudo-class Selectors

Pseudo-classes allow you to select elements based on their state or position. Some common pseudo-classes include :hover, :active, and :nth-child(n).

Try yourself
        
            
// Using pseudo-class selectors
const hoveredElements = document.querySelectorAll(':hover'); // Returns a NodeList
const oddElements = document.querySelectorAll(':nth-child(odd)'); // Returns a NodeList

        
    

Traversal and Hierarchical Selectors

You can navigate the DOM tree using traversal methods. For example, to select a child element, you can use:

Try yourself
        
            
// Traversal and hierarchical selectors
const parent = document.querySelector('.parent');
const child = parent.querySelector('.child');

        
    

Event-Based Selectors

You can also select elements based on events. For instance, to select all buttons, you can use:

Try yourself
        
            
// Selecting elements based on events
const buttons = document.querySelectorAll('button'); // Returns a NodeList

        
    

Real-world Examples


Summary

JavaScript selectors are powerful tools for interacting with the DOM. They enable you to target specific elements, combine selectors for precision, select based on attributes or pseudo-classes, navigate the DOM tree, and even select elements based on events. Understanding selectors is crucial for dynamic web applications.