Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Thanked 0 Times in 0 Posts

    Can't write to a textarea


    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?

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Splendora, Texas, United States of America
    Thanked 293 Times in 287 Posts
    Quote Originally Posted by sanla View Post
    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.
    For every complex problem, there is an answer that is clear, simple, and wrong.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts