Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery cloned element being altered in variable

EDIT:

Working example now at jsbin.com/ivukar/10


Here is what I'm trying to do, summed up into the core steps without all the detail that would be fairly meaningless to you:

  1. Clone an existing div from the DOM and store that clone in a variable
  2. Remove that div from the DOM
  3. Append the cloned div into the DOM
  4. Make a change to the HTML content of the div in the DOM
  5. Remove the div and insert the clone again

Now following these steps, let's say the HTML content of our div was "test", I would expect the following:

  1. Variable to have the div stored with the content "test"
  2. Div removed from DOM
  3. Div append to DOM with content "test"
  4. Div on the page altered to have content "altered"
  5. Div on the page removed. Div appended to the body again with the content "test" since that is stored in a variable and should not be affected by a change to the DOM

And yet what happens is that as soon as I make that change to the html content of the element, using for example: $('#element').html('altered'); It changes the content of the variable as well...

I cannot see why it would do that, as the variable is only ever referenced when appending it to the DOM, I am not changing the content of the variable whatsoever...

Here is a JsBin link so you can see what I mean.

Or alternatively here is the example code:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
var saved = '';

function my_clone()
{
saved = $('#el').clone();
$('#output').append("<a href='#' onclick='my_remove();'>Remove version on screen</a> ----- This removes any element with the id 'el'<br>");
}

function my_remove()
{

$('#el').remove();
$('#output').append("<a href='#' onclick='my_append();'>Append clone to body</a> ----- This takes the cloned object stored in the variable 'saved' and appends it to the html body<br>");

}

function my_append()
{

$('body').append( saved );
$('#output').append("<a href='#' onclick='my_alter();'>Alter .html() of element</a>----- This alters the html of any element with the id 'el'<br>");

}

function my_alter()
{

$('#el').html('altered');
$('#output').append("<a href='#' onclick='my_remove_again();'>Remove version on screen again</a>----- This removes any element with the id 'el'<br>");

}

function my_remove_again()
{
$('#el').remove();
$('#output').append("<a href='#' onclick='my_append_again();'>Append clone to body</a> ----- This again takes the object stored in the 'saved' variable, which is separate from the DOM and should not have been affected by the html change and appends to the html body<br>");
} 

function my_append_again()
{
$('body').append( saved );
}


</script>
<style>
#el {color:red;}
</style>
</head>
<body>

<div id="el">
    <div id="various">Various</div>
    <div id="sub">Sub
        <div id="and-sub-sub">And Sub-Sub</div>
    </div>
    <div id="elements">Elements</div>
</div>

<br><br>
<div id="output">
<a href="#" onclick="my_clone();">Clone</a> ------ This stores the clone into a global variable called 'saved'<br>
</div>

</body>
</html>

Could anyone tell me where I am going wrong here?

Thanks.

like image 321
CMR Avatar asked Apr 23 '13 14:04

CMR


1 Answers

The problem was that you were assigning the actual DOM element to saved rather than the HTML content.

An old trick:

saved = $("#el").clone().wrap('<div/>').parent().html();

You first wrap the clone in a parent div whose HTML you return.

Updated JSBIN http://jsbin.com/ivukar/4

Reference: Get DOM element as string

like image 56
Jay Na Avatar answered Oct 15 '22 18:10

Jay Na