+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Javascript Replace

  1. #1
    seaside is offline x10Hosting Member seaside is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    14

    Javascript Replace

    Ok so I am trying to make a textbox that you can write a sentence, and then hit a button. Then the text in the textbox will change certain words to certain other words.

    Ex: You write "Hello my name is John Smith." and hit the button. Then the text is changed to "Hi my name was John Smith."

    Something like that. So that I can choose words like "hello" and "is", and have them changed to "Hi" and "was".

    I have a starting point, but do not know how to put in the textbox, or how to match up words to replace.

    I know the replace() method exists, but how would I do this?

    Thanks.

  2. #2
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: Javascript Replace

    if you plan on using jQuery, you might want to check out this article

    http://www.nabble.com/jQuery-regex-r...727s27240.html

  3. #3
    seaside is offline x10Hosting Member seaside is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    14

    Re: Javascript Replace

    Thanks Diable, but could you write some code to show wat i should put in my page to get the example above. I didn't understand the atricle.

  4. #4
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: Javascript Replace

    you're going to have to pay me for that
    =)

    no im just kidding, let me write it up, and i'll post it when im done

  5. #5
    seaside is offline x10Hosting Member seaside is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    14

    Re: Javascript Replace

    Thanks sooo much. Il up ur rep! lol.

    Thanks.

    Please remember the textbox and button. Thanks

  6. #6
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: Javascript Replace

    HTML:
    <div id="text">Hello my name is John Smith</div><button onclick="tmod('text')">Click</button>

    Javascript:
    function tmod(id){
    var str = document.getElementById(id).innerHTML;
    str = str.replace("is","was");
    str = str.replace("Hello","Hi");
    document.getElementById(id).innerHTML = str;
    }

    Oh and for a text box, it'll be

    HTML:
    <input type='text' id="text" value="Hello my name is John Smith" /><button onclick="tmod('text')">Click</button>

    Javascript:
    function tmod(id){
    var str = document.getElementById(id).value;
    str = str.replace("is","was");
    str = str.replace("Hello","Hi");
    document.getElementById(id).value = str;
    }
    Last edited by scopey; 10-07-2009 at 03:32 PM.
    - When in doubt, refer to the PHP manual.

  7. #7
    seaside is offline x10Hosting Member seaside is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    14

    Re: Javascript Replace

    thank you. Can I have an E-mail for any later questions?
    Edit:
    Is there any way, also, to allow the user to have several input boxes that they can write which words are changed to which. Ex: I could write in one box "Dog" and "Cat" in the next. So people can change their words?

    Thnx again.
    Last edited by seaside; 10-07-2009 at 03:37 PM. Reason: Automerged Doublepost

  8. #8
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Javascript Replace

    Do mean that the user could enter their own list of replacement pairs?
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  9. #9
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: Javascript Replace

    I'm not quite sure what you mean sorry.

    If you want an interface where users can enter text, then choose what word to replace with what, you could do something like this:

    HTML:
    <input type='text' id='input1' /><input type='text' id='input1str' /><input type='text' id='input1rep' /><button onclick="tmod('input1')">Click</button>

    Javascript:
    function tmod(id){
    var str = document.getElementById(id).value;
    var strstr = document.getElementById(id+"str").value;
    var strrep = document.getElementById(id+"rep").value;
    str = str.replace(strstr,strrep);
    document.getElementById(id).value = str;
    }

    I hope that's what you meant.

    If you want users to be able to define multiple strings to replace, the following should work (I'm just writing and not actually testing, if the code doesn't work, give me a yell and I'll fix it):

    (Tell users to seperate the strings with commas)

    function tmod(id){
    var str = document.getElementById(id).value;
    var strstrs = document.getElementById(id+"str").value.split(",") ;
    var strreps = document.getElementById(id+"rep").value.split(",") ;
    var alen = strstrs.length;
    var i=0;
    for(i=0;i<alen;i++){
    if(strstrs[i] && strreps[i]){
    str = str.replace(strstrs[i],strreps[i]);
    }
    else{
    break;
    }
    }
    document.getElementById(id).value = str;
    }

    Edit: Forgot a semicolon....
    Last edited by scopey; 10-07-2009 at 04:15 PM.
    - When in doubt, refer to the PHP manual.

  10. #10
    seaside is offline x10Hosting Member seaside is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    14

    Re: Javascript Replace

    wow! Thanks! The first one is fantastic.

    One last thing if you could.

    How do I make the words that they can enter NOT case sensitive. In the first script i know about the /i, but in the newer one you wrote, how would I do this. Again, Thanks!

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  2. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM
  5. Some nice-simple JavaScript effects !!
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 07-13-2006, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers