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 trialS Ananda
9,474 PointsCan't figure out how to get the value of the text input element.
Here's the goal - Get the value of the text input element, and store it in a variable linkName
Here's the error - Bummer! You selected the element, but have not gotten the value.
I think I want to get the a href value. I tried the attached code, but it doesn't work. What am I missing?
I also tried a lot or other permutations, but none of them worked either, so now I'm stumped.
<!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');
linkName.textContent;
2 Answers
Steven Parker
231,271 PointsYour error message has a very good hint!
The message "Bummer! You selected the element, but have not gotten the value." is spot-on. When you use querySelector you get the element itself, but what you really want is the value property of that element.
let linkName = document.querySelector('input').value;
Samuel Ferree
31,722 PointsJavaScript makes this extremely simple. when you're used to things like "innerText" and "innerHTML" you might miss it.
let linkName = document.querySelector('input').value; // <-- Voila!
// linkName now contains whatever the user typed into the input field
You'll use value a lot, so it's worth committing to memory, but if you ever forget property or method names in JavaScript, don't hesitate checking with documentation like MDN. Half of being a good developer is knowing where to find the things you don't know.
S Ananda
9,474 PointsS Ananda
9,474 PointsI had forgotten that you could add a dot value after the querySelector. Thanks for the reminder.