Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Soufiane Bdaoui
9,601 PointsCan't Figure why this JS code is wrong
HTML code
!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
</head>
<body>
<section>
<div><a href="FirstLink.html" class="aLink"> First Link</a</div>
<div><a href="SecondLink.html" class="aLink">Second Link</a> </div>
</section>
</body>
</html>
JS code
var linkPages = document.getElementByClassName("aLink");
function changeText(){
linkPages.style.color="yellow";
linkPages.style.backgroundColor="yellow";
}
linkPages.onClick = changeText();
1 Answer

Steven Parker
220,925 PointsThree things stand out even without knowing the specific objectives of this code:
I notice you have "getElementByClassName", but the actual function is "getElementsByClassName" (plural).
Also, the actual function returns an element collection, not a single element, so it would not have "style" property. But the individual elements in it would, perhaps you need a loop?
And finally, it would be most unusual to set both "color" and "backgroundColor" to the same value unless you were intending to make the text invisible.
Soufiane Bdaoui
9,601 PointsYeah, I was expecting it to work like getElementByID(). Thanks for the help, now I get it.
Steven Parker
220,925 PointsSteven Parker
220,925 PointsCan you provide a link to the course page you are working with?