Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove <div> using jQuery

I have this HTML code:

<div id="note_list">
  <div class="note">
    Text 1
    <a href="">X</a>
  </div>
  <div class="note">
    Text 2  
    <a href="">X</a>
  </div>
  <div class="note">
    Text 3  
    <a href="">X</a>
  </div>
  <div class="note">
    Text 4  
    <a href="">X</a>
  </div>
  <div class="note">
    Text 5  
    <a href="">X</a>
  </div>  
</div>

Now I would like to use jQuery to delete a <div> element AFTER the 'X' clicking, is it possible?

First X closes:

  <div class="note">
    Text 1
    <a href="">X</a>
  </div>

etc etc. Can I remove a div without using id=""?

Thank you!

like image 369
Dail Avatar asked Oct 19 '11 10:10

Dail


2 Answers

$(".note a").click(function(e) {
    e.preventDefault();
    $(this).parent().remove();
});

or instead of remove() you could use slideUp()

like image 196
Manuel van Rijn Avatar answered Sep 20 '22 09:09

Manuel van Rijn


Yes, use jQuery's traversal methods to find the correct element. In this case, you just need parent():

$('div.note a').click(function(event) {
    event.preventDefault();
    $(this).parent().remove();
});
like image 3
lonesomeday Avatar answered Sep 22 '22 09:09

lonesomeday