Thursday, August 23, 2007

Function to replace Carriage return to
in HTML TEXTAREA

Function to replace Carriage return to BR in HTML TEXTAREA

1 comment:

Jared said...

//My adaptation of his code. He had an incomplete for loop in his code.

function replaceCarriageReturn(emailBody,replaceWith)
{
emailBody = escape(emailBody); //encode all characters in text area to find carriage return character

for(i=0; i < emailBody.length; i++) {
//loop through string, replacing carriage return encoding with HTML break tag
if(emailBody.indexOf("%0D%0A") > -1) {
//Windows encodes returns as \r\n hex
emailBody=emailBody.replace("%0D%0A",replaceWith)
}
else if(emailBody.indexOf("%0A") > -1) {
//Unix encodes returns as \n hex
emailBody=emailBody.replace("%0A",replaceWith)
}
else if(emailBody.indexOf("%0D") > -1) {
//Macintosh encodes returns as \r hex
emailBody=emailBody.replace("%0D",replaceWith)
}
}
emailBody=unescape(emailBody) //decode all characters in text area back
alert(emailBody);
}


input type="button" onclick="replaceCarriageReturn(form1.body.value,'br />')" value="Replace Carriage Returns with Html Break Tag">