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

ashique desai
ashique desai
3,662 Points

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

Hi there, I have tried to solve this using various methods but still could not find the right solution. There is a similar question on the forum but that too is not answered satisfactorily. Can any body help me with this challenge?

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
```JavaScript
let linkName = document.getElementById('linkName').value.textContent('a');

2 Answers

Hi,

I'll try to help. I think the issue is you're trying to do everything in one single step, but there are two steps to accomplish in the challenge. The first one you store the value of the input:

let linkName = document.getElementById("linkName").value;

And the second one, 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;

To make changes in an element you must select it first, in your code you just select the input element with the id of linkName, but then you have to select the anchor element with the id of link to make changes in the a tag. In the challenge, you cannot do both things at once ;)

I hope you find it useful. Have a nice day

ashique desai
ashique desai
3,662 Points

Thanks a lot, Eduardo Parra.

Vernon Neilly
Vernon Neilly
2,652 Points

I am also stuck on this question:

Here is what is in my app.js file:

const myInput = document.querySelector('input'); let inputValue = myInput.value; const myAnchor = document.querySelector('a#link'); inputValue = myAnchor.textContent;

Here are the two differences that I see in my code.

  1. I am using 'querySelector('')' instead...Is this wrong? Why will this not work?
  2. The provided answer uses concatenation and I am using 2 steps (define element in variable) then (set variable to element textContent).

Where did I go wrong?

Thanks in advance,

VN2.

I was stuck as I didn't know textContent existed...:((