user1060187
user1060187

Reputation: 977

javascript get value of paragraph to appear in textarea

I'm trying to add the value of a paragraph to a text area using javascript. This is my code at the moment. I can't quite seem to get it to work. Any help is appreciated

  <html>
  <head>
   </head>
  <body>
  <button value="this is a length definition" onclick="reveal(this.value)"> hi</button>
  <script type="text/javascript">
  function reveal(value)
  {
  var text = value;
  document.outputtext.value += value;
  }
  </script>
  <table>
  <tr>
  <td><textarea name="outputtext"></textarea></td>
  </tr></table>
  </body>
  </html>

Upvotes: 0

Views: 2078

Answers (4)

dearnotmine
dearnotmine

Reputation: 99

<html>
<head>
</head>
<body>
<button value="this is a length definition" onclick="reveal(this.value)"> hi</button>
<script type="text/javascript">
function reveal(value)
{
  var text = value;
  document.getElementById("outputtext").value += text;
}
</script>
<table>
  <tr>
    <td><textarea id="outputtext"></textarea></td>
  </tr></table>
</body>
</html>

Upvotes: 1

mplungjan
mplungjan

Reputation: 177851

Your textarea is not part of the document. Also your button value is not a paragraph.

  • Give it an ID and use document.getElementById("outputtext").value (recommended) or
  • wrap in a form and do document.formName.outputtext.value or
  • use the clumsy document.getElementsByName("outputtext")[0].value

Here is the code using ID

DEMO

  <html>
  <head>
   </head>
  <body>
  <button value="this is a length definition" onclick="reveal(this.value)"> hi</button>
  <script type="text/javascript">
  function reveal(value) {
    document.getElementById("outputtext").value += value;
  }
  </script>
  <table>
  <tr>
  <td><textarea id="outputtext"></textarea></td>
  </tr></table>
  </body>
  </html>

Upvotes: 2

Mark Yao
Mark Yao

Reputation: 408

Code change to:

 <html>
 <head>
 </head>
<body>
<button value="this is a length definition" onclick="reveal(this.value);"> hi</button>
<script type="text/javascript">
function reveal(value)
     document.getElementById("outputtext").value += value;
 }
</script>
<table>
  <tr>
    <td><textarea id="outputtext"></textarea></td>
  </tr></table>
</body>
</html>

Upvotes: 0

Nhu Trinh
Nhu Trinh

Reputation: 13956

shoud change to

<textarea id="outputtext"></textarea>

then code change to

document.getElementById('outputtext')

good luck

You can use name but you have to call getElemntsByName() and receive an array of element

Upvotes: 1

Related Questions