i am trying to replace the content of a div with the content of another div(which is hidden). The code works for the first time only but the second time doesn’t work at all.
I have a div where the titles of some articles are scrolling. I want to achieve that:everytime i am going to click the title of an article its content(the content is in hidden div) is going to appear in another div with the id=”sreen”.
<script type="text/javascript">
//everytime you click the title of an article
$("a.title").live("click", function(){
//replace the content of div screen with the content of an article
$('div#screen').replaceWith($(this).next('div.content'));
});
</script>
Any ideas???
Using
.replaceWithwill effectively removediv#screen. So using.html()will be what you want to do to maintain the elementdiv#screen.You mentioned that your formating is not working correctly which leads me to believe you have css classes on
div.content. Calling.html()ondiv.contentwill ommit the root node of div.content.$("div.content").html()will produce<span>some text</span>If my assumptions are correct you might want to look at using
clone()which will clone the current object without events orclone(true)to include any data and events.Another way of doing this would be use .outerHTML
Example on jsfiddle.