Is there an easy way to get all elements by data-foo (a specific data- attribute name) like getElementsByTagName or getElementsByClassName thanks.
Note: I am not using a JS library.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’ve got one approach, in compliant browsers, so long as you know in advance the name of the attribute you’re looking for, say
data-myatt:JS Fiddle proof-of-concept.
References:
document.querySelectorAll().