Can i delete a text box dynamically?
-
1It would be helpful to explain what you're trying to do and what you've tried so far. It would also be helpful if you didn't completely replace one question with a different question after someone has already answered.Bill the Lizard– Bill the Lizard2011-02-04 18:01:43 +00:00Commented Feb 4, 2011 at 18:01
-
1@Bill the Lizard:I want to delete the text box automatically.If u take stackoverflow if i move a cursor on a comment ,i get a cross icon.How do i do it?gaurav– gaurav2011-02-04 18:15:30 +00:00Commented Feb 4, 2011 at 18:15
Add a comment
|
2 Answers
(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:
- Uses
hover, which takes a function to execute onmouseenterand again onmouseleave - Inside those functions, the
spanthat removes the comment is added or removed, respectively. delegateis used to bind a click event to the span (necessary because thespandoes not exist until the user hovers over thecommentdiv).
4 Comments
gaurav
When i move my cursor to edge of text box,i need to have a small cross icon,if i click it should delete
Andrew Whitaker
@gaurav: I noticed your comment on the original post and updated my answer.
gaurav
This is working in jsfiddle but not working in localost,should i need to add js1.5 and use on load.Thnx for that gr8 explanation.
Andrew Whitaker
@gaurav: Yes, you need to include jQuery 1.5 in the
head element of your document, something like this: <script type="text/javascript" src="jquery.js"></script>$('#your_text_box_id').remove()
1 Comment
gaurav
When i move my cursor to edge of text box,i need to have a small cross icon,if i click it should delete.