1

Hey guys, this is simple but I can't make it workn... blah!

I have a UL with many LI. Inside of each LI I have a and two :

<ul>    
  <li><span>Qualification</span> - <a class="editItem" href="get/14">edit</a> | <a class="deleteItem" href="delete/14">delete</a></li>
</ul>

When I click the delete anchor, I would like to hide the LI. How can I select the LI?

I tried something like this:

$(".deleteItem").click(function(e) {
     $(this).parent().find("li").hide();
     // or
     $(this).prev().prev().prev().hide();

});

But it doesn't work at all. :( What am I doing wrong? Thanks..

1
  • Thanks everybody! For some reason, using $(this) inside my ajax function wasn't working. But all the answers above are right. Thank you again! Commented Mar 1, 2010 at 16:57

5 Answers 5

5

To get the LI:

$(".deleteItem").click(function(e) {
  $(this).closest("li").hide();
});

.prev() refers to siblings, in this case <li> is a parent to the link, .closest() gets the nearest parent matching the selector.

Sign up to request clarification or add additional context in comments.

Comments

3

The li is the parent of the anchor you're clicking, so you could do this:

$(".deleteItem").click(function(e) {
    e.preventDefault();
     $(this).parent().hide();

});

Comments

2
$(".deleteItem").click(function(e) {
    $(this).closest('li').hide();
});

Try that.

Comments

1

should be simply:

$(this).parent().hide();

the LI is the parent of the A element. Others are siblings. Prev is used to find the previous sibling, so it wont give you the parent.

Comments

1

It appears your need to fire off a request to delete the record itself from the database too. So rather than focusing on just hiding the li, you shouldn't neglect your link's original purpose:

$(".deleteItem").click(function(e){
  // prevent page-change
  e.preventDefault();
  // ask server to delete this record
  $.post($(this).attr("href"), function(result){
    // remove this record upon server-response
    $(e.target).closest("li").slideUp().remove();
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.