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 trialIndu khan
169 PointsWhat is the use of document.querySelector() ??
What is the use of document.querySelector() ??
2 Answers
Austin Whipple
29,725 PointsIndu,
Returns the first Element within the document that matches the specified selector, or group of selectors.
Essentially, this give you some options to explore and modify the DOM. With this you can select elements matching the selector you specify to save and/or manipulate as you see fit.
As an aside, if you're using jQuery, you'll probably never use this particular bit of code and just use $('.mySelector') and such to traverse the DOM.
gautam sharma
564 Pointsexample: to change color of heading document.querySelector('#heading').style.color = 'red';
Indu khan
169 PointsIndu khan
169 PointsCan you give me an example?
Austin Whipple
29,725 PointsAustin Whipple
29,725 PointsThere are a couple examples in that MDN article. Are you trying to achieve something in particular using the
document.querySelector()
method?