Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery detecting and removing an element clicked

I have a hierachy of DIVs with classes associated but not IDs. How can I remove the item being clicked?

<div>
    <div class="minibox" onclick="remove_me()">Box1</div>
    <div class="minibox" onclick="remove_me()">Box1</div>
    <div class="minibox" onclick="remove_me()">Box1</div>
    <div class="minibox" onclick="remove_me()">Box1</div>
    <div class="minibox" onclick="remove_me()">Box1</div>
</div>
<script>
    function remove_me(){
    ///remove the clicked div
    }
</script>
like image 807
Shahid Karimi Avatar asked Mar 14 '11 14:03

Shahid Karimi


People also ask

How to know which element is clicked in jQuery?

You can simply use the event. target property to get the class from any element which is clicked on the document in jQuery.

How do I get the clicked element in an event?

To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.


2 Answers

$('div .minibox').click(function(e){
    $(e.target).remove();
});
like image 181
gion_13 Avatar answered Dec 08 '22 13:12

gion_13


$('.minibox').click(function() { $(this).remove(); });
like image 26
JaredMcAteer Avatar answered Dec 08 '22 13:12

JaredMcAteer