3

Well, I got something like this:

HTML:

<div id="main" class="underline" >Hello</div>

JavaScript:

$('div').live('click', function(){
    $(this).toggleClass("underline");
});​

https://jsfiddle.net/VYukS/

I have to run some function on event when div class was removed, and I haven't got any permisions to edit functions that adding / removing class from div.

How can I catch that event when class is removing?

'click' event was used only as an example, so that class can be removen not only on the clicking

Syscall
  • 19,327
  • 10
  • 37
  • 52
Igor Konopko
  • 764
  • 2
  • 13
  • 26
  • 1
    possible duplicate of [jQuery - Fire event if CSS class changed](http://stackoverflow.com/questions/1950038/jquery-fire-event-if-css-class-changed) (in particular, the second answer: http://stackoverflow.com/a/1950199/444991) – Matt May 15 '12 at 10:43
  • In future, please don't *just* include a link to jsFiddle. Your post should standalone from any other resource; consider what'd happen if jsFiddle went down in the future. – Matt May 15 '12 at 10:43

3 Answers3

4

There is no such event. But you can do simpler:

$('div').live('click', function(){
    $(this).toggleClass("underline")
    if ($(this).hasClass("underline")) {
        alert("added");
    } else {
        alert("removed");
    }
});​

DEMO: https://jsfiddle.net/VYukS/1/

Syscall
  • 19,327
  • 10
  • 37
  • 52
VisioN
  • 143,310
  • 32
  • 282
  • 281
2

NOTE: live() is deprecated. Try to use jquery newest version

$('body').on('click', 'div#main', function(){
    $(this).toggleClass("underline");
    // to check the class presence
   if($(this).hasClass('underline'))
     alert('no class');
   else
     alert('has underline');
});

or use delegate()

$('body').delegate('div#main', 'click', function(){
    $(this).toggleClass("underline");
});
Syscall
  • 19,327
  • 10
  • 37
  • 52
The System Restart
  • 2,873
  • 19
  • 28
1

Try this

     $('div').unbind('click', function(){
       $(this).toggleClass("underline");
     });
pradip
  • 138
  • 6