Reputation: 1
Can i delete a text box dynamically?
Upvotes: 0
Views: 528
Reputation: 126042
(Edited after seeing comment in OP):
To get StackOverflow-like functionality, you could do something like this:
HTML:
<div class="comment">
This is a test this is a test this is a test.
</div>
JavaScript:
$(".comment").hover(function() {
$(this).append("<span class='close'>X</span>");
}, function() {
$(this).find("span.close").remove();
}).delegate(".close", "click", function() {
$(this).closest("div.comment").remove();
});
Here's a working example: http://jsfiddle.net/andrewwhitaker/xQTSb/
It all looks a little hectic, but here's what's going on:
hover
, which takes a function to execute on mouseenter
and again on mouseleave
span
that removes the comment is added or removed, respectively.delegate
is used to bind a click event to the span (necessary because the span
does not exist until the user hovers over the comment
div).Upvotes: 1