0

Hi I'm a new jquery user, and I need some help.

I'm trying to change a css :hover attribute of a last child div element upon a click of that div. More specifically, I have a nav-bar div with some child div buttons in it. The child divs are all a par of the same class so that when I hover over them...a border-width attribute changes. I would like it so that when I click last child div, the border color is changed, but still only shows when the mouse is hovering...and when I click it again, I would like the original border attributes defined in the css to retake effect.

Here is a sample of what I'm trying to do:

$("div.navbar:last").click(function(){    
    if ($("div#details").is(":hidden")){    
        $("div#details").fadeIn(100);    
        $("div.navbar:hover").css("border-bottom-color","#ee4444");
    } else {
        $("div#details").fadeOut(100);
        $("div.navbar").css("border-bottom-color","#88ddff");
    }   
});

Any help is appreciated.

2
  • 2
    jQuery does'nt support pseudo selectors. Commented Apr 2, 2013 at 18:03
  • @adeneo More accurately, JavaScript cannot alter pseudo selectors. Commented Apr 2, 2013 at 18:06

3 Answers 3

4

You can't use the :hover selector in jQuery. You should avoid using the .css() method, and instead use addClass, removeClass, and toggleClass to change DOM state.

Actual CSS can then take over and use the classes as styling hooks to change how the page is rendered.

Sign up to request clarification or add additional context in comments.

Comments

0

As Adeneo stated, you can't use psuedo selectors (like :hover) in jQuery. If you want to attach a hover event in jQuery you could however use something like the following:

$("#myElement").on('hover', function() {
    alert("I've been hovered over!");
});

Comments

0

You can put the pseudoclass on a secondary class which you add on click

JS

  $("div.navbar:last").click(function(){    
    if ($("div#details").is(":hidden")){    
        $("div#details").fadeIn(100);    
        $("div.navbar").addClass("hoverBottom");
    } else {
        $("div#details").fadeOut(100);
        $("div.navbar").css("border-bottom-color","#88ddff");
    }  
    });

CSS

.hoverBottom:after {
  border-bottom-color","#ee4444";
}

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.