Method copy text in javascript

Asked

Viewed 865 times

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 to copy the whole element of a div, but is giving the error: "[object HTMLDivElement]";

Some help?

1 answer

0


Create two variables to store 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 http://jsfiddle.net/GCn8j/

Source: https://stackoverflow.com/questions/20173101/copy-the-content-of-a-div-into-another-div

  • That’s :D, what I wanted was this innerHTML. Very obg.

  • I’m glad you decided :) has how to mark the answer as right? :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.