Foad Tahmasebi
Foad Tahmasebi

Reputation: 1342

JQuery addClass affect once

I want to animate my div tag when the button is pressed, by adding animate.css class by jquery, but this only works once and stops working each subsequent time the button is being pressed.

This is html code:

    <div id="note" class="">a</div>
    <button id="submit">submit</button>

and this is jquery

$(document).ready(function() {
$('#submit').click(function() {
    $('#note').removeClass();
    $('#note').addClass('animated fadeInLeft');
    $('#note').html('some text');
});});

you can see on http://jsfiddle.net/Lgg9D/5/
how to animate div each time press button?
thanks

Upvotes: 2

Views: 592

Answers (3)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

it just take a little time before classes are removed. you can add a little delay after removing classes:

$(document).ready(function() {
    $('#submit').click(function() {
        $('#note').removeClass('animated fadeInLeft');
        setTimeout(function(){
            $('#note').addClass('animated fadeInLeft');},50);

        $('#note').html('some text');
    });
}); 

http://jsfiddle.net/Lgg9D/13/

Upvotes: 2

Why not just toggle the class, It handles the addition and removal alone

$(document).ready(function() {
    $('#submit').click(function() {
        $('#note').toggleClass('animated fadeInLeft');
        if($("#note").html() == 'a')
        {
        $('#note').html('some text');
        }
        else
        {
        $('#note').html('a');
        }
    });
});

Demo Fiddle

Upvotes: 0

King King
King King

Reputation: 63317

You can try this:

$('#submit').click(function() {        
    $('#note').removeClass();
    setTimeout(function(){
      $('#note').addClass('animated fadeInLeft');
      $('#note').html('some text');
    },1);        
});

Demo.

Upvotes: 2

Related Questions