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 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    replace str in text area

    Hi im trying to replace some text in a text area however it cant seems to pick up the text from inside the textarea. any help is appreciated

    <html>
    <body>

    <p>Click button:</p>

    <textarea id="zebra">Type here!</textarea>

    <button onclick="myFunction()">Click here</button>

    <script>
    function myFunction()
    {
    var str=document.getElementById("zebra").innerHTML;
    var n=str.replace("zebra","horse");
    document.getElementById("zebra").innerHTML=n;
    }
    </script>

    </body>
    </html>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Change .innerHTML to .value
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    abcabc (09-07-2012)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thank you it works !

  • #4
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    however the str zebra might be repeated many times throughout the text. Is there anyway to have it replace all with a click instead of having the user to press the button multiple times ? thank you in advance

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by abcabc View Post
    however the str zebra might be repeated many times throughout the text. Is there anyway to have it replace all with a click instead of having the user to press the button multiple times ? thank you in advance
    Code:
    var n=str.replace(/zebra/gi,"horse");

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    the fastest way i know uses strings to do universal replacements:
    Code:
    var n=str.split("zebra").join("horse");
    probably doesn't matter for littlethings, but on big loops or when making lots of replacements, it's noticeably faster than regexp...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Of course
    var n=str.split("zebra").join("horse");
    won't work neither will
    var n=str.replace(/zebra/gi,"horse");
    IF you do not want fazebrain to
    become fahorsein.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    He wanted all instances of zebra replaced by horse.

    But if only whole words are wanted:-

    Code:
    var n=str.replace(/\bzebra\b/gi,"horse");
    n = n.replace(/\bmouse\b/gi,"cat");
    n = n.replace(/\bmice\b/gi,"cats");
    Last edited by Philip M; 09-07-2012 at 03:32 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    abcabc (09-08-2012)

  • #9
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you everyone for their input. I managed to get what I wanted.

  • #10
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Is it possible for the code

    var n=str.replace(/\bII\b/gi,"\n");
    n = n.replace(/\bmouse\b/gi,"cat");

    to work with a symbol specifically the 2 brackets. ))

    thank you again

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You need to escape (with a backslash) the following META characters in a regex pettern so thay are interpreted as literals and not control characters:-
    ^ $ \ / ( ) | ? + * [ ] { } . (but not , )

    So:-


    Code:
    <script type = "text/javascript">
    
    var str = "abc))xyz))";
    str = str.replace(/\)/g,"something")
    alert (str);
    
    </script>
    Last edited by Philip M; 09-08-2012 at 06:29 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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