In jQuery it would simply be
$("a.class").randommethod();
or
$(".class1 .class2").randommethod();
How do I achieve the same effect using pure Javascript? (I’m editing a open-source software for personal use and it doesn’t use jQuery)
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 can’t do it using
getElementsByClassName. You would need to use something more likequerySelectorAll, which accepts CSS style selectors.You should note that neither method has full cross-browser support. That’s why people use javascript libraries. If you don’t need to support older browsers, then
querySelectorAllis a good choice.Another option is to use the
Sizzleselector engine that jQuery uses.