473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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


Expand|Select|Wrap|Line Numbers
  1. // Replaces all instances of the given substring.
  2. String.prototype.replaceAll1 = function(strTarget, strSubString )
  3. {
  4.  
  5. var strText = this;
  6. if(strTarget == ""||strSubString=="")
  7. {
  8.     alert("Please Enter any value to replace");
  9.     return strText;
  10. }
  11. else
  12. {
  13. var strText = this;
  14. var intIndexOfMatch = strText.indexOf( strTarget );
  15.  
  16.  
  17. // Keep looping while an instance of the target string
  18. // still exists in the string.
  19. while (intIndexOfMatch != -1 || intIndexOfMatch !=0 ){
  20.  
  21. // Relace out the current instance.
  22. strText = strText.replace( strTarget, strSubString )
  23. // Get the index of any next matching substring.
  24. return( strText );
  25. intIndexOfMatch = strText.indexOf( strTarget );
  26. }
  27. }
  28. }
--------------------------------------------------------------------------------------------------------------

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,
Sep 5 '07 #1
3 1703
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Please use CODE tags when posting code. Thanks!

Use regular expressions and the replace method - see link.
Sep 5 '07 #2
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
Sep 5 '07 #3
acoder
16,027 Expert Mod 8TB
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.
Sep 5 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sasha Shevelev | last post by:
Hi there, I can;t believe no one else has had this prob but here goes... I add some javascript into my page and asp.net sometimes (not always) puts the js into my page with a line break...
7
by: Matthew | last post by:
I am very new to JavaScript, and have been using Notepad. Is there a editor out there that showes the JavaScript environment variables? Example: I just found out about the document.forms array...
3
by: Java script Dude | last post by:
I have still yet to see a JavaScript Editor that comes close to reading a good JS book, learing it and using it with a text editor. Anyway, here my recipe for build successfull DHTML...
1
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
1
by: Jorge Ponte | last post by:
hi I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In that WUC I want have a textbox and a button. I want to click on the button and open a popup (I use javascript for...
10
by: madtom1999 | last post by:
I've been playing with some javascript code and have produced a very rough proof of concept source code editor written in javascript (a bit like FCKeditor). Its not ready for release ((L)GPL>2 when...
1
by: carlg | last post by:
There seem to be plenty of Javascript implemented WYSIWYG editors for editing rich text in a web browser, but I haven't been able to find one that shows line numbers. Even if that was the only...
1
by: sarwarmohiuddin | last post by:
Hello MAtes, I am having this problem of adding a script element in a rich text editor box. i am doing this. The problem is that it won't add the script tag alltogather, or give an error "Unknown...
36
by: karen987 | last post by:
My newsweblog has ASP pages with news articles and a commenting system. The comments are posted, and to read them the reader clicks on the headline of the comment, and it opens up in a pop up window....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.