Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did I get [object HTMLParagraphElement]

Tags:

javascript

I am a newbie. Here is my code:

<html>
  <head>
    <script type="text/javascript">
      function replyOne () {
        document.getElementById("comment_content").value = document.getElementById("username")
      }
    </script>
  </head>
  <body>
    <p id="username">Jack</p>
    <textarea id="comment_content" ></textarea>
    <button onclick="replyOne()">Copy Text</button>
  </body>
</html>

I expect that when I click the button, it will copy 'Jack' to the textarea. But instead it just writes '[object HTMLParagraphElement]'.

like image 718
hsming Avatar asked Apr 22 '13 12:04

hsming


1 Answers

It should be:

document.getElementById("comment_content").value =
    document.getElementById("username").innerHTML

Without the .innerHTML, it will try to copy in the actual element, not its content.

like image 55
tckmn Avatar answered Oct 20 '22 00:10

tckmn