2

I have a situation where only the first click of an image works, the second time when clicked the other function doesn't run.

<script>
jQuery(document).ready(function($) {
   $('#temp_container img').click(function(){
     var img = $('#temp_container img').attr('src');
     $('#temp_container2').append('<img src="'+img+'">');
   });

   $('#temp_container2 img').click(function(){
     alert('works');
   });
});
</script>

<div id="temp_container">
   <img src="http://cdn-images.farfetch.com/10/62/22/35/10622235_3103339_170.jpg">
</div>
<div id="temp_container2"></div>

When you click on the image in div temp_container, the image is duplicated and appended to temp_container2, when clicking on the image in div temp_container2 an alert should be executed. But for some reason this doesn't execute when run. I've been stuck on this issue for hours.

Thanks

3
  • 1
    Look at delegating events in jQuery - since you are declaring the 2nd listener before there is any content in #temp_container2 you won't be able to bind onto it directly at that point. Commented Feb 24, 2014 at 11:12
  • 1
    Since dom is updated... you will have to use on() method... check event delegation Commented Feb 24, 2014 at 11:13
  • learn.jquery.com/events/event-delegation Commented Feb 24, 2014 at 11:13

2 Answers 2

4

You need event delegation using on() as you are adding elements dynamically.

Demo

$('#temp_container2').on('click', 'img', function(){
   alert('works');
});

While adding elements in $('#temp_container2') you probably need to use $(this).attr('src') instead of $('#temp_container img').attr('src')

 $('#temp_container img').click(function(){
  var img = $('#temp_container img').attr('src');
  $('#temp_container2').append('<img src="'+img+'">');
});

Delegated events

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers, jQuery doc

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

4 Comments

+1 Was answering the same, but you did it already so just added a demo, so hope you don't mind :)
Thanks for the really helpful reply. I've been testing this and I noticed that if there is another div above the temp_container2, then the on() doesn't seem to work jsfiddle.net/H57xr
You have pasted same link the one in the answer, and probably had to give some other link?
Actually I managed to get it working by chaining div's together in the selector. Thanks for the great help! :)
0
$('#temp_container img').click(function(){

Should be

$('#temp_container').click(function(){ 

Or you can create a new div for the image itself, this makes it easier.

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.