0

I have two buttons which im working with, I am trying to make a hover effect and when the user clicks on the button have a clicked event.

I have been playing around with a few jquery methods such as mouseover mouseout etc but still no luck. The idea is to get the hover to only work on elements that have not been selected already.

The issue with the code below is that once an button has been selected if the user hovers over the selected method it gets rid of its current state.

Is their a way of not getting rid of the current state once a button has been selected?

Thanks

$(".M").click(function(){
    $(this).css('background-color','#515B69');
    $(this).css('color','#fff');
    $(".F").css('color','#515B69');
    $(".F").css('background-color','#fff');   
});

$(".M").mouseover(function() {
    $(this).css('background-color','#515B69');
    $(this).css('color','#fff');
});

$('.M').mouseleave(function(){
    $(this).css('color','#515B69');
    $(this).css('background-color','#fff');
});

$(".F").click(function(){
    $(this).css('background-color','#515B69');
    $(this).css('color','#fff');
    $(".M").css('color','#515B69');
    $(".M").css('background-color','#fff');
});

$(".M").mouseover(function() {
    $(this).css('background-color','#515B69');
    $(this).css('color','#fff');
});

$('.F').mouseleave(function(){
    $(this).css('color','#515B69');
    $(this).css('background-color','#fff');
});

3 Answers 3

4

I would recommend offloading a lot of these tasks with CSS

.M, .F {
  background-color: #fff;
  color: #515B69;
}

.M.active, .M:hover,
.F.active, .F:hover {
  color: #fff;
  background-color: #515B69;
}

And for your JS

$('.M, .F').on('click', function () {
  $('.M, .F').removeClass('active');
  $(this).addClass('active');
});
Sign up to request clarification or add additional context in comments.

5 Comments

+1, You were first... :). To be not off topic: also the toggleClass (api.jquery.com/toggleclass) function is helpful.
Thanks for the help, but im still getting the same problem when a user hovers over the clicked state it gets rid of it
Make also the .M.active:hover style... Maybe this will help :)
@Jacek You're right, $.toggleClass() works great. In this case you still want $.removeClass() on the first line in the callback because its applied to all buttons, active and inactive, so we want to make sure we're removing from all. Which means the second line is always adding a class, whether you're toggling or adding, and it's better to be explicit.
@josephspens Yeah. I saw toggle in Your answer before edit (as far as I remember), so I decided to make a note about that. Thx for information, and great support. Best regards.
1

Set a variable in your click functions and then check it with if statements in your mouseover and mouseleave functions. For example:

var clicked = 0;

$(".F").click(function(){
$(this).css('background-color','#515B69');
$(this).css('color','#fff');
$(".M").css('color','#515B69');
$(".M").css('background-color','#fff');
clicked = 1;
});

if (clicked > 0){
 $(".F").mouseover(function() {
 $(this).css('background-color','#515B69');
 $(this).css('color','#fff');
 });
}

Comments

1

I'd prefer to use removeClass and addClass. If You use removeClass without parameters, all classes will be removed from element. Then add Class, that You want. You can make it that way, for example:

$(selector).removeClass().addClass("your_class");

In that way, styles are seperated from scripts, which is always a good practice. Try to rewrite Your code in that way. If You have any questions, just ask in a comment, I will update my answer :).

PS. Of course You must place "your_class" in style.css file :).

Best regards

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.