jQuery - check if class exists if no destroy a function -
i trying destroy function depending on existence of css class.
i want check class .active
, class toggled clicking button. tried using
if(jquery(#test).hasclass('.active')
but work once, if remove .active
still function running whereas want destroy after removal of .active
class.
i tried .unbind()
no use.
please tell me how destroy function totally after removal of .active
class
function destroy_no_active_class(){ jquery('#test.active').mouseover(function (e) { e.stoppropagation();e.preventdefault(); jquery(this).addclass('highlight'); }).mouseout(function () { jquery(this).removeclass('highlight'); }).click(function(e){ e.stoppropagation(); e.preventdefault(); show_data(this); }); }); }
assuming 'destroy function' mean want stop highlighting element, can check within handler once it's triggered, , execute logic conditionally. more useful unbinding handler, since automatically work again if element becomes 'active' again.
function destroy_no_active_class(){ jquery('#test').on('mouseover.mine mouseout.mine', function (e) { if ($(this).is(".active")) { e.stoppropagation(); e.preventdefault(); jquery(this).toggleclass('highlight'); } }).on('click.mine', function(e){ if ($(this).is(".active")) { e.stoppropagation(); e.preventdefault(); show_data(this); } }); }); }
within handler, alternatively do
if (!$(this).is(".active")) { $(this).off('mouseover.mine mouseout.mine click.mine'); }
the downside here that, if element becomes active again, have rebind handlers. (note $
alias jquery
. if have naming conflict, substitute jquery
$
in code above have been doing.)
Comments
Post a Comment