Hi, I would like to eliminate all trailing and leading newlines that would appear in a textarea on my web page. However, I would like not like to have all the text transformed into a single paragraph.
I wanted the same as you quite a long time ago; as I didn't know, if there is a predifined stuff to do that in Javascript, I tried to create a makeshift in order to get rid of linebreaks and spaces at the beginning end the end of the user's input.
For me it worked fine and until now, I don't know, if there is a "professional" solution for that in js...
Try this: [code=php] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> <script language="JavaScript" type="text/javascript"> <!-- function check(){ temp = document.forms[0].textInput.value; temp = temp.replace(/ /g, "ÿ");//replace spaces with ÿ temp = temp.replace(/s/g, "µ");//replace all remaining white spaces with µ out="µ"; out2="ÿ"; while (temp.indexOf(out)==0||temp.indexOf(out2)==0) {// replace leading µ's and ÿ's temp = (temp.substring(1, temp.length)); } while (temp.substring(temp.length-1,temp.length)==out||temp.substring(temp.length-1,temp.length)==out2) { //replace trailing µ's and ÿ's temp = (temp.substring(0, temp.length-1)); } temp = temp.replace(/µµ/g, "n");//change µ's in text back to linebreaks temp = temp.replace(/ÿ/g, " ");//change ÿ's in text back to spaces document.forms[0].textInput.value=temp; } //--> </script> </head> <body> <form> <textarea name="textInput" rows=8 cols=45></textarea><br> <input type=button value=click onclick="check()"> </body> </html> [/code] Cheers - Pit
BTW - I hope, you're not going to hang me, if a user really enters "ÿ" or "µ" :p (if you're dealing with maths or engineering, you could at least use something other than "µ" for that part of the job)...
Thank you for the speedy reply. I wasn't looking for a professional solution, just some code that would work. ? Unfortunately I tried the code and it's not removing the leading and trailing new lines.? .
I'm completely new to javascript so pardon my ignorance if what I say seems quite elementary.
I am planning to retrieve info from the input text area and display it on another page without the leading and trailing newlines. With your code, I am neither getting an error nor the correct output.?
Right now, I tested that at home on a PC Win98 with IE6 Opera 6 and NS4.7. The site, for which it was originally used was visited by people, whose machines ran with different OS's and browsers.
So far I haven't heard, that the stuff doesn't work - I don't know, what might be wrong. Did you use the code like I posted it, or did you put it into your own file?
if you have more than one form in your html and the form, the code I posted is dealing with, is not the first one in your page, the stuff will fail.
If so, either make the form in question the first one of your page or replace "forms[0]" with "forms[x]", x being the number of the form minus one (second form => "forms[1]" etc.).
The job it does is alright (replacing linebreaks with spaces). What tarun wanted is: I would like to eliminate all trailing and leading newlines that would appear in a textarea on my web page. However, I would like not like to have all the text transformed into a single paragraph.[/QUOTE] Due to that some more action is required...