Link to home
Start Free TrialLog in
Avatar of ioustinos_sarris
ioustinos_sarrisFlag for Greece

asked on

javascript clone object

Hi there, i am looking for a way to clone objects. I have found variations of clonig methods but all seem to be questioned by others...Could anyone suggest a sure and stable clone method?
Note that i need DEEP cloning...

Moreover, if i want to simplify things and know beforehand which objects i need to be cloning, can't i just create a property function in these objects? what would that be?

thanks
Avatar of HonorGod
HonorGod
Flag of United States of America image

Are you talking about document elements?

http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/cloneNode.htm

There is a cloneNode method...
Can you please explain what you mean by "DEEP cloning"


Here is a example which can clone entire table

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<script language="javascript">
function UpdateTable (tableId, divId) {
var tObj = document.getElementById (tableId);
var dObj = document.getElementById (divId);
var nObj = tObj.cloneNode (true);
dObj.appendChild (nObj);
tObj.parentNode.removeChild (tObj);
alert (dObj.outerHTML);
}
</script>

<table id="mytable"><tr><td>test</td></tr></table>
<div id="mydiv"></div>
<input type="button" value="Insert Table in Div" onClick="UpdateTable ('mytable'
, 'mydiv')">

</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was referring to second part of the  question

>> Moreover, if i want to simplify things and know beforehand which objects i need to be cloning, can't i just create a property function in these objects?

In object being cloned, he wants to ignore certain child objects ??

 
parvinasar:  Please accept my apologies.  I didn't notice that it was you who had made the update.

>> Moreover, if i want to simplify things and know beforehand which objects i need to be cloning, can't i just create a property function in these objects?

To my knowledge, no such property, or mechanism exists.
Thanks for the grade & points.

Good luck & have a great day.