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

Richard Nash
Richard Nash
24,862 Points

This seems like it should totally work...what am i doing wrong here?

Please let me know Guil Hernandez what I am missing on this one, as it seems correct.

Also, MDN suggest never to use 'innerHTML' due to XSS attacks...what are those?

Thanks!

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>
app.js
let linkName = document.querySelector('input').value;

a.textContent = linkName;

3 Answers

Juan Garcia
Juan Garcia
10,577 Points

I was struggling with that problem too. I was able to solve it like this: let link = document.querySelector('a'); link.textContent = linkName;

Rokas Mazeika
Rokas Mazeika
4,243 Points

i think that the problem is that u put let instead of var

Richard Nash
Richard Nash
24,862 Points

let is what was already given by the question. I'll give it a try, though. But theoretically speaking, i'm not trying to change the contents of the variable, i'm trying to apply the content of the variable to an html element, so that should not be the issue.

Richard Nash
Richard Nash
24,862 Points

Yeah, that worked for me as well. I guess you really have to set everything as a variable before doing anything to it. i was trying to skip the step of storing the a in a constant. Thank you very much Juan, and happy coding :-)

Juan Garcia
Juan Garcia
10,577 Points

No problem. Glad I could help :)