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 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2003
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Inserting text into a form by pressing a button...

    Hey,

    I am about to kill myself trying to figure this script out.
    How do I make it where if i press a button on a page,
    it will insert text into a text area?



    Here are a few things i have tried in the past but did not worK:

    This one is where I tried to insert a copyright logo into the textarea named "zone"

    <html>
    <head>
    <script language="javascript">
    function="copyright()"
    document.zone.area.value =
    document.zone.area.value "&copy";
    </script>

    </head>
    <body>
    <textarea name="zone" rows=7 cols=46></textarea>
    <input type="button" value="Copyright" onclick="copyright()">&nbsp
    </body>
    </html>

    This one is where i tried to make a new object form the tutorial on javascriptkit.com. Im trying to insert the text "hello" into the textarea named "zone".

    <html>
    <head>
    <script>
    function userobject(){
    }
    //myobject is now an object of type userobject!
    var myobject=new userobject("hello")
    </script>
    </head>
    <body>
    <textarea name="zone" rows=7 cols=46></textarea>
    <input type="button" value="Hello" onclick="myobject()">
    </body>
    </html>

    Please help me out with this script. I have spent hours and hours
    attemting to get this right. I know its simple, but I dont have too much Javascript experience, I like to stick with HTML.

    Any response would be appreciated.

    Carl

  • #2
    New Coder
    Join Date
    Oct 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alirght, I like to explain things before I give out the answers, kind of helps me think them through in my head first, so if you don't want to hear it, just skip down to the code. First of all, a textarea is a form element, so in order to manipulate it without using an "id" you must first establish a form of some type. Secondly, you cannot add a "value" to a textarea, only take one from it since the "value" is the HTML in between the brackets. I have included two different ways to do this, one with the "id" way. You can see it is the "innerHTML" property that does it.

    <html>
    <head>
    <script language="javascript">
    function copyright(){
    document.form.zone.innerHTML="©";}
    </script>

    </head>
    <body>
    <form name="form">
    <textarea name="zone" rows=7 cols=46></textarea><br>
    <input type="button" value="Copyright" onclick="copyright()">
    </form>
    </body>
    </html>


    <html>
    <head>
    <script language="javascript">
    function copyright(){
    document.getElementById("zone").innerHTML="©";}
    </script>

    </head>
    <body>
    <form name="form">
    <textarea id="zone" rows=7 cols=46></textarea><br>
    <input type="button" value="Copyright" onclick="copyright()">
    </form>
    </body>
    </html>

    WAIT!! Just to let you know, this code will replace everything in the textarea with the copyright symbol. If you just want to add it to the end of what you already have add a "+" sign before the ="©"

    Hope this helps!
    Last edited by xupaosso; 11-09-2003 at 08:59 AM.
    Help! I've reached the end of my code!

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The innerHTML property won't work. It's not a valid property for TextArea elements, since they don't actually render HTML.

    Use the value property instead.

    There is an innerText property, but that is IE-only, so value is still the best choice.

    The problem you're really having is with your reference. Element names aren't exposed onto the document object -- but form names/ids are. Since you don't even have a form tag, this type of referencing won't work. You either need to use an ID like xupaosso suggested, or add a form and reference it correctly.
    Code:
    <html>
    <head>
    <script language="javascript">
    function copyright()
    {
        document.form1.zone.value += "&copy;";
    }
    </script> 
    
    </head>
    <body>
    <form name="form1">
    <textarea name="zone" rows="7" cols="46"></textarea>
    <input type="button" value="Copyright" onclick="copyright()"> 
    </form>
    </body>
    </html>
    There are many other ways to more efficiently handle this reference, but I'll leave that exercise for you or for another day.
    Last edited by beetle; 11-09-2003 at 11:41 AM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    New Coder
    Join Date
    Oct 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    beetle, I tried the value myself, and it didn't work, but the innerHTML did...is my computer on crack?
    Help! I've reached the end of my code!

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Must be. I'm not surprised that IE would allow using innerHTML on a textarea. It's not a valid property, but the browser most likely aliases it to the value property. TextArea elements have no innerHTML -- it's quite simply becuase they don't render HTML.

    Why the value property didn't work for you? Something else must have been amiss.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #6
    New Coder
    Join Date
    Nov 2003
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Post

    Thanks for all of your help!

    I especially liked beetle's way of doing it because you can enter the
    symbol more than once.

    With the other ones, you can only enter it once.


  •  

    Posting Permissions

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