Results 1 to 10 of 10

Thread: How to append text in a textarea without replacing the original text??

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to append text in a textarea without replacing the original text??

    Can anyone tell me how do I append text in a textarea element without replacing the original data??
    What I mean is if I have this text in a textarea:

    Code:
    Test1
    Test2
    Now how do I append text which may end up like this:

    Code:
    Test1
    Test2
    Test3
    The traditional formname.formelement.value isn't working for me it just replaces the whole thing inside the element. Any help would be greatly appreciated.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    <html>
    <head>
    <script>
    function addtxt(input) {
    var obj=document.getElementById(input)
    var txt=document.createTextNode("blah blah")
    obj.appendChild(txt)
    }
    </script>
    </head>
    <body>
    <textarea id="textarea1"></textarea>
    <br><input type="button" value="Write blah blah" onclick="addtxt('textarea1')">
    </body>
    </html>
    Last edited by mburt; 08-11-2006 at 06:08 PM.
    - Mike

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, worked great.

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You could also do this:

    Code:
    <html>
    <head>
    <script>
    function addtxt(input) {
    var obj=document.getElementById(input)
    obj.value+="blah test 123"
    }
    </script>
    </head>
    <body>
    <textarea id="textarea1"></textarea>
    <br><input type="button" value="Write blah blah" onclick="addtxt('textarea1')">
    </body>
    </html>
    - Mike

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah yes!! Now I remember I know the second technique but never used it. Thanks anyways.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    .value is the better technique here unless you've got one heck of a lot of text (1MB or more, probably).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I know I need to post this as a new thread but I am too lazy to do that. So, can anyone tell me how do I make an event fire when the user presses enter from a form ?? I have a basic form which does some ajax request something like this:

    Code:
    <form name="chatform">
    <textarea name="chatresp" cols="50" rows="20" id="showresults" readonly="readonly"></textarea><br>
    <input type="text" name="text" id="text"><input type="button" value="Send" onclick="send();">
    </form>
    Now I need it to execute send(); even when the user simply hits enter and doesn't click the "Send" button. Thanks.

    EDIT: Forgot to mention can anyone also tell me how do I scroll to the bottom of a textarea which gets dynamically updated??

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You have to make the button a submit button and use onsubmit on the form.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I can't use submit button in a ajax form can I?? If I do then the form will be submitted(meaning the page will reload).

  10. #10
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Never mind, found out the solution. I used return false.

Bookmarks

Posting Permissions

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