7

I have a limited understanding of Javascriptan I am trying to add a class to a div when I hover another div Right now I can add a class to the same div but I would like to be able to add let's say the class blue to a div called first when I hover #second and #third.

current code :

    
    $(document).ready(function() {     
    $("#second, #third").hover(function(){     
    $(this).addClass("hover");    
    $(this).removeClass("hoverable");      
    },     
    function(){    
    $(this).removeClass("hover");     
        $(this).addClass("hoverable");     
    }  
    );     
    });        

My live website can be seen at : www.designinterieurm2.com/dev

1
  • This will possibly include DOM traversal of some kind, what does the relevant html look like? Commented Feb 17, 2011 at 20:28

3 Answers 3

23
$(document).ready(function() {     
    $('#second, #third').hover(function(){     
        $('#first').addClass('blue');    
    },     
    function(){    
        $('#first').removeClass('blue');     
    });
});   
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it worked! The mistake I was making was using double quotes instead of single quotes inside my function
No, sorry, that was not the problem. Double or single quotes both work, as long as the overall syntax is correct you can use either. I actually prefer single quotes, and will eliminate double-quotes when I notice them.
3

Just replace $(this) with $("#div_id");

where #div_id is the id of the div you're trying to change.

Comments

0
$(".toptab").hover(function(){

    $(this).addClass("current");
    $(this).find("ul").show();

},function(){

    $(this).find("ul").hide();
    $(this).removeClass("current");

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.