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 trialMel Rojo
Full Stack JavaScript Techdegree Student 4,699 PointsHow do I set the text content of the <p> element with the class info to the value stored in inputValue?
I keep getting the error: Bummer: There was an error with your code: TypeError: 'null' is not an object (evaluating 'document.getElementById('info').textContent = inputValue')
I have been researching how to complete this task but I am still stuck.
var inputValue = document.getElementById('linkText').value;
document.getElementById('info').textContent = inputValue;
<!DOCTYPE html>
<html>
<head>
<title>DOM Manipulation</title>
</head>
<link rel="stylesheet" href="style.css" />
<body>
<div id="content">
<label for="linkText">Link Text:</label>
<input type="text" id="linkText" value="sample text">
<p class="info"></p>
</div>
<script src="app.js"></script>
</body>
</html>
4 Answers
jb30
44,807 Pointsinfo
is a class, not an id. Instead of document.getElementById('info').textContent = inputValue;
, try document.getElementsByClassName('info')[0].textContent = inputValue;
.
Bodie Wood
Full Stack JavaScript Techdegree Student 4,027 PointsYou can use document.querySelector()
to target the info class.
For example:
const inputValue = document.querySelector('input').value;
document.querySelector('.info').textContent = inputValue;
This is a simple way to the solution.
Mel Rojo
Full Stack JavaScript Techdegree Student 4,699 PointsThanks jb30! It worked as you said it would!
Kavitha Mahesawarappa Sujatha
9,066 Pointsconst inputValue = document.querySelector('input').value; document.querySelector('p').textContent = inputValue;
jsimms
5,092 Pointsjsimms
5,092 PointsHow come we needed to put the index value for it to run properly?
jb30
44,807 Pointsjb30
44,807 PointsThe
document.getElementsByClassName
method returns an HTML collection, not an element. There could be multiple elements in the HTML collection, and they could all have different values for their textContent. The index value is used to specify which element from the collection to get the textContent from.