Click event keeps firing

Posted by Ben Shelock on Stack Overflow See other posts from Stack Overflow or by Ben Shelock
Published on 2010-03-29T01:40:02Z Indexed on 2010/03/29 1:43 UTC
Read the original article Hit count: 487

Filed under:
|
|

I have absolutely no idea why this is happening but the following code seems to be executed a huge ammount of times in all browsers.

$('#save_albums').click(function(){
    for(var i = 1; i <= 5; i++){
        html = $('#your_albums ol li').eq(i).html();
        alert(html);
    }
});

Looks fairly innocent to me...

Here's the code in it's entirety

$(function(){
    $('#query').keyup(function(){
        var text = encodeURI($(this).val());
        if(text.length > 3){                        
            $('#results').load('search.php?album='+text, function(){

                $('.album').hover(function(){
                    $(this).css('outline', '1px solid black')
                },function(){
                    $(this).css('outline', 'none')
                });

                $('.album').click(function(){
                    $('#fores').remove();
                    $('#yours').show();                                 

                    if($('#your_albums ol li').length <= 4){
                        albumInfo = '<li><div class="album">' + $(this).html() + '</div></li>';

                        if($('#your_albums ol li').length >= 1){
                            $('#your_albums ol li').last().after(albumInfo);
                        }
                        else{
                            $('#your_albums ol').html(albumInfo);
                        }
                    }
                    else{
                        alert('No more than 5 please');
                    }
                });

                $('#clear_albums').click(function(e){
                    e.preventDefault;
                    $('#your_albums ol li').remove();
                });

                $('#save_albums').click(function(){
                    for(var i = 1; i <= 5; i++){
                        html = $('#your_albums ol li').eq(i).html();
                        alert(html);
                    }
                });

            });
        }
        else{
            $('#results').text('Query must be more than 3 characters');
        }
    });
}); 

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about event