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 'p.description'?

const p = document.querySelector('p.description');

Could someone help me understand what the 'p.description' means?

'description' is a class, so the dot notation makes sense. I'm confused why it needs to be preceded by a 'p', though.

(code is from the video)

index.html

html
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <p>Making a web page interactive</p>

    <p class=description>Things that are purple:</p>
    <input type="text" class="description">
    <button class="description">Change List Description</button>
    <ul>
      <li>grapes</li>
      <li>amethyst</li>
      <li>lavender</li>
      <li>plums</li>
    </ul>
    <script src="app.js"></script> 
  </body>
</html>

app.js

const input = document.querySelector('input');
const p = document.querySelector('p.description');
const button button = document.querySelector('button');

button.addEventListener ('click', ()=> {
   p.textContent = input.value + ':';
})

2 Answers

Because there are also an input and a button elements with class description directly below the p element with class description, and you only want to select the p element with class description for that particular code.

Ah got it. So if I wanted to select all elements with the class description, then I'd use this instead? (obv it wouldn't work with the code from the class, but I just want to understand)

document.querySelectorAll('.description')

Yes.

Thanks Joseph Yhu !