NodeDad
NodeDad

Reputation: 1537

if and else statement check if has class

I cant seem to figure out whats going on here... Im kind of half asleep, really trying to get this last thing done before i go to bed lol.

My goal: Click someone, then check if it has the class, if not, then add it, then remove it once the 1 second animation is complete. Right now im getting unexpteced identifier on the 3rd line down. (removeClass)

Note: the slamedown class is a keyframe

$('#accountloginsignup h1').click(function() {
  if ($('#takeonebar').hasClass('slamdown')){
    $('#takeonebar')removeClass('slamedown');
  } else {
    $('#takeonebar').addClass('slamdown');
  }
});

Upvotes: 8

Views: 86828

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388316

.toggleClass() is for this specific purpose

From the doc

Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.

$('#accountloginsignup h1').click(function() {
    $('#takeonebar').toggleClass('slamdown');
});

There is a typo

$('#accountloginsignup h1').click(function() {
    if ($('#takeonebar').hasClass('slamdown')){
        $('#takeonebar').removeClass('slamdown');  /* missing . before removeClass */
    } else {
        $('#takeonebar').addClass('slamdown');
      }
});

Upvotes: 19

amjayesh07
amjayesh07

Reputation: 1

this helped me while working with hasclass

$( "div" ).click(function() {

if ( $( this ).hasClass( "protected" ) ) {

    $( this )
        .animate({ left: -10 })
        .animate({ left: 10 })
        .animate({ left: -10 })
        .animate({ left: 10 })
        .animate({ left: 0 });

}

});

reference link

Upvotes: -1

Ben McCormick
Ben McCormick

Reputation: 25718

You had a few typos:

$('#accountloginsignup h1').click(function() {
        if ($('#takeonebar').hasClass('slamdown')){
            $('#takeonebar').removeClass('slamdown');
        } else {
            $('#takeonebar').addClass('slamdown');
          }
        });

but what you really want is $('#takeonebar').toggleClass('slamdown')

jquery Docs

Upvotes: 1

Related Questions