Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete parent element using jQuery

I have some list item tags in my jsp. Each list item has some elements inside, including a link ("a" tag) called delete. All that I want is to delete the entire list item when I click the link.

Here is the structure of my code:

$("a").click(function(event) {    event.preventDefault();    $(this).parent('.li').remove();  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <li id="191" class="li">    <div class="text">Some text</div>    <h4><a href="URL">Text</a></h4>    <div class="details">      <img src="URL_image.jpg">      <span class="author">Some info</span>      <div class="info"> Text        <div class="msg-modification" display="inline" align="right">          <a name="delete" id="191" href="#">Delete</a>        </div>      </div>    </div>  </li>

But this doesn't work. I'm new at jQuery, so I tried some things, like for example:

$(this).remove(); 

This works, it deletes the link when clicked.

$("#221").remove(); 

This works, it deletes the indicated list item, but it's not "dynamic".

Can someone give me a tip?

like image 927
Noob Avatar asked Jul 11 '11 08:07

Noob


People also ask

What is unwrap () in JS?

unwrap() method removes the element's parent and returns the unwrapped content. This is effectively the inverse of the . wrap() method. The matched elements (and their siblings, if any) replace their parents within the DOM structure.

What does addClass do in jQuery?

jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

What is unwrap in jQuery?

The unwrap() method is an inbuilt method in jQuery which is used to remove the parent element from the selected element. Syntax: $(selector).unwrap() Parameters: This method does not accept any parameter. Return Value: This method returns the selected element with the changes made by unwrap() method.


1 Answers

Simply use the .closest() method: $(this).closest('.li').remove();
It starts with the current element and then climbs up the chain looking for a matching element and stops as soon as it found one.

.parent() only accesses the direct parent of the element, i.e. div.msg-modification which does not match .li. So it never reaches the element you are looking for.

Another solution besides .closest() (which checks the current element and then climbs up the chain) would be using .parents() - however, this would have the caveat that it does not stop as soon as it finds a matching element (and it doesn't check the current element but only parent elements). In your case it doesn't really matter but for what you are trying to do .closest() is the most appropriate method.


Another important thing:

NEVER use the same ID for more than one element. It's not allowed and causes very hard-to-debug problems. Remove the id="191" from the link and, if you need to access the ID in the click handler, use $(this).closest('.li').attr('id'). Actually it would be even cleaner if you used data-id="123" and then .data('id') instead of .attr('id') to access it (so your element ID does not need to resemble whatever ID the (database?) row has)

like image 169
ThiefMaster Avatar answered Sep 29 '22 10:09

ThiefMaster