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

Darwin Smith
Darwin Smith
11,080 Points

Confused with instruction: Set the text content of the a tag to be the value stored in the variable inputValue.

What I get from this is that I need to reassign inputValue which does not make sense or work.

app.js
const val = document.getElementById('linkName');
let inputValue = val.value;
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>

2 Answers

Shay Paustovsky
Shay Paustovsky
969 Points

Hi Darwin,

On each HTML Object/Element (used interchangeably) you have a (.textContent) & (.innerHTML) among many others properties and methods. The difference between them is slight.

// para.textContent = 'Hi I\'m a paragraph :)' - sets the text of the selected element.
// listUl.innerHTML = `<li> List Item 1 </li>` - sets the inner content/tags of the selected element.

Since you have assigned the (input) HTML object to the 'val' variable, it has the (.value) property automatically. Now you can read text that the user types into the input field and assign it as the text content of a different element.

const headline = document.querySelector('h1')
const inputF = document.querySelector('input');

headline.textContent = inputF.value
// Assigns the value of the inputF variable as the text of the headline.

Hopefully This has solved and cleared your confusion :)

:smiley: Shay :smiley:

Steven Parker
Steven Parker
231,008 Points

In task 1, you got a value from a page element and stored it in "inputValue". For task 2, you'll be doing the opposite.

You won't change "inputValue" again, but you'll use it to set the value (actually the "textContent") of a page element.