Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Remi Vledder
14,144 PointsWhat is the benefit of Element.querySelector() over Element.getElementsByClassName()?
In the tutorial movie "Perform: Traversing Elements with querySelector", I'm wondering what the reason is to use querySelector over getEementsByClassName?
Remi Vledder
14,144 PointsKartikey Shahi Thanks for sharing the link.
Main reasons in that article are that the getElement* functions are old (which isn't really a reason), and that the querySelector and querySelectorAll are more powerful. An additional benefit is that you don't necessarily have to use a class or id to target an element.
However the getElement* functions have a performance advantage http://jsperf.com/getelementbyid-vs-queryselector
So if there aren't any other differences than that it seems that you could better stick with the good ol' functions then...
1 Answer
jack AM
2,618 PointsWith queryselector(), it can be a complete css3 selector, with ids and classes together (.class #id .class.class). Which is a massive difference than just getElementById or getElementsByClassName. So it depends on what you want to do, and how you want to accomplish it.
Kartikey Shahi
Courses Plus Student 2,002 PointsKartikey Shahi
Courses Plus Student 2,002 PointsHey Remi, Check out the link here hope it helps you https://www.kirupa.com/html5/finding_elements_dom_using_querySelector.htm