Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shorthand in jquery

Tags:

jquery

I have this jquery codes:

 $(document).ready(function() {
$("#masthead ul li").mouseover(function(){
    $("#masthead ul li").removeClass("cr");
    $(this).toggleClass("cr");
});
   }); 

  $(document).ready(function() {
$("#intUl li").mouseover(function(){
    $("#intUl li").removeClass("cr");
    $(this).toggleClass("cr");
});
}); 

Can we write shorthand for two similar jquery code? Thanks in advance


1 Answers

$('#masthead ul li, #intUL li').mouseover(function() {
    $(this).siblings().removeClass("cr");
    $(this).toggleClass("cr");  
});

Does the same as the original code; not sure why you're removing a class only to readd it, however. If you're trying to get it to flash or something, I'm fairly sure it won't work as you're expecting.

Misunderstood original intent; a commenter clarified for me, and I fixed it :)

like image 168
Erik Avatar answered Jan 20 '26 20:01

Erik