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,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 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,444
    Thanks
    11
    Thanked 598 Times in 578 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 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 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
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 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 04: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
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 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 07: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
    •