This problem is bugging me out a bit. Should be a very simple solution but I can't quite grasp it. What I am doing is creating several nested elements via Jquery and then removing it from the page using JQuery.fade() method.
The close button on the element created does not fire the event but if applied to an independant button, it works.
Javascript:
$( document ).ready(function() {
$("button.create").click(function(){
// action goes here!!
// $(".alertBox").fadeIn();
$('.alerts').append('<div class="alertBox"><span class="hide"><a href="#">X Close</a></span><p><strong>Alert!</strong> Thank you for submitting your comment. Scroll to the bottom of the page to view it.</p></div>');
return false;
});
$("button.close").click(function(){
// action goes here!!
$(".alertBox").fadeOut();
return false;
});
$(".hide").click(function(){
// action goes here!!
alert("check check");
$(".alertBox").fadeOut();
});
});
HTML:
<h1>Fun with Alert Boxes</h1>
<p>This page is to test the ability to add or remove alert boxes from the DOM. By clicking on the button below, you will make an alert box appear and then you will be able to close the alert box with a button in the top right corner. This will all rely on JQuery to add and remove content from the DOM.<p>
<div class="alerts"></div>
<p> </p>
<button class="create">Create Alert</button>
<button class="close">Destroy Alert</button>
And I have a Fiddle for it http://jsfiddle.net/coolwebs/5n5nosdc/4/ - I have checked it with Console log and can't see any obvious errors. Obviously the issue is that because the function is declared before the element is available on the page, is it possible that that is why it does not work?
Update: Did some testing by generating the script dynamically and it proved my suspicion.