You're close, but the binding isn't dynamic in the way you think. What you're doing here:
$('.catNameVal').on('hover', function() {
notyId = $(this).attr("catnameid");
$(this).append(' <a>Delete</a>');
$(this).unbind('mouseenter mouseleave');
});
Is using .on()
to bind to all currently known matches to '.catNameVal'
, which won't discover new additions to the DOM after the fact. What you want to do is bind this to a universal parent DOM element to all of the additions. document
is usually a safe choice, since everything is a child of that. Then you'd include in the binding the filter:
$(document).on('hover', '.catNameVal', function() {
notyId = $(this).attr("catnameid");
$(this).append(' <a>Delete</a>');
$(this).unbind('mouseenter mouseleave');
});
What this does is bind the actual event to document
but applies the selector of '.catNameVal'
when evaluating the event. So all matching events which bubble up to document
will be evaluated, checked against the filter, and executed if they match. This catches late-added DOM elements because they will still bubble up to document
.