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

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -