HelloJavaScript: $('div'...">

JQuery on class delete event

Well, I have something like this:

HTML:

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

JavaScript:

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

http://jsfiddle.net/VYukS/

I need to run some function for the event when the div class was deleted, and I do not have any rights to edit functions that add / remove the class from the div.

How can I catch this event when a class is deleted?

The 'click' event was used only as an example, so the class can be removed not only by clicking

+3
source share
3 answers

There is no such event. But you can make it simpler:

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

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

+4
source

NOTE: live()Deprecated. Try using the latest version of jquery

$('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");
});
+2

     $('div').unbind('click', function(){
       $(this).toggleClass("underline");
     });
+1

All Articles