Page 1 of 1

javascript form

Posted: Sat Jan 20, 2007 10:44 pm
by tecktalkcm0391

Code: Select all

function generate_text(field){
	var field = field;
	var str = document.forms.message.field.value;
// tabs
	str.replace(/[tab]/gi, "      ");
	// address
	str.replace(/[contact]/gi, " <a href=\"directions.htm\">");
	str.replace(/[\/contact]/gi, "</a>");
	// contact 
	str.replace(/[contact]/gi, " <a href=\"contact.htm\">");
	str.replace(/[\/contact]/gi, "</a>");
	// paragraph
	str.replace(/[p]/gi, "<p>");
	
	return str;  

}
How come it doesn't replace any of the [p]'s with <p> and [tab]s with &nbsp....?

Posted: Sat Jan 20, 2007 11:15 pm
by superdezign
Well, from the look of things your attempting to replace the string "[tab]." Do your users have to type in "[tab]" to create a tab, or are you actually looking for a tab?

If I'm not mistaken, you would then be looking for "\t" instead.

And as for the "[p]," I'm frankly confused with what your looking for. Are you searching for newlines? Because, in that case, you'd use "\n", although it'd be more sensible to replace that with "<br>" instead of "<p>."

I hope this is helpful. You don't really specify what purpose your function is performing, so I'm making quite a few assumptions.

Posted: Sun Jan 21, 2007 8:19 am
by feyd
The code is using regular expressions. "[" and "]" have a special meaning in regular expressions. For example the first is looking for any of the characters "t", "a", or "b" to replace them.

Posted: Sun Jan 21, 2007 10:22 am
by superdezign
Okay then, I was just making sure you were at least on the right track. Now to teach you a little something:

String.replace(findString, newString) is a function, therefore it works like a function. It returns a value. You are returning the value, and doing nothing with it.

The correct syntax is:

str = str.replace(/[tab]/gi, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");

Just so you know though, I just discovered that your function is EXTREMELY glitchy.

Firstly, if you type the letters "tab" in the middle of anything, it does your tab thing.
Wanna know what's worse? Your "[p]" replacement replaces every "p" in your "&nbsp;" with "<p>." Also, when you replace "[contact]," you also replace the "contact" in "contact.htm," causing a loop. If that's not a horrible glitch, then I don't know what is. I really think that you should rethink this script.

My first suggestion would be to lose the brackets.

Posted: Sun Jan 21, 2007 10:36 am
by Kieran Huggins
I'm starting to think "superdezign" was a misspelling....

Listen to feyd, you need to escape square brackets.

Posted: Sun Jan 21, 2007 10:57 am
by superdezign
Was I incorrect..? I tested the code and ended up with some very strange results, mostly the code replacing text within the replacements.

Posted: Sun Jan 21, 2007 11:37 am
by Kieran Huggins
you need to escape square brackets
He wants to replace the string including the square brackets with his own markup (think bbcode).

Posted: Sun Jan 21, 2007 2:09 pm
by superdezign
I had a feeling that was what he was attempting to do, but I didn't think BBcode had a [tab].

... Kind of embarassing that I wasn't aware that brackets had an effect on the replace() function.

Posted: Sun Jan 21, 2007 3:19 pm
by feyd
Brackets don't affect the replace function. They affect the regular expression object is how it tests for positives.

:P

Posted: Sun Jan 21, 2007 4:12 pm
by superdezign
Yes yes.. I've been avoiding regular expressions for years now, and I JUST visited Regular-Expressions.info

I have to say, I thought it was much more complicated. Always looked like jarbled garbage to me.
I understand now.

Posted: Sun Jan 28, 2007 10:20 am
by tecktalkcm0391

Code: Select all

function update_preview(nid, text)
{
	if (document.getElementById)
	{
		x = document.getElementById(nid);
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[nid];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[nid];
		x.document.open();
		x.document.write(text);
		x.document.close();
	}
}


function generate_text(field){
	var field = field;
	var str = document.forms.message.message_box.value; // Yes i know that i have this set werid i couldn't get it to go to the field, but i only need one for now so i just manually set it
	// tabs
	str = str.replace(/\[tab\]/gi, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	// address
	str = str.replace(/\[address\]/gi, " <a href=\"directions.htm\">");
	str = str.replace(/\[\/address\]/gi, "</a>");
	// contact 
	str = str.replace(/\[contact\]/gi, " <a href=\"contact.htm\">");
	str = str.replace(/\[\/contact\]/gi, "</a>");
	// paragraph
	str = str.replace(/\[p\]/gi, "<p>");
	//str = str.replace("[p]"/gi, "<p>");
	
	return str;  

}
//-->
I have that code, and it works fine in Firefox, but why doesn't it work in IE.

I have it do text = generate_text('message_box') and then update_preview('preview',text)


EDIT:
I've got it down to the str.replaces i thought it was the updating of the preview area, but I really don't understand what you guys are trying to tell me, is anything really wrong with my code?

ADDED: I just read http://www.dreamincode.net/code/snippet304.htm now I get it... thanks guys