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

JavaScript Interpolation

Indu khan
Indu khan
169 Points

What is the use of document.querySelector() ??

What is the use of document.querySelector() ??

2 Answers

Austin Whipple
Austin Whipple
29,725 Points

Indu,

From MDN documentation:

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.

Indu khan
Indu khan
169 Points

Can you give me an example?

Austin Whipple
Austin Whipple
29,725 Points

There are a couple examples in that MDN article. Are you trying to achieve something in particular using the document.querySelector() method?

example: to change color of heading document.querySelector('#heading').style.color = 'red';