1

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.

Community
  • 1
  • 1
Luken00
  • 21
  • 5
  • no point trying to use `getElementById` for elements that don't have an ID. – charlietfl Dec 23 '12 at 13:27
  • i should have explained myself better: i tried changing class to id, trying the getElementById and it worked (for the first one). it wasn't however what i need it because i have more than one div to change and ids must be unique. the example was to show that i tried something that partially worked and tried to adapt it to my needs. thanks anyway for your time – Luken00 Dec 23 '12 at 13:41

2 Answers2

4

I'd suggest:

$('#buttons li').hover(function(){
    $('.item-' + this.className).addClass('hover');
},
function(){
    $('.item-' + this.className).removeClass('hover');
});

JS Fiddle demo.

References:

David Thomas
  • 240,457
  • 50
  • 366
  • 401
1

Finagle your markup a little bit and you can do it with plain CSS:

<ul id="buttons">
   <li class="a">button a</li>
   <li class="b">button b</li>
   <li class="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>
   </li>
</ul>​​​​​

.a:hover ~ li .item-a {
    background: red;
}

.b:hover ~ li .item-b {
    background: blue;
}

Demo

bookcasey
  • 37,335
  • 13
  • 73
  • 92
  • nice solution. i got it working now with javascript, so i don't have to edit at all the html, but i'll save it for future reference. thanks :) – Luken00 Dec 23 '12 at 14:51