What is the best way to get an element by its class name using JavaScript?
Can I just use something like document.getElementByClassName(‘myClass’) ?
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.
Yup, there is such a thing as
getElementsByClassName()and here’s a browser support list which pretty much says “All recent browsers except IE8 and below”also, there is
querySelectorAll()which uses CSS selectors. It’s pretty much similar to how jQuery does it. Here’s a browser support list of it, which also says “All recent browsers except IE7 and below”