I’m currently using the following code to append a X link to each element on rollover.
$(".item").hover(function() {
var id = $(this).attr("id");
var roll = $('<a class="close" href="#">X</a>').hide();
roll.prependTo($(this)).fadeIn(0);
}, function() {
$(this).find("a:first").fadeOut(0, function() {
$(this).remove()
});
});
What I am unable to code up is the ability for when this X link is pressed, it removes the parent div it is currently in.
Would love help on this 🙂
Well, firstly I would suggest that dynamically adding and removing elements on a hover() event could be problematic. The usual solution is to show/hide them as appropriate. It’s far more performant too.
So:
and
and
Now you can do this by dynamically adding the link to the DOM but I would advise against it. If you do however, use
live()for the event handling: