PDA

View Full Version : replace string textarea Firefox


ijyoung
02-23-2006, 02:31 PM
This is probably an old chestnut but cannot find anything on net.

I have a script to format copy in a textarea which allows line breaks, etc to be translated into html when "onchange" activated. Works fine in Opera and IE but of course doesn't work in FF.

Code as follows:

function replace(string,text,by) {
// Replaces text with by in string
var strLength = string.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return string;

var i = string.indexOf(text);
if ((!i) && (text != string.substring(0,txtLength))) return string;
if (i == -1) return string;

var newstr = string.substring(0,i) + by;

if (i+txtLength < strLength)
newstr += replace(string.substring(i+txtLength,strLength),text,by);

return newstr;
}

function replace1(string) {
// Calls replace() many times to step through all the text changes we need

var a = "\r";
var b = "<br />";
var replaced=replace(string,a,b);
return replaced;

var a = "\n";
var b = "<br />";
var replaced=replace(string,a,b);
return replaced;

var a = "•";
var b = "·";
var replaced=replace(string,a,b);

var a = "*";
var b = "·";
var replaced=replace(string,a,b);

var a = "&";
var b = "&amp;";
var replaced=replace(string,a,b);

var a = "-";
var b = "-";
var replaced=replace(string,a,b);

var a = "-";
var b = "-";
var replaced=replace(string,a,b);

var a = "§";
var b = "·";
var replaced=replace(string,a,b);

var a = "£";
var b = "£";
var replaced=replace(string,a,b);


}


textarea as follows:

<textarea rows="4" name="description" cols="40" onchange="description1=this.value;description1=replace1(description1);description.value=description1">

Anyone help?

Cheers

Ian

Kor
02-23-2006, 03:45 PM
as the electrics issues ... they must to be conected to the sniff to do their job... First at all, you must close the textarea tag...

<textarea rows="4" name="description" cols="40" onchange="description1=this.value;description1=replace1(description1);description.value=description1 "></textarea>


but of course doesn't work in FF

Most of the time that means deffinitely that your code is either wrong, or out of standards. I used to be an IE user on the IE5 times, now I use manly FF. Guess why?

What is your code suppose to do, after all? Your code looks weird and "snake-eats-its-tale"... And redundant as well...

ijyoung
02-23-2006, 03:59 PM
Sorry </textarea> is in original. That ain't the issue as the page has no markup errors.

The script allows the textarea to change line breaks etc etc into html so that when viewed by browser it will look like the way it was inputted.

Cheers

Ian