Categories
JavaScript Answers

How to get elements by attribute when querySelectorAll with JavaScript?

Spread the love

Sometimes, we want to get elements by attribute when querySelectorAll with JavaScript.

In this article, we’ll look at how to get elements by attribute when querySelectorAll with JavaScript.

How to get elements by attribute when querySelectorAll with JavaScript?

To get elements by attribute when querySelectorAll with JavaScript, we can use the attribute selector.

For instance, we write

const els = document.querySelector('[role="button"]')

to call querySelector with '[role="button"]' to select all the elements that has the role attribute set to button.

Conclusion

To get elements by attribute when querySelectorAll with JavaScript, we can use the attribute selector.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *