return elements that match specified CSS selector(s)

Can querySelectorAll() select elements by class name?

Answers:

  • Yes, querySelectorAll() can select elements by class name in JavaScript. You can use the class selector by prefixing the class name with a period (.).

    For example, if you want to select all elements with the class name example, you can use the following code:

    const elements = document.querySelectorAll('.example');
    

    This will return a NodeList of all elements in the document that have the class example. You can then iterate over this NodeList or access specific elements by index, if needed.

    Note that querySelectorAll() can also be used with other selectors, such as IDs (#), tag names, and combinations of these selectors.

Related Questions: