return elements that match specified CSS selector(s)

Is querySelectorAll a part of DOM API?

Answers:

  • Yes, querySelectorAll is a method that is part of the DOM (Document Object Model) API. It is used to select multiple elements from the DOM based on a specified CSS selector.

    The querySelectorAll method returns a static NodeList of elements that match the given selector(s). This allows developers to use CSS-like selector syntax to find elements in the document, making it a powerful and flexible way to traverse the DOM.

    Here is a basic example of how querySelectorAll can be used:

    // Select all paragraph elements in the document
    const paragraphs = document.querySelectorAll('p');
    
    // Iterate through the NodeList and log each paragraph element
    paragraphs.forEach((paragraph) => {
        console.log(paragraph.innerText);
    });
    

    In this example, querySelectorAll('p') selects all <p> elements in the document, and then each of them is logged to the console.