views:

17

answers:

1

I've been using jquery to do some image swapping and fading.

I have a div, with and id of cup-builder-error

I use jquery to load images into divs, then on error fade in and out the error div

$("#imgShell").attr("src","products/components/" + idArray[2] + ".png").error(
                                        function(){
                                            $("#cup-builder-error").fadeIn('fast');                                             
                                            $("#cup-builder-error").fadeOut('slow');
                                        });

that code is surrounded by $("a").click( function(event){

What happens is the fade in / out routine loops the number of time that the error happens.

What did I do wrong?

Thanks for the help!

+1  A: 

Each time the "a" is clicked, a new error event is being bound. You only need to do it once.

$('#imgShell').error(function () {
    $("#cup-builder-error").fadeIn('fast');                                             
    $("#cup-builder-error").fadeOut('slow');
});

$('a').click(function (event) {
    $('#imgShell').attr("src","products/components/" + idArray[2] + ".png");

    event.preventDefault();
});
Matt
That was exactly what I needed. I have images with links on them currently to do the image swapping, if I use the prevent default would it stop the click action from being sent to the browser?
Kris.Mitchell
It will prevent the default action being executed. When used in the click event for anchor tags, it prevents the href attribute being followed. When used in the submit event for forms, it prevents the form being submitted.
Matt