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 (Retiring) Making Changes to the DOM Getting and Setting Text with textContent and innerHTML

Why do we assign class="description" to input and button elements??

Hi guys,

could you explain me why do we assign the class description to the input and button elements? I can see that in app.js we only use the class for the p element and even in the CSS we do not need this class for these elements!

Thanks! Luca

In THIS specific example, it doesn't make much sense to use the .description class instead of the #description id since we've only selected one element out of all the <p> tags in the html document. My only guess is that we'll be using the remaining <p> tags later in the lessons.

3 Answers

Joshua Roberson
Joshua Roberson
4,472 Points

You have to broaden your view a bit to really understand the value of Class.

When you're looking at a larger full-blown web application, reusability becomes extremely important.

By assigning a class name to "this" button.. all of it's properties can be reapplied in the sense of using a theme if you would.

So it works two ways:

  • If I needed to update the styling of all of the buttons in question, if they have the same class name, I can do this simply by modifying a few lines within the CSS file.
  • When I go to add a new button (in this case) I can easily have it adapt styling already set in place by the CSS.

Classes are a quick way to keep our code clean, regardless the size of the application in use.

In CSS you can use an ID only once. By using CLASS you can use it over and over to define attributes of multiple elements.

I understand but as I can see the class description is used only for the p element, isn't it?

Marius Unknown
Marius Unknown
12,925 Points

If you have some buttons or inputs from past lessons in your page then you will have to make selector names with button.description or input.description. Otherwise -- you are right, in this particularly example classes for these elements are unnecessary

P.S. you will see this solution in future lessons.