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 14 of 14
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Need help in function!

    Hello,

    I am writing code for my form which has 2 textbox One is labelled as "search" and the other one is "replace" and one textarea. If a user enters "hello how are you" in the textarea and user needs to change that hello to hi, he/she needs to enter "hello" in search textbox and "hi" in "replace" textbox. There is another button that says "replace now", if user clicks that button hello changes to hi.

    Can anyone give me some hints please. I am so confused how can I define such function?

    Thank you,

  • #2
    Regular Coder
    Join Date
    Jun 2008
    Location
    SE Wisconsin, US
    Posts
    222
    Thanks
    1
    Thanked 20 Times in 20 Posts
    I assume that you want to do this in JavaScript. You will need to be familiar with the following methods and concepts:

    - string.replace()
    - regular expression syntax
    - document.getElementById()
    - object.click()

    Here are some resources for finding the answers to these questions:

    w3schools.com
    http://msdn.microsoft.com/en-us/library/default.aspx (look under 1) Web Development -> HTML and CSS and 2) Web Development -> Scripting

    Hope that helps!
    Milwaukee Web Designer and SEO Milwaukee Firm specializing in ASP.Net, C#, VB.Net, SQL Server and Access.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Try this:-

    Code:
    <textarea id = "txtarea" rows = "12" cols = "40"></textarea><br><br>
    Search For <input type = "text" name = "search" id = "search" size = "20"><br>
    Replace By <input type = "text" name = "rep" id = "rep" size = "20"><br>
    <input type = "button" name = "but1" value = "Replace Now" onclick = "doIt()">
    
    
    <script type = "text/javascript">
    function doIt() {
    var txt = document.getElementById("txtarea").value;
    var str2rep = document.getElementById("search").value;
    var repstr = document.getElementById("rep").value;
    var len = txt.length;
    var a = txt.indexOf(str2rep);
    if (a < 0) {
    alert ("This sequence of characters was not found ");
    }
    else {
    var newStr = txt.substr(0,a);
    newStr = newStr + repstr; 
    var x = Number(a + str2rep.length);
    newStr = newStr + txt.substring(x,len);
    document.getElementById("txtarea").innerHTML=newStr;
    }
    }
    
    </script>
    Note that the search is case-sensitive. Hello and hello are not the same. If the same character sequence occurs in the textarea twice you can replace the second occurence by clicking on the button a second time. Or if you want one click to replace all the searched for character sequences at once ask me for a revised script.



    “In ancient times they had no statistics, so they had to fall back on lies” — Stephen B Leacock
    Last edited by Philip M; 01-25-2009 at 09:58 AM.

  • Users who have thanked Philip M for this post:

    newbee_coder (01-27-2009)

  • #4
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you so much Philip!
    I got an error when I run the code. It is saying...
    Error: document.getElementById("search") is null
    Source File: file:///E:/USB/*************

    I think the below line has a bug in it.

    var str2rep = document.getElementById("search").value;

    Thanks,

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Well, it works fine for me.

    Are you quite sure that you have copied it correctly?

  • Users who have thanked Philip M for this post:

    newbee_coder (01-27-2009)

  • #6
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    yes! I am sure!!

    Thanks,

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    As I say, it works fine for me and there is no error in the code.

    What, exactly, are you entering in the Search For box?
    And in the Replace By box?

  • Users who have thanked Philip M for this post:

    newbee_coder (01-27-2009)

  • #8
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    if you dont mind, can I PM you my entire code?

    Thanks,

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by newbee_coder View Post
    if you dont mind, can I PM you my entire code?

    Thanks,
    I seem to have got it whether I mind or not.

    The code as PM'd to me works perfectly for me in IE6. Are you using some ancient browser? Or using an inappropriate DOCTYPE? Otherwise I am right out of ideas.
    Also tested in Firefox and it works fine - there is absolutely no reason why it should not work.
    Last edited by Philip M; 01-27-2009 at 11:08 AM.

  • #10
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Try to change highlighted with value instead:
    Code:
    document.getElementById("txtarea").innerHTML=newStr;
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by rangana View Post
    Try to change highlighted with value instead:
    Code:
    document.getElementById("txtarea").innerHTML=newStr;
    No, that is not the problem OP says he gets error message at
    Error: document.getElementById("search") is null

    But you can change innerHTML to value if desired - it makes no difference. As I say, it works for me in IE and Firefox.

  • #12
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Hi Philip,

    Using innerHTML, I get the desired results on IE7, but not on FF 3.0.5.

    Using value, I get the desired results on both browsers.

    ...but none of them throws the error that the OP said:
    Error: document.getElementById("search") is null

    Reading that, I suppose we both assumed that there's no element having the id of search (yet).

    Anyway, you might want to share to us the OP's code as passed via PM.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    [QUOTE=rangana;774766]Hi Philip,

    Using innerHTML, I get the desired results on IE7, but not on FF 3.0.5.

    Using value, I get the desired results on both browsers.

    [QUOTE]

    Hmm. Works for me with innerHTML in both IE6 and Firefox 3.0.5 But I do agree that value is better.

    Code is as supplied in Post#3
    Last edited by Philip M; 01-27-2009 at 01:25 PM.

  • #14
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Let's hear it from the OP, whether changing to value have make/break the script.

    I've tried a lower version, FF 3.0.3, and still failing at my end using innerHTML.

    Anyway, let's just hear it from the OP.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

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