1

I have the following markup: <div class="user-photo"> <img src="" /> </div>

I want to insert an image in that div when I click another image on a page. My code looks like this: $('body').on('click', '.image', function() { $('.user-photo').children('img').attr('src', $(this).attr('src')); });

But it is not working

1
  • How is it not working? Do you see an error? Commented Feb 29, 2012 at 14:22

3 Answers 3

2

demo: http://jsfiddle.net/WnC9B/8/

$('body').on('click', '.image', function(e) {
  $('.user-photo').children('img').attr('src', $(e.target).attr('src'));
 });​
Sign up to request clarification or add additional context in comments.

3 Comments

i can not toggle between clicking multiple images
when i click on the image it inserts it , but when i click on another image it doesn't insert that image, so i can not click multiple images and make the div image change
i am trying to build a photo gallery btw
0

I think you are using this in the wrong way (this refers to the bodyin this example, you should use e.target to refer to the clicked element)

$('body').on('click', '.image', function(e) { 
  $('.user-photo > img').attr('src', $(e.target).attr('src')); 
});

Comments

0

You can use the .tmpl() of jquery. It will be used to append html content in div in your case.

$.tmpl("<div class="user-photo"> <img src='' /> </div>")..appendTo("#targetdiv");

Another way is:

$('#div').html('<div class="user-photo"> <img src='' /> </div>')

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.