ktm
ktm

Reputation: 6085

How to copy text from one div to another in javascript

I have div1 and div2, upon clicking copy button I want the text in div1 copied to div2, how is it possible in javascript, pls help.

Upvotes: 3

Views: 9218

Answers (4)

Amit Enjoying
Amit Enjoying

Reputation: 1

<html>
  <head>
  <script>
     function change() {
       document.getElementById('demo1').innerHTML = document.getElementById('demo').innerHTML;
     }
  </script>
  </head>

  <body>
    <div class="left">
      <input type="text" id="demo" value="hi" />
      <input type="button" onClick="change()" />
    </div>
    <div class="right">
      <p id="demo1"></p>
    </div>
  </body>
</html>

Upvotes: -1

Nathan Taylor
Nathan Taylor

Reputation: 24606

Using jQuery:

$("#div2").html($("#div1").html());

You can trigger this with a click() handler on the button:

$("#copy").click(function() {
    $("#div2").html($("#div1").html());
});

Upvotes: 5

jigfox
jigfox

Reputation: 18177

<div id="div1">Some Content</div>
<div id="div2"></div>
<input type="button" onclick="document.getElementById('div2').innerHTML=document.getElementById('div1').innerHTML"/>

Upvotes: 5

TheBrain
TheBrain

Reputation: 5608

<input type="button" onclick="document.getElementById('div2').innerHTML = document.getElementById('div1').innerHTML" value="Copy" />

Upvotes: 4

Related Questions