Copy text in javascript

1
<td> 
    <div id="Ei" onclick="getValue()">Click me!</div> 
</td>
<td>
    <script type="text/javascript"> 
        ShowLMCButton(document.getElementById("Ei"));  
    </script>
</td>

I want to make the user copy the entire element of a div, but it is giving the error: "[object HTMLDivElement]" ;

Any help?

    
asked by anonymous 03.03.2015 / 14:48

1 answer

0

Create two variables to store the content:

var firstDivContent = document.getElementById('mydiv1');
var secondDivContent = document.getElementById('mydiv2');

Now just set the value in the second div:

 secondDivContent.innerHTML = firstDivContent.innerHTML;

Complete code:

<html>
<head>
<script type="text/javascript">
function copyDiv(){
  var firstDivContent = document.getElementById('mydiv1');
  var secondDivContent = document.getElementById('mydiv2');
  secondDivContent.innerHTML = firstDivContent.innerHTML;
}
</script>
</head>
<body onload="copyDiv();">
<div id="mydiv1">
  <div id="div1">
  </div>
  <div id="div2">
  </div>
 </div>

 <div id="mydiv2">
 </div>
 </body>
 </html>

Demonstration link

Source: link

    
03.03.2015 / 14:52