tags:

views:

228

answers:

1

Hi All,

Forgive me as this is probably an easy question for your jQuery guys out there, but I have the following:

  1. Unordered list with images as list items
  2. Empty div box with dotted border to show that it's empty

I want the user to be able to click on a list item and have the empty div fill with it's image.

Thanks in advance

+3  A: 

You can write something like this:

$('ul li:has(img)').click(function() {
    $('#divBox').append($('img', this).clone()); 
});

EDIT: Fixed first version.

Or, alternatively,

$('ul li:has(img)').click(function() {
    $('#divBox #imageElem').attr('src', $('img', this).attr('src'));
});
SLaks
Very impressive and great solution, but how to keep the image in the list? And also SET the image in the DIV and not append it. If I use your solution it keeps adding the images to the div while removing them from the unordered list.
st4ck0v3rfl0w
Try my alternative.
SLaks
I tried and I can't get the alternative to work quite yet (I even changed your "atr" to "attr") I'm confident it's my stupid error and not your code...
st4ck0v3rfl0w
You need to change the selector (`'#divBox #imageElem'`) to match your HTML.
SLaks
duh, got it to work, thanks SLaks
st4ck0v3rfl0w
@st4ck0v3rfl0w, try `$('#divBox').append($('<img />').attr('src', $('img', this).attr('src')));`Posting as a comment rather than an answer because I think this is what @SLaks meant (correct me if I'm wrong).
eyelidlessness
@eyelidlessness: You are slightly wrong; see my edit.
SLaks
Er. Yeah. It just seemed there was a mystery `#imageElem` that was never specified before heh.
eyelidlessness