Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace Carriage Return

Status
Not open for further replies.

OceanDesigner

Programmer
Oct 30, 2003
173
US
I have a page with a button and some text fields. When the button is pushed a new window opens that prints the text in the text fields in window 1 to the page in window 2. I do this with in line javascript:

Code:
document.write("<td>" + opener.document.myform.Description.value + "</td>");

This field can have carriage returns, but the returns are not observed in the second page. So I need to replace the carriage returns with "<br>". In vbscript, I can do this very simply with: replace(variable,chr(13),"<br>"). What is the comparable code in javascript?

Thanks, Jeff
 
Got it. In javascritpt it looks like this:

variable.replace("\n","<br>")
 
I just wrote this 2 days ago to test some regex:
Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JavaScript>

function regexpCheck(str) {
   str = str.replace(/[\n]/g, "<br>");
   str = str.replace(/[\r]/g, "");
   alert(str);
}

</SCRIPT>

<BODY>
<FORM NAME="blahForm">
<textarea NAME=blahText rows=10></textarea><br>
<INPUT TYPE=BUTTON VALUE='Remove Carriage Returns' NAME=blahButton ONCLICK='regexpCheck(blahForm.blahText.value);'>
</FORM>
</BODY>
</HTML>

-kaht

banghead.gif
 
Close.....

You need to compensate for both newline and linefeed.

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top