View Full Version : Can't write to a textarea

07-18-2011, 10:22 AM

Nothing will display in my textarea after pressing an input number which have to seen on the display. The code is from a simple calculator

<form action="#" method="get" onsubmit="return false">
<textarea disabled="disabled"></textarea>
<div id="calculatorButtons">
<button onclick="calculatorNumber(1)">1</button>
<button onclick="calculatorNumber(2)">2</button>
<button onclick="calculatorNumber(3)">3</button>
<button onclick="calculatorOp('+')">+</button>
<button onclick="calculatorNumber(4)">4</button>
<button onclick="calculatorNumber(5)">5</button>
<button onclick="calculatorNumber(6)">6</button>
<button onclick="calculatorOp('-')">-</button>
<button onclick="calculatorNumber(7)">7</button>
<button onclick="calculatorNumber(8)">8</button>
<button onclick="calculatorNumber(9)">9</button>
<button onclick="calculatorOp('*')">*</button>
<button onclick="calculatorNumber(0)">0</button>
<button onclick="calculatorOp(',')">,</button>
<button onclick="calculatorClear()">Clear</button>
<button onclick="calculatorCalculate()">Calc</button>

function calculatorNumber(i) {

document.form.getElementById('textarea').innerHTML = i;


What is the right way to refer to the textarea?

07-18-2011, 11:08 AM
What is the right way to refer to the textarea?You need to add an ID attribute to your textarea element. For example:

<textarea id="myTextAreaElement" disabled="disabled"></textarea>

Then you can reference the textarea element with document.getElementById("myTextAreaElement"). (Make sure to drop the form. part of the statement in your current code.)

To set the value of the element, you can use the code you're using now. However, I prefer a more specific approach:

function calculatorNumber(myNumber) {
document.getElementById("myTextAreaElement").value = myNumber.toString();

Lastly, if you don't intend to have the form submit data to a server (as indicated by the action="#" attribute and your cancellation of the submission event), you may as well drop the form element to save yourself some code.