Use your left/right keys to browse tutorials
jQuery Ajax delete

jQuery Ajax delete

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on July 17, 2009

Removing contents with Ajax is a useful tool to have in any web designers kit. Using a few lines of jQuery we can remove a div and simultaneously remove a record from the database with Ajax. In the download and demo you’ll see a small red cross to the right of each comment. Clicking the cross will remove the comment div with a slide up animation which will remove the div. click the image below to check out the demo.


The Code

We start by writing the dom ready function to simply hide our loading message div which is used as a visual representation to the user when we go to delete a comment.

$(document).ready(function() {

The next block of code is what essentially does the hard work. we have a .click function to start with that when run fades in the ‘#load’ div to show the user that we’re deleting the item.

We then set the variable ‘commentContainer’ which is set to represent the parent element to ‘.delete’ which is the div ‘.box’ as show in the image below.

var commentContainer = $(this).parent();


The variable ‘id’ is then set with the value of the delete button id which would could be the id of the row in the database that you want to delete. This is then posted to the page delete.php. once the Ajax request has been made and a response has been acknowledged we then slide up and remove the div ‘.box’ and fade out the loader. Finally ‘return False;’ is added to the end of the function in order to stop the page from refreshing as we are using an ‘a tag’.

$(function() {
$(".delete").click(function() {
var commentContainer = $(this).parent();
var id = $(this).attr("id");
var string = 'id='+ id ;

   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(){
	commentContainer.slideUp('slow', function() {$(this).remove();});


return false;

I have left the delete.php page for you to add your own database connection script and query in order to use the code for your own needs. If however you have any questions on how to set this up please let me know below.

The complete code along with images is available to download below.


More tutorials from Papermashup
  • Pingback: Favorites from the Feeds #01

  • Dave

    Just what i was looking for! Thanks!

  • Inside the Webb

    This is great! I love how Facebook implements this, and I’d be excited to try it on my blog.

  • Daniel

    Hey Ashley,

    thanks for the tuto! Just a small note: the css file is missing in the zip download.


    • Ashley

      @Daniel thanks for that I’ve updated it the download :)

  • JDStraughan

    This is one of the best jquery tutorials I have seen recently. Added to

  • dottorgonzo

    can also be used for posts?

    • Ashley

      @dottorgonzo You could use if for any purpose to remove content from your database.

  • Pingback: CSS Brigit | jQuery Ajax delete

  • Jake Rocheleau

    This may be similar to other Ajax tutorials I’ve seen, but it’s definitely one of the most streamlined and easy-to-follow tutorials on the subject.

    • Ashley

      @jake thanks for comment and I’m glad you found it useful! :)

  • Pingback: News jQuery Ajax delete | | Web 2.0 Designer

  • Ben

    Cool. Looks similar to David Walsh’s inline delete thing

    • Ashley

      @ben can’t say i’ve seen it. could you post a link?