Tukhsanov
Tukhsanov

Reputation: 303

removeClass not working jQuery

I wanted to create something when cliked to the button the background should change one image to another image such as toggle. However i used removeClass and addClass but not working. Here is my code.

html

<button>click</button>

jquery

var button = $('button').eq(0);
var html = $('html');
button.on('click', function(){

  if(html.hasClass == 'have'){
    alert('do');
    html.css({
    background : 'url(http://img.pixland.uz/u2080f129653m.jpg)'
    }).removeClass('have');
  }else{
    html.css({
    background : 'url(http://img.pixland.uz/u13301f281573m.jpg)'
    }).addClass('have');
  }

});

DEMO

Upvotes: 0

Views: 66

Answers (3)

sp00m
sp00m

Reputation: 48837

Check the syntax of hasClass:

if (html.hasClass('have')) {
    //
}

Upvotes: 4

Milind Anantwar
Milind Anantwar

Reputation: 82251

hasClass should hasClass('have'):

if(html.hasClass('have'));

Demo

Upvotes: 1

konghou
konghou

Reputation: 557

syntax should be

if(html.hasClass('have')){ ... }

Upvotes: 1

Related Questions