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 Modifying Elements

Why my code does not work?

When I write like this:

let inputValue = document.querySelector('#linkName').value; inputValue = document.querySelector('a').textContent;

It did not work, but when I reverse the order before and after the '=':

document.querySelector('link').textContent = inputValue;

it worked, can anyone tell me why? For me, the 2 methods are the same.

app.js
let inputValue = document.querySelector('#linkName').value;
inputValue = document.querySelector('#link').textContent;
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>DOM Manipulation</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <div id="content">
            <label>Link Name:</label>
            <input type="text" id="linkName">
            <a id="link" href="https://teamtreehouse.com"></a>
        </div>
        <script src="app.js"></script>
    </body>
</html>

1 Answer

Steven Parker
Steven Parker
231,269 Points

The assignment operator ("=") always works right-to-left. The value of the term on the right side is stored in the location referenced on the left side. So when you swap the sides, it's not the same. Only one way is correct.

thanks Steven, but in the code below shows the opposite: value at right is assigned to the left (listDiv's display). Can you help me to understand why in the code below it is legit, but not in the code above?

const toggleList = document.querySelector('#toggleList');
const listDiv = document.querySelector('.list');

toggleList.addEventListener('click', () =>{
  listDiv.style.display = 'none';
})
Steven Parker
Steven Parker
231,269 Points

We are saying the same thing: the value at right is assigned to the left.

What's wrong with the code above is that it is putting the "textContent" into the "inputValue", but the instructions say to put the "inputValue" into the "textContent" instead;

Thank you! very clearly explained!