Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't write to a textarea

    Hi,

    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">
    <p>
    <textarea disabled="disabled"></textarea>
    </p>
    <div id="calculatorButtons">
    <p>
    <button onclick="calculatorNumber(1)">1</button>
    <button onclick="calculatorNumber(2)">2</button>
    <button onclick="calculatorNumber(3)">3</button>
    <button onclick="calculatorOp('+')">+</button>
    </p>
    <p>
    <button onclick="calculatorNumber(4)">4</button>
    <button onclick="calculatorNumber(5)">5</button>
    <button onclick="calculatorNumber(6)">6</button>
    <button onclick="calculatorOp('-')">-</button>
    </p>
    <p>
    <button onclick="calculatorNumber(7)">7</button>
    <button onclick="calculatorNumber(8)">8</button>
    <button onclick="calculatorNumber(9)">9</button>
    <button onclick="calculatorOp('*')">*</button>
    </p>
    <p>
    <button onclick="calculatorNumber(0)">0</button>
    <button onclick="calculatorOp(',')">,</button>
    <button onclick="calculatorClear()">Clear</button>
    <button onclick="calculatorCalculate()">Calc</button>
    </p>
    </div>
    </form>

    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
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 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:

    Code:
    <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:

    Code:
    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
    •