hsming hsming - 2 years ago 839
Javascript Question

Why did I get [object HTMLParagraphElement]

I am a newbie. Here is my code:

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

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

Answer Source

It should be:

document.getElementById("comment_content").value =

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download