0

I am doing some code to insert images to a div, and after loading them, the user might be able to trigger an event by clicking on the images, this doesnt work for some odd reason, here is the code:

 $(function(){
    getPictures('pictures.xml');

    function getPictures(picturesXML){
        $.get(picturesXML, function(data){

            var pictures = data;    
            var countElements = $(pictures).find('pic').length;             

            $(pictures).find('pic').each( function( i ){

                var img = $('<img src="images/' + $(this).attr('src') + '" style="top : ' + $(this).attr('top') + 'px; left: ' + $(this).attr('left') + 'px" width=" '+ $(this).attr('w')  +'"/>');

                    img.load( function(){   
                        $('.space').append( $(this) );
                        $(this, 'space').delay(100*i).fadeIn('fast');   
                    });                                 

            })      

            $('.space img').mouseenter(function(){
                alert('hello');

            })

        })
    }
}) 

Is there anybody here who can help me figuring out this. Thanks!

1 Answer 1

1

Regular jQuery event functions tend to not support event bubbling. Try using $.on method. In this case, replace:

$('.space img').mouseenter(function(){
            alert('hello');

        })

with:

$(document).on('mouseenter','.space img', function(){
            alert('hello');

        });

Also, notice the missing semicolon. This should do the trick.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.