Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
  }
});
like image 520
NodeDad Avatar asked Apr 24 '13 02:04

NodeDad


People also ask

Has class in if condition in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How do you check the class is present or not?

The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. Syntax: $(selector). hasClass(className);

Can we use if else in class?

if-else statement should be in a function or inside constructor at-least. No. If you want conditions, do it in the functions.


1 Answers

.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');
      }
});
like image 149
Arun P Johny Avatar answered Sep 28 '22 20:09

Arun P Johny