469,612 Members | 1,673 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

<textarea> function to replace Returns and Spaces

I'm working on a form to collect data in a textarea which and am trying to keep returns and spaces. I have a couple of functions that I Frankensteined together to replace returns with <br> and to replace spaces with &nbsp;. The <br> part works well enough, but I keep getting "%20" instead of "&nbsp;" for the spaces.

I understand that escape() changes " " to "%20", but I would think the ConvertSpaces function below would change the %20 to &nbsp;, but it doesn't. (FYI, I need &nbsp; instead of %20 because it appears to be the only "space" code that the resulting shopping cart page will accept for multiple sequential spaces.)

I know there's lots about this on the web, but apparently I'm too unskilled to understand anything I'm finding.

<input onclick="ConvertCarriageReturns(this.form.op31,'&l t;br&gt;')" type="image" name="add" /></form>

function ConvertCarriageReturns(textarea, strReplace)
{
textarea.value = escape(textarea.value)
for(i=0;i<textarea.value.length;i++)
{
if(textarea.value.indexOf("%0D%0A") > -1 )
{
textarea.value = textarea.value.replace("%0D%0A",strReplace)
}
}
ConvertSpaces(unescape(textarea.value),'&nbsp;')
}


function ConvertSpaces(textarea, strReplace)
{
textarea.value = escape(textarea.value)
for(i=0;i<textarea.value.length;i++)
{
if(textarea.value.indexOf(" ") > -1 )
{
textarea.value = textarea.value.replace(" ",strReplace)
}
}
textarea.value = unescape(textarea.value)
}
May 15 '07 #1
3 5158
iam_clint
1,208 Expert 1GB
i believe you have to replace " " in your serverside code... because it will escape it to %20 also i think.
May 15 '07 #2
Rrrggh. I was really hoping you were going to tell me I missed a semicolon or something easy like that. This is reaching beyond my realm of personal ability now.
May 15 '07 #3
pbmods
5,821 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<textarea.value.length;i++)
  2. {
  3. if(textarea.value.indexOf(" ") > -1 )
  4. {
  5. textarea.value = textarea.value.replace(" ",strReplace)
  6. }
  7. }
  8. textarea.value = unescape(textarea.value)
This might not work properly, especially if, for example, two spaces are next to each other and strReplace is null.

Try this instead:

Expand|Select|Wrap|Line Numbers
  1. if(String(strReplace).indexOf(' ') > -1) {
  2.     alert('strReplace contains a space!\n"' + strReplace + '");
  3.     return false;
  4. }
  5.  
  6. textarea.value = textarea.value.replace(/ /g, strReplace);
  7.  
If you don't want to use regular expressions, you could just use the code you had, but without the for loop:
Expand|Select|Wrap|Line Numbers
  1. while(textarea.value.indexOf(' ') > -1)
  2.     textarea.value = textarea.value.replace(' ', strReplace);
  3.  
May 16 '07 #4

Post your reply

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

Similar topics

5 posts views Thread by Matt | last post: by
4 posts views Thread by Dennis Allen | last post: by
2 posts views Thread by Jonathan Taub | last post: by
11 posts views Thread by Les Paul | last post: by
8 posts views Thread by ASP Yaboh | last post: by
5 posts views Thread by Icarus - iD_Ten_T helper | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.