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
Łukasz Czuliński
8,646 PointsFunctions working when nodes found by getElementById, but not getElementsByClassName
I'm stumped as to why this behavior is happening. For example, everything works great when I do:
document.getElementById('quote-form').onmouseover = function(){
console.log('the function');
};
But when I change it to :
document.getElementsByClassName('form').onmouseover = function(){
console.log('the function');
};
I get nothing. My form is as follows:
<form method="POST" action="http://localhost:8000/quotes" accept-charset="UTF-8" class="form" id="quote-form">
Any ideas what's happening here?
1 Answer
Hugo Paz
15,622 PointsHi lukasz,
The .getElementsByClassName() method returns an array-like object of all child elements which have all of the given class names.
So you need to specify which object you want to trigger the .onmouseover.
Assuming the form is the first element with that class, you just need to use this code:
document.getElementsByClassName('form')[0].onmouseover = function(){
console.log('the function');
};
Łukasz Czuliński
8,646 PointsŁukasz Czuliński
8,646 PointsBrilliant, thanks! Makes perfect sense.