javascript - JQuery - Adding class onto element, and then interacting with that class doesn't work -
when click ( ".toggle-button a" )
, adds .close
class itself. , fades in .info-overlay
this works fine, when click again, want info-overlay
fade out again, , close
class removed. doesn't work.
am missing here?
http://jsfiddle.net/bazzle/xps9p/1/
html
<div class="toggle-button"> <a>click</a> </div> <div class="info-overlay"> content </div>
css
.info-overlay{ display:block; width:100px; height:100px; background-color:green; display:none; };
js
$( ".toggle-button a" ).click(function() { $( ".info-overlay" ).fadein("500"); $(this).addclass('close'); }); $( ".toggle-button a.close" ).click(function(){ $( ".info-overlay").fadeout("500"); $(this).removeclass('close'); });
use event delegation:
change
$( ".toggle-button a.close" ).click(function(){ $( ".info-overlay").fadeout("500"); $(this).removeclass('close'); });
to:
$(document).on('click',".toggle-button a.close",function(){ $( ".info-overlay").fadeout("500"); $(this).removeclass('close'); });
because .click()
attach , forget handler, .on()
dynamic.
Comments
Post a Comment