How to Use JavaScript QuerySelector Attribute Contains Examples?

Estimated read time 1 min read

The element.querySelector method in JavaScript can be used with the attribute selector [attribute*=value] to select an element based on whether its attribute contains a certain value. Here’s an example:

// Select an element with a class name that contains "example"
const el = document.querySelector("[class*='example']");

This will return the first element that has a class attribute value that contains the string “example”. If no such element is found, it returns null.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply