tags:

views:

48

answers:

2

i want to copy a content from one div when clicked and write it to other div:

But when doing this i lost the clicked content from first div:

$(img).click(function(){
  var newdiv=this;
  $(".second").html(newdiv);
});

i have two divs with class first and second. frist div having images.

why i lost the first div's content or the click element, ie "this"

pls give me a solution

+4  A: 

I think you want:

$(".first").click(function(){

var newdiv=$(this).html();

$(".second").html(newdiv);

})

Edit (thanks gnarf!)

Patrick Karcher
`$(this).html()` not `this.html()`
gnarf
If you want to copy the "outer" html (i.e. the selected element is a `<img>` which wont return anything for .html(): `var html = $("<div/>").append($(this).clone()).html();` to get the "outerHTML"
gnarf
From the text of his question I guessed that he want this all to happen with the ".first" div was clicked; hence, the `$(img)` I considered a mistype. If he wants this to happen when an <img> tag *within* the div is clicked, then he'd have to do it a little differently, yes. Your guess is as good as mine.
Patrick Karcher
@Patrick - nod - was just giving an example of getting "outer" html - this question is a pretty hard one to understand :)
gnarf
+2  A: 

I'm assuming that you want to put any .first img inside of .second when it is clicked on:

$('.first img').click(function() {
  var $this = $(this);
  $('.second').empty().append($this.clone());
});
gnarf