1

I want to update the following code which should remove/add class on click of .card panel not on mouseleave which it does currently.

http://jsfiddle.net/GDdtS/9884/

$('.flip').click(function(){
    $(this).find('.card').addClass('flipped').mouseleave(function(){
        $(this).removeClass('flipped');
    });
    return false;
});
2
  • You could try having an if statement to check whether the element has the class flipped and if it does, remove it, else add it to the element. Commented Jul 22, 2015 at 11:45
  • your event working properly please check your css of flipped Commented Jul 22, 2015 at 11:46

4 Answers 4

4

You could check if the class is present and then decide conditionally what to do (add or remove a class as desired):

$('.flip').click(function(){
    var card = $(this).find('.card');
    if (!card.hasClass('flipped')) {
        card.addClass('flipped');
    } else {
        card.removeClass('flipped');
    }             
    return false;
});

If you want to use different buttons to flip the card you could use something like this:

$('.flip').click(function(){
   var card = $(this).find('.card');
   if (!card.hasClass('flipped')) {
      card.addClass('flipped');
   }
   return false;
});
$('.exit').click(function(){
  var card = $(this).closest('.exit').parents('.card');
  card.removeClass('flipped');
  return false;
});

This will flip back when the closing button on the back is clicked. It will get the close button and tries to find a parent with class .card to remove the .flipped class

 <div class="flip"> 
    <div class="card"> 
        <div class="face front"> 
            Front
        </div> 
        <div class="face back"> 
            <div class="exit">x</div>
            Back
        </div> 
    </div> 
</div> 

I created a fork of your fiddle: http://jsfiddle.net/8n3gokua/1/

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

7 Comments

Thanks. I am using your code because I want to remove the class by clicking a div not to use as toggle
@MarkusHayner But this code has exactly the same result as using toggleClass. Adding the class if not already there, and removing it if it is already there is by definition toggling.
Yes but I am looking to remove the class from a button like when you have a div $('.exit').removeClass('flipped'); You know what I mean?
any idea how to do this? i don't want to click over all card class to remove the class
Where do you want to click instead?
|
1

You can try following code

 $('.flip').click(function(){
    $(this).find('.card').toggleClass('flipped');
    return false;
});

Comments

0

You can use .toggleClass just like bellow:

$('.flip').click(function(){
    $(this).find('.card').toggleClass('flipped')
});

You can learn more from here.

Comments

0

Something like this?

$('.flip').bind("click mouseleave", function(){
    $(this).find('.card').toggleClass('flipped');
});

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.