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 to the CF scene
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replacing textbox text

    Hi, I am very new to html coding and would like some help. I am trying to replace text in a textbox with other text. So far I have got this, like I said I don't know much so anything will help. Thanks!
    _____________________________________________________________


    <html>
    <script>
    function Function1()
    {
    var str=document.getElementById("replace").innerHTML;
    var n=str.replace("google","bing");
    document.getElementById("replace").innerHTML=n;
    }
    </script>
    <center>
    <form>

    <input type="text" id="replace">
    <input type="submit" value="Submit" onclick="Function1">

    </form>
    </center>

    </html>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,178
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Textarea content, like any other form element, is just 'value' , not innerHTML.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,139
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    A few other errors which the infamous Doctor has not noticed.

    You ought not use the word replace as the name of an HTML element as it is a Javascript reserved word. And you may not give a variable a name which is a Javascript keyword or event such as alert, case, char, confirm, false, int, null, onload, return, this, void, window, and so on.

    onclick="Function1" is missing the brackets.

    The syntax of replace is incorrect. It requires a regular expression.

    You cannot use a submit button here as the form will submit to itself and simply refresh with the original value of the textbox displayed.

    The <center> tag is obsolete and deprecated.

    Your script should be positioned right in front of the closing </body> tag.


    Code:
    <html>
    <head>
    </head>
    <body>
    
    <form>
    <input type="text" id="rep" value = "google">
    <input type="button" value="Submit" onclick="Function1()">
    </form>
    
    <script type = "text/javascript">
    function Function1() {
    var str = document.getElementById("rep").value; 
    var newstr = str.replace(/google/gi,"bing");
    document.getElementById("rep").value = newstr;
    }
    </script>
    
    </body>
    </html>
    Quizmaster: Name an instrument which can be found hanging on the wall in many households.
    Contestant: A piano.
    Last edited by Philip M; 06-25-2013 at 05:27 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.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by Philip M View Post
    The syntax of replace is incorrect. It requires a regular expression.
    since when / in which browser?
    https://developer.mozilla.org/en-US/...String/replace

  • #5
    New to the CF scene
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help, that worked great. Sorry if my html was terrible just started using it so don't know much.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,139
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by xelawho View Post
    It is necessary to use a regex if you want the replace to be case-insensitive. Google as well as google.

    "The use of the flags parameter in the String.replace method is non-standard. Instead of using this parameter, use a RegExp object with the corresponding flags." I take it that "non-standard" means Mozilla only. It does not work in IE, so to me is useless.
    Last edited by Philip M; 06-26-2013 at 07:48 AM.

    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.


  •  

    Tags for this Thread

    Posting Permissions

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