0

I have been trying to set up the following script without success.

I have the element #num_11 which fades in a couple of other elements on hover and fades them out on mouseout. That's working perfectly (which I am already quite proud of)

What I want is that if you CLICK on #num_11 that the other elements STAY visible. On click again, they should disappear again and the whole script go back to the original "mode"

$("#num_11").hover(
    function(){
        $("#identification li").each(function() {
            $("h1", this).fadeIn();
            $("span", this).addClass("opague");
        });
    },
    function(){
        $("#identification li").each(function() {
            $("h1", this).hide();
            $("span", this).removeClass("opague");
        });
    }
);

$("#num_11").click(function(){
    $(this).toggleClass('clicked');
});

What I have tried in the code above was putting $("#num_11").not(".clicked").hover( and $("#num_11:not(".clicked")").hover( as well as a if-else-query which ended up in chaos…

I would really appreciate your help with my challenge.

All the best, K

0

2 Answers 2

1

try following code;

$("#num_11").hover(
function(){
  if(!$(this).hasclass('clicked')){
    $("#identification li").each(function() {
        $("h1", this).fadeIn();
        $("span", this).addClass("opague");
    });
  }
},
function(){
  if(!$(this).hasclass('clicked')){
    $("#identification li").each(function() {
        $("h1", this).hide();
        $("span", this).removeClass("opague");
    });
  }
});
$("#num_11").click(function(){
$(this).toggleClass('clicked');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Chamika, this worked for me! I don't know why this solution didn't come to my mind during my helpless tries ;)
0

Try filtering:

$("#num_11").filter(function() {
    return !$(this).hasClass('clicked');
}).hover(
    ...
);

2 Comments

Hi rkw, thanks for your quick answer. Unfortunately this solution didn't bring the result – the behaviour was all the same as without filtering. But I like your approach with only one extra line of code
I just realized something, is #num_11 a single element, or is it suppose to represent a group of elements?

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.