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

Michael Caveney
Michael Caveney
50,144 Points

Modifying Elements 2 of 2

"Set the text content of the a tag to be the value stored in the variable linkName"

I have no idea how to do this without reworking the first task, and rewatching the video and re-reading the documentation has provided any insight. Help?

<!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>
let linkName = document.querySelector("input").value;

4 Answers

Hi,

I'll try to help. Now that you have the value of the input element stored in the linkName variable, you have to set the text of the a tag to the value stored in the linkName variable:

// link is the id of the a tag
document.getElementById("link").textContent = linkName;

At the of the 2nd tag you should have the follwing 2 lines of code:

let linkName = document.querySelector("input").value;
document.getElementById("link").textContent = linkName;

I hope it helps. Have a nice day :)

One Net
One Net
3,729 Points

Another alternative which worked for me:

let linkName = document.querySelector('#linkName').value;
document.querySelector('a').innerHTML = linkName;
Nerijus Balakauskas
Nerijus Balakauskas
5,370 Points
let linkName = linkName.value;
document.querySelector('a').innerHTML = linkName;

This works too.

I've tried this but I get an error saying the anchor tag does not contain the correct value.

let inputValue = document.getElementById('linkName').value;

document.getElementById("link").textContent = linkName;