May 31, 2012

jQuery setInterval

Question by gkunno

I have a jQuery function that flips an element and then turn it back after a few seconds, however there is multiple elements with the same “flip” tag and the setInterval elements flip all the elements back after the first click. How could i implement the flip effect to turn only the elements that have been clicked in e kind of flow, so that not all elements flip after the delay. The code is:

$(document).ready(function() {

    $('.click').toggle(function() //on click functions => toggle elements
    {           

        setTimeout( function() { // trigger the flip-back after delay (1500ms)
        $('.flip').trigger('click');
        }, 1500) 

        $this = $(this);
        $this.addClass('flip');
        $this.children('.front').delay(600).hide(0);
        $this.children('.back').delay(600).show(0);

    },          
        function() 
        {

        $this = $(this);
        $this.removeClass('flip');
        $this.children('.front').delay(600).show(0);
        $this.children('.back').delay(600).hide(0);

        });  
});

My intention is that the elements would flip back after the delay, but now all the clicked elements flip back after the delay, even though they are only visible for the time – delay. Hops this makes sense ๐Ÿ™‚

Thnx for any help!

Answer by Starx

Use, $(this) or this to refer the current item that is click. Also, the extra function is not needed, just use toggleClass() to apply/remove the class.

$('.click').click(function() //on click functions => toggle elements
{           

    var $this = $(this);
    setTimeout( function() { // trigger the flip-back after delay (1500ms)
        $this.trigger('click');
    }, 1500);

    $this.toggleClass('flip');
    $this.children('.front').delay(600).hide(0);
    $this.children('.back').delay(600).show(0);

});
December 30, 2011

Remove/Add hover after toggle Jquery

Question by hyperrjas

I have this css:

.disable {
          properties
          }

.comment_action:hover {
                       properties
                       }

I have this jQuery:

//comment button index disable
$("#mydiv").toggle(function() {
 $(this).find(".comment_action").addClass('disable');   
},
function(){
 $(this).find(".comment_action").removeClass('disable');
});

The problem for me is that when I doing click the .comment_action:hover is not disappear or removed. I want that If I doing click on the class .comment_action:hover dissapear and if I doing click again the .comment_action:hover appear.

Answer by lawrence.alan

You would need an !important added to properties you want to override in the :hover psuedo-selector…

Because :hover takes precedence, even if .disabled is applied.

Also your javascript should be calling find with .comment_action instead of comment_action

See working example: http://jsfiddle.net/TN4rh/11/

Answer by Starx

Better solution would be toggle the class. What you are trying to do can be done in one line.

$("#mydiv").click(function() {
    $(this).find(".comment_action").toggleClass('disable');       
});

Demo

๐Ÿ™‚

...

Please fill the form - I will response as fast as I can!