Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
Bella Bradbury
Web Development Techdegree Student 14,361 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!