- There is a parent div
dvParent
- There is a child div
dvjQuery
insidedvParent
I am assuming when i empty dvParent
, any events that are on/delegated over child div dvjQuery
will be deleted but this is not happening.
So when I click remove button to empty parent div dvParent
and recreate child div dvjQuery
, i can still see on.mouseenter working.Why ?
JS:
$(document).ready(function () {
var dvjQuery = $("<div/>").attr("id", "dvjQuery").text("some text");
$('#btnAdd').attr('disabled', 'disabled');
$("form").on({
mouseenter: function () {
$(this).addClass('highlight');
},
mouseleave: function () {
$(this).removeClass('highlight');
}
}, '#dvjQuery');
$('#btnRemove').click(function () {
$("#dvParent").empty();
$(this).attr('disabled', 'disabled');
$('#btnAdd').removeAttr('disabled');
});
$('#btnAdd').click(function () {
$("#dvParent").html(dvjQuery);
$(this).attr('disabled', 'disabled');
$('#btnRemove').removeAttr('disabled');
});
});
HTML:
<form>
<div id="dvParent">
<div id="dvjQuery">some text</div>
</div>
<br/><br/>
<input type='button' id='btnRemove' Value=' Remove Div ' />
<input type='button' id='btnAdd' Value=' Add Div ' />
<br/><br/>
</form>
UPDATE:
Strangely this is working. Why ? : JSFIDDLE
I am not using off
on this one.
//
//HTML
//
<input type='button' id='btnRemove' Value=' Remove Div ' />
<input type='button' id='btnAdd' Value=' Add Div ' />
<div id="raw">
<ul id="temp">
<li>One</li>
</ul>
</div>
//
//JS
//
$(document).ready(function () {
var list = $('<ul id="temp"><li>One</li></ul>');
$('#btnAdd').attr('disabled', 'disabled');
$("#temp").on({
mouseenter: function () {
$(this).addClass('highlight');
},
mouseleave: function () {
$(this).removeClass('highlight');
}
}, 'li');
$('#btnRemove').click(function () {
$("#raw").empty();
$(this).attr('disabled', 'disabled');
$('#btnAdd').removeAttr('disabled');
});
$('#btnAdd').click(function () {
$("#raw").append(list);
$(this).attr('disabled', 'disabled');
$('#btnRemove').removeAttr('disabled');
});
});