Animated AJAX Record Deletion Using MooTools

By  on  

I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with MooTools JavaScript.

The PHP - Content & Header

The following snippet goes at the top of the page:

	$query = 'DELETE FROM my_table WHERE item_id = '.(int)$_GET['delete'];
	$result = mysql_query($query,$link);

The following is used to display the records:

$query = 'SELECT * FROM my_table ORDER BY title ASC';
$result = mysql_query($query,$link);
while($row = mysql_fetch_assoc($result))
	echo '<div class="record" id="record-',$row['item_id'],'">
				<a href="?delete=',$row['item_id'],'" class="delete">Delete</a>

The MooTools JavaScript

window.addEvent('domready',function() {
	$$('a.delete').each(function(el) {
		el.addEvent('click',function(e) {
			var parent = el.getParent('div');
			var request = new Request({
				url: 'mootools-record-delete.php',
				link: 'chain',
				method: 'get',
				data: {
					'delete': parent.get('id').replace('record-',''),
					ajax: 1
				onRequest: function() {
					new Fx.Tween(parent,{
					}).start('background-color', '#fb6c6c');
				onSuccess: function() {
					new Fx.Slide(parent,{
						onComplete: function() {

For every link, we add a click event that triggers the AJAX request. During the request, we tween the containing element to a red background. When the AJAX request returns a "success" response, we slide the element off of the screen.

How would you use this? Share!

Recent Features

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    Chris Coyier&#8217;s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    Flashy FAQs Using MooTools Sliders

    I often qualify a great website by one that pay attention to detail and makes all of the "little things" seem as though much time was spent on them. Let's face it -- FAQs are as boring as they come. That is, until you...

  • By
    MooTools Documentation Search Favelet

    I'm going to share something with you that will blow your mind: I don't have the MooTools documentation memorized. I just don't. I visit the MooTools docs frequently to figure out the order of parameters of More classes and how best to use...


  1. I first saw this type of effect on WordPress and immediately fell in love with it.

    This is good JS. :)

  2. Very nice indeed! Great work.

  3. I haven’t done any PHP for a long time, but what will happen you I call:
    ?delete=1 or 1=1

    Does mysql_query protect from sql injection?

  4. Hi David,
    i’d like to have your answer to a question I posted on your article about binding… could you help me?
    Nunzio Fiore

  5. Monkeytail


    Where in the MooTools docs can I find more about Fx.Slide ‘onComplete’ option ?

  6. cssProdigy

    Another great post.

  7. This sounds great, but have you checked what kind of load it puts on the server? I know it sounds crazy, but I checked my logs recently and my #1 bandwidth hog was the admin-ajax.php file in the WordPress admin panel.

  8. @Pedro: The “(int)” typecasting will make the $_GET[‘id’] become “0”, in your case.

    @Agitationist: This puts very, very little load on the server. Basically none. Sure the WordPress file has issues, but the code above is easy on the server.

  9. Great, thanks – good to know.

  10. Salih Gedik

    Very Nice. Thank you. I like mootools

  11. Whenever I see this, I always ask why it is – Why do you create a new request object with every click instead of creating it outside of the click and reusing it?

    var my_req = new Request();
    my_req.send(data); //inside of your click event
  12. Good combination between Class Request and Class Fx.

    Thanks for sharing.

  13. @emehrkay: Great catch — you could just use one Request.

  14. I always liked that effect in WordPress, too. It gives the user proper feedback after clicking delete, making the experience more enjoyable and user-friendly.

  15. Hi all, I use this post for asking all of you a question about binding, I posted on another example of our David Walsh . The questio is here somebody can answer to this doubt?

    thank you to all and a special to David

  16. To help protect from SQL injection, I always check that the referring page is the one you expect it to be, i.e.
    if (isset($_GET[“delete”]) && is_numeric($_GET[“delete”]) && (array_shift(explode(“?”, basename($_SERVER[“HTTP_REFERER”]))) == “show_records_page.php”)) { … }

    This checks the referring page’s filename, removes any query string and compares against whatever filename you put in for your page that calls the Request. I’d always try to catch a dodgy query rather than run it with something that defaults back to something ‘safe’.

  17. Would love a rewrite for jQuery. It is by far the superior liberary in my view and I already use it on the projects in which I can see use for this.

  18. Truthseeker

    Amazing thing, I tried something like this myself but it wasn’t working, now I see what I did wrong. Thank you.

  19. This sample isn’t OK. Instead

    $result = mysql_query($result,$link); 

    you must write

    $result = mysql_query($query,$link); 

    By the way, excellent article…

  20. Michael

    Could someone please show me a working example using CodeIgniter?

  21. bsn

    I would definitely suggest returning a significant response to the XMLHttpRequest – you seem to assume that the row has been deleted and visually confirm this to the user, regardless of what has happened server-side. Possible scenarios where the DELETE query could fail are:

    MySQL refused the connection for whatever reason
    The row does not exist (deleted by another user?)
    User does not have permission to delete certain rows

    Something always goes wrong, normally when the client’s spouse is clicking aroung in IE7.

    Why not return ‘0’ on success, and an integer error code (e.g. ‘1 : database error’, ‘2 : row not exist’) for any other eventuality? Then the ‘view’ can act accordingly.

    I would also suggest adding handlers to tackle a failed or timed-out XMLHTTPRequest, and then provide appropriate feedback to the user. There’s nothing worse than an ajax “waiting” icon that just doesn’t go away, even when the request is completed. (OK, there are worse things.)

    Just my quarter of a byte.

  22. hi ,

    i just want to ask that why it does not works on false elements like i m creating li elements through js and using ur script to delete li elements but it does not work but when i hardcoded the li elements on page then it works.. . please any one sortout this problem….

    here is my code..

    window.addEvent('domready', function() {
    //This is the function that will run every time a new item is added or the 
    //list is sorted.
    var showNewOrder = function() {
        //This function means we get serialize() to tell us the text of each 
        //element, instead of its ID, which is the default return.
        var serializeFunction = function(el) { return el.get('text'); };
        //We pass our custom function to serialize();
        var orderTxt = sort.serialize(serializeFunction);
        //And then we add that text to our page so everyone can see it.
        $('data').set('text', orderTxt.join(' '));
    function valueExist(myval){
        var x=0; 
        if(myKeywords.length > 0){
            for (x=0;x<=myKeywords.length;x++){
                if(myKeywords[x]==myval){return true;}
            }// and the loop goes on until...
        //  begin your js code for deletion
    $$('a.delete').each(function(el) {
        el.addEvent('click',function(e) {
            var request = new Request({
                url: 'mytest.php',
                link: 'chain',
                method: 'get',
                data: {
                    'delete': el.getParent('li').get('id').replace('record-',''),
                     ajax: 1
                onRequest: function() {
                    new Fx.Tween(el.getParent('li'),{
                    }).start('background-color', '#fb6c6c');
                onSuccess: function() {
                    new Fx.Slide(el.getParent('li'),{
                        onComplete: function() {
       //  end your js code for deletion
    //This code initalizes the sortable list.
    var sort = new Sortables('.todo', {
        handle: '.drag-handle',
        //This will constrain the list items to the list.
        constrain: true,
        //We'll get to see a nice cloned element when we drag.
        clone: true,
        //This function will happen when the user 'drops' an item in a new place.
        onComplete: showNewOrder
    //This is the code that makes the text input add list items to the <ul>,
    //which we then make sortable.
    var i = 1;
    var myKeywords = new Array();
    $('addTask').addEvent('submit', function(e) {
        //Get the value of the text input.
        var val = $('newTask').get('value');
        //The code here will execute if the input is empty.
        if (!val) {
            return; //Return will skip the rest of the code in the function. 
                alert("You have already added this keyword.");
                //Return will skip the rest of the code in the function. 
        //Create a new <li> to hold all our content.
        var li = new Element('li', {id: 'record-'+i});
        //the draggable element.
        var div_clear = new Element('div', {'class':'clear'});
        //This handle element will serve as the point where the user 'picks up'
        //the draggable element.
        //var handle = new Element('div', {id:'handle-'+i, 'class':'drag-handle'});
        //handle.inject(li, 'top');
        var div_img = new Element('div', {'style':'float:right;padding:0px;'});
        var div_keyword = new Element('div', {'style':'float:left;margin-top:2px;', text:val });
        var img_anchor = new Element('a', {'href':'?delete='+i,'title':'Delete','class':'delete'});
        var img = new Element('img', {'src':'images/delete.png','alt':'Delete','width':'24','height':'24','border':'0'});
        //var input = new Element('input', {'type':'checkbox',id:'chk'+i,name:'chk'+i});
        //Set the value of the form to '', since we've added its value to the <li>.
        $('newTask').set('value', '');
        //Add the <li> to our list.
        //Do a fancy effect on the <li>.
        //We have to add the list item to our Sortable object so it's sortable.
        //We put the new order inside of the data div.


  23. Stephen


    it looks ugly if it is a table and a complete row () that has to be disposed.
    Any ideas?


  24. Peter

    How to modify this script to adding the entry to base?

  25. Kyle

    First off, great article, I really like the instant feedback and animation when deleting. I do have one (hopefully) simple question:

    Everything works fine, except the fact that the record is not actually deleting from my database. The animation works beautiful, but on a page refresh, the deleted entries re-appear. And the interesting thing is, as soon as I remove the MooTools script, from the HEAD, the entries are deleted with no problem. Just no animation :-(

    Any ideas?

  26. pete

    Yep – thanks for all the work you spent in your excelent articles!

    Got the same problem as @Kyle: changed the url in the javascript but the script only deletes from my database if i run it without the
    mootools code.

    Any idea …??

  27. Andy94

    Hi David, my best regards for your website. I’ve never seen a site like this one, it’s so good.
    I have the same problem as pete and Kyle: when I click on an element, the animation works fine but there is no deletion. If I reach the page by putting the querystring on the addressbar, all works fine.
    Can you help us?
    I really appreciate that.

    P.S. Sorry for my English, I’m an italian boy, and I am just 14.

  28. @Andy94: Have an example I can look at online?

  29. Andy94

    @David: of course I have one. You can visit this website THIS. This is just a test which extracts data from a db, it is located in localhost. As tou can see, the animation works fine, but if you refresh the page the element that you deleted reappear. The PHP code, is located at the end of this comment.
    Thank you so much David.

    mysql_connect(my connection parameters);
    $query = 'SELECT * FROM theuploader_log ORDER BY logid ASC';
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result))
     <div class="record" id="record-',$row['logid'],'">
       <a href="index.php?delete='.$row['logid'].'" class="delete">Delete</a>


    mysql_connect(my connection parameters);
     $query = 'DELETE FROM theuploader_log WHERE logid='.$_GET['delete'].'';
     $result = mysql_query($query);
  30. Andy94

    @David: of course I have one. You can visit this website THIS. This is just a test which extracts data from a db, it is located in localhost. As tou can see, the animation works fine, but if you refresh the page the element that you deleted reappear. The PHP code, is located at the end of this comment.
    Thank you so much David.

    I will post the PHP code in another comment, because your system doesn’t allow me to send comments with PHP code inside.

  31. Andy94

    @David: Here is my PHP code. Sorry but I had to htmlentities it, so the brs cannot be displayed.

  32. I have been trying to get this working on my site with no luck. Not sure what the problems is. I have implemented everything you have listed above and the closest I can get is when you click the delete link the div fades to red but doesn’t slide away even though the content in the database IS being deleted. What am I doing wrong?

  33. This is cool. Mootools really make things cool. I’ve implemented the this kind of slide/fade style on few of my projects but never tried to do it from scratch..

  34. Hrm… awesome tutorial/demo sooooo useful.

    For some reason I can’t get it to send the “delete” id variable back to the page or to any other. When I look at the requests being made even on your demo it just requests the file


    There aren’t any variables in the query string although there are in the link you click on. Is this correct? If so I don’t understand how the PHP can check to see if the variable isset() for the delete to work.

    if(isset($_GET[‘delete’])) { }

  35. I made two mods to this: (Click once to select, again to delete or let timeout). (Made into a Sortable list with deletion confirmation).

    ps. Having a hard time getting around the Spam filter setup for these comments… ad to remove the links to get posted.

  36. Very nice and useful things, thnx to share it with us…

  37. Alexander

    Implemented and now i have it working with any problem, but i would like to know how to disable if the user wants to click it once again and the first request is not yet finished.

    I intentinally use a php sleep() function and if i click several time it will send several request. So i need to prevent that.

    Any ideas?


  38. @Alexander: Hide the “delete” link when the item’s been clicked.

  39. Alexander

    Thanks David, did not think about that! hehe..i was more focused on “disabling” the click intead of removeing the delete.


  40. Martin


    I have the code inserted, only difference is I am using an image as the delete link rather than plain text and on inspection everything appears fine but nothing happens when I click the link.

    I don’t have the ‘$link’ variable in the queries because I don’t see it declared anywhere and don’t know what it does. If i include it I get errors anyway.

    Just to see if anyone could explain it’s purpose.


  41. Ronald


    This may sound strange, but how do I get this to work with Mootools 1.1.
    This is due to the fact that the CMS I am programming for, works with the old mootools version.


  42. Paul

    @Michael: Did you manage to get it working with CodeIgniter?

  43. Paul

    @Michael: Did you manage to get it working with CodeIgniter?

  44. Hi everyone,

    I have tried to get this working again.

    Everytime I use $link somewhere it breaks and I don’t know what this variable is meant to be doing, it is never declared anywhere?

    Has anyone stripped out any unnecessary code and got it to work?

  45. Try as I might, I can’t get the script to work with a single delete button at the bottom of the list rather than the delete link in each row. The idea is to select one or more rows and press one button to delete the rows.
    Any ideas? anyone do this yet?

  46. Hi David
    This is really good tutorial,but I got the same problem with some people. The animation working fine, but the deleting not working. Deletion only working when disable the animation.
    Please advise that!

  47. Hey david,you know ? I Fucking your mother.
    Mad !! this code is not working.not deleting.NO NO NOOOOOOOOO !!!!! fuck fuck fuck you david..

    Plase fuck of david ! david amına koyim david !!

    • Alex

      Why do you hate him so much?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!