return elements that match specified CSS selector(s)

Can querySelectorAll() select elements by multiple selectors?

Answers:

  • Yes, querySelectorAll() can select elements using multiple selectors. You can pass a string that contains a comma-separated list of selectors to querySelectorAll(). The method will return a NodeList of all elements that match any of the selectors specified.

    Here's an example:

    // This will select all <p> and <div> elements in the document.
    const elements = document.querySelectorAll('p, div');
    
    // You can iterate through the NodeList
    elements.forEach(element => {
        console.log(element);
    });
    

    In this example, querySelectorAll('p, div') selects all <p> and <div> elements in the document, and the results are logged to the console. You can combine any valid CSS selectors in this way, including classes, IDs, attributes, and pseudo-classes.