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) Getting a Handle on the DOM Select a Page Element By Its ID

I cant seem to get this code to work I followed along and It keeps saying null or undefined when I use the value method

html <!DOCTYPE html> <html> <head> <title>JavaScript and the DOM</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <h1 id="myHeading">JavaScript and the DOM</h1> <p id="myParagraph">Making a web page interactive</p> <input type="text" id="myInputText"> <button id="myButton">change headline color</button> <script src="app.js"></script> </body> </html>

js const myHeading=document.getElementById("myHeading"); const myButton=document.getElementById("myButton"); const myTextInput=document.getElementById("myTextInput"); myButton.addEventListener('click', () => { myHeading.style.color=myTextInput.value
} );

1 Answer

matt mccherry
matt mccherry
4,493 Points

Simple frustrating problem! Check your id tags in the html.

You will see that your input id is <input type="text" id="myInputText">. The JS is looking for an input element of myTextInput.

const myTextInput=document.getElementById("myTextInput"); 
//Solution (either change html or JS) 
//JS changes shown here

const myHeading=document.getElementById("myHeading"); 
const myButton=document.getElementById("myButton"); 
const myInputText=document.getElementById("myInputText");  // changed to "myInputText"

myButton.addEventListener('click', () => { 
                          myHeading.style.color= myInputText.value; // changed to "myInputText"
} );