Javascript text editor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bharathiraja
    New Member
    • Sep 2007
    • 2

    Javascript text editor

    Hello All,

    I am developing an Editor similar to the Notepad with "Find & Replace" , "ReplaceAll " functionality.

    In an HTML form I am having 3 text fields with an Editor button, when I click the Editor button it opens a pop up window with "Replace" and "ReplaceAll " buttons where I am editing the form text fields and returning back to the form.

    The issue i am facing is that I am not able to do "Replace" functionality because everytime it is taking the complete string and not taking the string from where replace is done.

    For ex,

    if 111 is the string

    then when i click "Replace" button I have to replace 1 with 12 but what is happening is like this

    1 st time -- 1211
    2nd time -- 12211
    3r dtime -- 122211

    It is not taking like this

    1 st time -- 1211
    2nd time -- 12121
    3r dtime -- 121212

    Below is the script i am using

    -------------------------------------------------------------------------------------------------------------------


    [CODE=javascript]// Replaces all instances of the given substring.
    String.prototyp e.replaceAll1 = function(strTar get, strSubString )
    {

    var strText = this;
    if(strTarget == ""||strSubStrin g=="")
    {
    alert("Please Enter any value to replace");
    return strText;
    }
    else
    {
    var strText = this;
    var intIndexOfMatch = strText.indexOf ( strTarget );


    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatc h != -1 || intIndexOfMatch !=0 ){

    // Relace out the current instance.
    strText = strText.replace ( strTarget, strSubString )
    // Get the index of any next matching substring.
    return( strText );
    intIndexOfMatch = strText.indexOf ( strTarget );
    }
    }
    }[/CODE]--------------------------------------------------------------------------------------------------------------

    If i use bodytextrange function it works fine in IE but fails in Firefox,

    Can you please point me to an article or website where i can create a script with browser compatble.

    Please help me out with this script.

    Thank you,
    Last edited by acoder; Sep 5 '07, 11:19 AM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    Please use CODE tags when posting code. Thanks!

    Use regular expressions and the replace method - see link.

    Comment

    • Bharathiraja
      New Member
      • Sep 2007
      • 2

      #3
      Hello Acoder,

      Thank you for your reply.

      I have reviewed the link which you have sent. From the link I am able to search and replace all the string occurrences in a text area i.e. I can do "ReplaceAll " function using RegExp.

      But In my code I just wanted the "Replace" function just like the Notepad function where I can search and replace the first matched string in a text area, then the second string, then the 3rd etc... and finally if no matched string is found then I can put an alert like "No Matched Strings found".

      Could you please enlighten me in this regard. Also if you can point to some sample code it will be very helpful for me.

      Thank you

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        In that case, keep tabs on the string position and only replace in the string after the last replaced position. Remember it should be at the end of the matched position, not the beginning.

        Comment

        Working...