...

View Full Version : replace doesn't work ??



docock
08-25-2011, 06:46 PM
I've got a form textarea on my site, which after being submitted is stripped by my php file.

Then my javascript kicks in and does some analysis of the entered text. However everytime the user entered a hard return within the textarea my javascript gives me an error 'tekenreeks niet afgesloten' which means something like 'string is not closed'.

Therefore I decided to replace all occurences of chr(10) and chr(13) with
ok2 (just some random characters), I did this with php which worked fine, and I didn't get the javascript error.

However I need to reshow the entered text (original) to the user, so I need to restore the old values. I tried
result.waarde.replace('ok2', 'chr(13)'); but it doesn't work, as it still displays ok2.

I'm no expert in javascript, but how can I replace those characters by the hard returns?

Oh yeah I'll show some more coding, perhaps the error can be found there:


result.waarde.replace('mdw', 'chr(13)');container.innerHTML = result.waarde;


and in my html:


<div id='waarde' style="width:500px;"></div>

DaveyErwin
08-25-2011, 07:45 PM
I'm sorry but I really do not understand,
however
result.waarde.replace('mdw', 'chr(13)');container.innerHTML = result.waarde;
should be ...
container.innerHTML = result.waarde.replace('mdw', 'chr(13)');
hope this helps ?

Philip M
08-25-2011, 07:46 PM
To strip newlines:-


var txt = "The text as entered by the user";
strippedTxt = txt.replace(/\r?\n/g,""); // remove all newlines

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Old Pedant
08-25-2011, 09:56 PM
I don't understand how the code result.waarde is able to refer to the div identified as

<div id='waarde' style="width:500px;"></div>

What is the variable result supposed to be???

And why would you want the string "chr(13)" in the text of your innerHTML??? Are you thinking the chr(13) will be a newline character, as it would be in VB or VBScript? Nope, no way. In JavaScript, the newline character is "\n"

Further, the replace method normally takes a regular expression as its first argument. If you use a string, it's automatically converted to a regular expression.

So...

I'd guess that what's really needed is:


container.innerHTML = document.getElementById("waarde").replace( /mdw/, "\n" );

But even that assumes that container is a reference to some DOM element that *has* and innerHTML property.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum