I wrote a script for searching pictures for the block I needed:

$('.craft').each(function() { var craft_id = $(this).attr('jet-id').toLowerCase(); var that = $(this); $.ajax({ url: '/img/crafts/' + craft_id + '.png', success: function() { that.find('.craft-searcher').attr('src', '/img/crafts/' + craft_id + '.png'); }, error: function() { that.find('.craft-searcher').attr('src', '/img/crafts/' + craft_id + '.jpg'); } }); }); 

The script's job is to search for a .png image and, if such a picture is not found, then a .jpg image will be attached to the block.

Question: how to attach another picture if neither .png nor .jpg were found? The script is able to handle only one fail ().

UPD: noticed that the script does the search twice. Why?

    1 answer 1

     $.ajax({ url: '/img/crafts/' + craft_id + '.png', success: function() { that.find('.craft-searcher').attr('src', '/img/crafts/' + craft_id + '.png'); }, error: function() { $.ajax({ url: '/img/crafts/' + craft_id + '.jpg', success: function() { that.find('.craft-searcher').attr('src', '/img/crafts/' + craft_id + '.jpg'); }, error: function() { that.find('.craft-searcher').attr('src', '/img/crafts/' + craft_id + '.gif'); } }); } }); 

    To respond to

    the script does the search twice. Why?

    There is not enough code in the question.