return elements that match specified CSS selector(s)

Can querySelectorAll target elements that contain multiple class names?

Answers:

  • Yes, querySelectorAll can target elements that contain multiple class names. You can specify multiple classes in the selector string by separating them with a dot (.). For example, if you want to select elements that have both class names class1 and class2, you would write:

    const elements = document.querySelectorAll('.class1.class2');
    

    This selector will match any element that has both class1 and class2 as part of its class list.

    If you want to select elements that have any of the specified classes, you can use a comma (,) to separate the class selectors:

    const elements = document.querySelectorAll('.class1, .class2');
    

    This will select elements that have either class1 or class2.

    Remember that querySelectorAll returns a NodeList, which you can then loop through or manipulate as needed.