querySelectorAll() the best JavaScript function in the world!

How can I chain multiple selectors together in querySelectorAll()?

1 answer

  1. To chain multiple selectors together in `querySelectorAll()`, you can use commas between the selectors. It allows you to select elements that match any of the provided selectors.

    For example, if you want to select all paragraphs `<p>` and all headings `<h1>`, you can do this:

    document.querySelectorAll("p, h1");

    This will return a NodeList containing all the matching elements (all paragraphs and all h1 headings) in the document.

    You can also chain more than two selectors by separating them with commas:

    document.querySelectorAll("p, h1, .my-class, #my-id");

    This will select all paragraphs, h1 headings, elements with the class "my-class", and the element with the id "my-id".

Related Questions