1

I've got a function that handles various types of behaviour for a property listing (My Shortlist) which is a <ul><li> setup. One type of behaviour is removing a property listing item <li> when you click a button within each item, that's working fine however my if statement to check when all items have been removed isn't working.

Can you tell me what I'm doing wrong? Here's the part of the function that handles removing items via the onclick event of the button and the dodgy if statement:

// Remove an item from the shortlist
$(this).find('.btn-minus').click(function() {
    var btnRemove = $(this);
    var propTile = $(this).parents('.property-tile');
    var propList = $('#property-listings');
    // If IE 8 / 7
    if ($('html').hasClass('lte8')) {
        propTile.hide('slide', 300, function()  {
            btnRemove.data("tooltip").hide();
        });
    }
    // All other browsers
    else {
        propTile.fadeOut(200, function()  {
            btnRemove.data("tooltip").hide();
            $(this).remove();
        }); 
    }
    if (propTile.length === 0) {
        propList.remove();
    }
});

And here's the call to the function: $(".my-shortlist").myShortlist(); where .my-shortlist is the <ul> element.

Thanks

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
crite
  • 138
  • 9
  • Isn't the problem that you tell the prop tile to slowly fade out (200 milliseconds) and then remove it, but then immediately (before the fade is done) check if the length is 0 (which it isn't yet, it is still fading)? – Roger Lindsjö Jun 05 '12 at 07:33
  • Yes I see that now, so I've set up a callback when the `fadeOut` is complete but it's firing after each individual item has been removed, I'm struggling to set it up so it fires once ALL items have been removed from the `
      `.
    – crite Jun 05 '12 at 10:20
  • I got it working, I had to detect the last `
  • ` using the `nextAll()` method then once the `fadeOut` had finished on the last `
  • ` remove `propList` like this: `propTile.fadeOut(400, function() {btnRemove.data("tooltip").hide(); $(this).remove();if (count == 0) {propList.remove();}})`
  • – crite Jun 05 '12 at 10:38