I'm trying to change (better would be to add) a class name to multiple divs using an onmouseover function in another element . the two elements are not nested to eachother so i can't (or at least don't) think i can use plain CSS to do so. i'm simplifying the following code to make it easier to read. the actual html for the "items" is a little bit more nested. hope this won't be an issue
<ul id="buttons">
<li class="a">button a</li>
<li class="b">button b</li>
</ul>
<div id="items">
<div class="item-a"></div>
<div class="item-b"></div>
<div class="item-b"></div>
<div class="item-a"></div>
<div class="item-b"></div>
</div>
so basically what I want to do is when I hover the "li" elements with the mouse the corresponding class in div "items" gets a class added to itself (so class "a" in buttons would add a class 'hover' to all the "item-a" classes, and so on).
I'm fairly new to javascript/jquery. so far I managed to get it done using Id instead of class, but since ID must be unique it's not what I need. the code was :
onmouseover="document.getElementById('item-a').className = 'hover';"
this worked. only one div (the first with that ID) but it worked, so I tried to change it from Id to ClassName but it didn't work
onmouseover="document.getElementsByClassName('item-a').className = 'hover';"
and also, if the above code would work, it would change the class, while i'd prefer to add it (and then remove it with a onmouseout function)
for reference, among the lot of searching i did i also tried this link Trigger the css:hover event with js trying to adapt it to my situation
$(window).load(function() {
$('.a').hover(function(){
$('.item-a').addClass('hover');
});
}
with no results.
thanks in advance for any help.