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 trialDionte Barnes
10,140 PointsNext, set the text content of the <a> element with the ID link to the value stored in inputValue.
Confused,can someone please help?
var inputValue = document.getElementById('linkName').value;
document.getElementById('link').textContent = 'linkName';
<!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>
6 Answers
Jonathan Hermansen
25,935 PointsThe code challenge ask you to insert the value stored in inputValue, not the text 'linkName'. So in your variable inputValue, you save the text from the input element with the ID set to linkName, you then need to use the variable inputValue instead of the text 'linkName':
// this variable stores the value that you can use as text in the <a>-element with the ID of link.
var inputValue = document.getElementById('linkName').value;
// Now you use the value of the variabel inputValue as text in your <a>-element.
document.getElementById('link').textContent = inputValue;
Siddharth Pandey
7,280 PointsI'm confused why does this: "document.getElementById('link').textContent = inputValue; " work, and why doesn't this: "inputValue = document.getElementById('link').textContent" work?
Jonathan Hermansen
25,935 PointsBecause the challenge ask you to set the text from the input field (id="linkName") into the a-element (id="link"). When you try to set the text from the a-element (id="link") into the input field (id="linkName"), its inserting an empty string, this is bacause the a-element do not have any value between the opening tag (<a id=...>) and the closing tag (</a>). The challenge do not ask you to set the text content of the input element, so even if the a-element had a value inside it, the challenge would not be completed.
Daniel Medley
5,948 PointsBecause the challenge ask you to set the text from the input field (id="linkName") into the a-element (id="link"). When you try to set the text from the a-element (id="link") into the input field (id="linkName"), its inserting an empty string, this is bacause the a-element do not have any value between the opening tag (<a id=...>) and the closing tag (</a>). The challenge do not ask you to set the text content of the input element, so even if the a-element had a value inside it, the challenge would not be completed.
Ng Yan Xiang
4,764 PointsWhy doesn't this work:
inputValue = document.querySelector("#link").textContent;
while this would:
document.querySelector("#link").textContent = inputValue;
Jonathan Hermansen
25,935 PointsBecause the challenge ask you to set the text from the input field (id="linkName") into the a-element (id="link"). When you try to set the text from the a-element (id="link") into the input field (id="linkName"), its inserting an empty string, this is bacause the a-element do not have any value between the opening tag (<a id=...>) and the closing tag (</a>). The challenge do not ask you to set the text content of the input element, so even if the a-element had a value inside it, the challenge would not be completed.
Hassan Al Manawy
24,859 PointsinputValue = document.querySelector("#link").textContent;
This one won't work cause the challenge asks to store the inputValue to the <a> element with the ID "Link" and not the other way around. In other words, this will change the inputValue to whatever stored as a content within the <a> element.
while "document.querySelector("#link").textContent = inputValue;" would find the selector first and then assign the inputValue to it.
Hope that helps.
Regards,
H
Daniel Medley
5,948 PointsBecause the challenge ask you to set the text from the input field (id="linkName") into the a-element (id="link"). When you try to set the text from the a-element (id="link") into the input field (id="linkName"), its inserting an empty string, this is bacause the a-element do not have any value between the opening tag (<a id=...>) and the closing tag (</a>). The challenge do not ask you to set the text content of the input element, so even if the a-element had a value inside it, the challenge would not be completed.
Ashish Dahal
4,796 Pointsdocument.querySelector("a").textContent=inputValue;
Robert O'Toole
6,366 Pointswould like to know why that doesn't work as well.... that's what i tried^^^
jhon white
20,006 Pointsvar inputValue = document.querySelector('#linkName').value;
document.querySelector('#link').textContent = inputValue;
Dionte Barnes
10,140 PointsDionte Barnes
10,140 PointsThank you for your help!
Junior Aidee
Front End Web Development Techdegree Graduate 14,657 PointsJunior Aidee
Front End Web Development Techdegree Graduate 14,657 PointsI wrote the below solution for the Challenge 2 of 2 and it didn't work. Why is that?
inputValue = document.getElementById('link').textContent;
Mauricio Hernandez
7,208 PointsMauricio Hernandez
7,208 PointsThank you, God bless you and your families.
Mauricio Hernandez
7,208 PointsMauricio Hernandez
7,208 PointsGod bless everyones families.