ReAdding Event Handler After Dynamically Removing and ReAdding Element
Hello everyone,
I think the title should be pretty clear in terms of describing the issue. Basically, I'm trying to create a reset() method where I can remove then immediate readd the element and it's event handler. Right now, it removes the element (which is initially built into the HTML) and readds the element. So that works, but the original events that were tied to that element are not added. Any idea as to why? Any help is appreciated. Here is the code I have:
- function resetAllOverlays()
- {
- var overlayBG = $("<p class=\'grid-overlay\' style=\'height:300px;\'></p>");
- var overlayText = $("<p class=\'grid-block-text\'></p>");
- var gridChildren = $(".grid").children();
- $(gridChildren).each(function(){
- // first remove all grid child elements
- $(this).empty();
- // then add them back
- $(this).append(overlayBG);
- $(this).append(overlayText);
- });
- // Add The Handlers Back
- $("p.grid-block-text").each(function() {
- $(this).on("mouseenter", function() {
- addOverlay($(this));
- });
- });
- $("p.grid-block-text").each(function() {
- $(this).on("mouseleave", function() {
- removeOverlay($(this));
- });
- });
- }