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 trialMahfuzur Rahman
3,204 Pointsfor loop doesn't work
<!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>Making a web page interactive</p>
<input type='text' id='myTextInput'>
<button id='myButton' > Changing heading color</button>
<p>Things that are purple:</p>
<ul id="myList"> <li>grapes</li> <li>amethyst</li> <li>lavender</li> <li>plums</li> </ul>
<script src='app.js'></script>
</body> </html>
//JS FILE CONTENT
const myHeading=document.getElementsByTagName('h1')[0]; const myButton=document.getElementById('myButton'); const myTextInput=document.getElementById('myTextInput'); const myList=document.getElementById('myList'); const myLi=myList.document.getElementsByTagName='li'); myButton.addEventListener('click', ()=>{ for(var i=0; i<myLi.length; i++){ myLi[i].style.color=myTextInput.value; } });
2 Answers
Andrey Misikhin
16,529 Pointsconst myHeading=document.getElementsByTagName('h1')[0];
const myButton=document.getElementById('myButton');
const myTextInput=document.getElementById('myTextInput');
const myList=document.getElementById('myList');
const myLi=myList.getElementsByTagName('li');
myButton.addEventListener('click', ()=>{
for(var i=0; i<myLi.length; i++) {
myLi[i].style.color=myTextInput.value;
}
});
Andrey Misikhin
16,529 PointsYes, you are right.
Mahfuzur Rahman
3,204 PointsMahfuzur Rahman
3,204 Pointsnow it works. using line by line comparison I think I've finally got the mistake I've made. So when we have already selected once using the document.getElement.... we don't have to use that again for accessing the nested elements. Right?