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 JavaScript and the DOM Getting a Handle on the DOM Use CSS Queries to Select Page Elements

Do we use DOM to do our CSS or CSS itself?

Im confused.. This lesson seems to teach that DOM can style CSS but we would use CSS for that..

What is the actual " useful " for working with DOM?

2 Answers

Bella Bradbury
.a{fill-rule:evenodd;}techdegree seal-36
Bella Bradbury
Front End Web Development Techdegree Graduate 32,790 Points

Hi Peter!

You can definitely change the CSS through JS, but it's more common to be changing the HTML. The reason that it might appear as though you're going to be writing CSS is just because, like CSS, JavaScript needs a way to know what elements you want it to alter! Both the CSS and JS files need to be linked to the HTML to communicate across files, but that's just the first step. You then have to clue the code into the element you're trying to manipulate via identifiers and language specific syntax. CSS selectors (.class {} and #id{}) are functionally the same as the JS code from this lesson (var example = document.querySelector('.class'); and var example = document.querySelector('#id');). They only begin to differ when you go from identifying the different elements to using the CSS to style a page and the JS variables to set behaviors!

One of the coolest parts of JavaScript is that you can use it to manipulate the DOM without having to manually change the HTML or CSS documents themselves. This can make your site super flexible and do some pretty cool things! JS is so widely used because it really allows you to do anything you can think of, especially when it comes to making your site more engaging for users.

A few examples of the countless ways to use this skill:

  • A quiz page that changes the text from question to question
  • Pagination of products, articles, etc.
  • Form validation

At the very least, there may be times as a developer where you only have access to the JS file and if you learn now how to manipulate the DOM you'll be one step ahead. Hope this helps!

Thank you!