Javascript DOM How to - Append cloned element








Question

We would like to know how to append cloned element.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.4.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from   w w  w  .j  a  v  a  2  s  .  c  o m-->
    console.log($('<div>').append( $("span").clone() ).html());
});
</script>
</head>
<body>
  <span id='kuku' class='lala bubu' value='xyz'>some text</span>
  <p>Stuff in the middle</p>
  <span id='kuku' class='lala bubu' value='xyz'>other</span>
</body>
</html>

The code above is rendered as follows: