Most of the multi line text areas we are using the user can input text by using line breaks. If that text used for internal purpose saving it as a single string may not be a problem. But what’s if we have to show that on a web page (forums, user comments, etc). We have to replace the new line character with the <br> tag. The famous way is to replace chr(13) with the <br> tag by using the Java Script. But the problem is this method is not working fine with every platform. But I have found a way to come up with and thought to share it with you. function escapeVal(textarea,replaceWith) { textarea.value = for(i=0; i<textarea.value.length; i++) { { textarea.value=textarea.value.replace(“%0D%0A”,replaceWith) } { textarea.value=textarea.value.replace(“%0A”,replaceWith) } { textarea.value=textarea.value.replace(“%0D”,replaceWith) } } textarea.value = unescape(textarea.value) } I have tested this on the Linux and windows with both Firefox and IE 7; and it worked fine. Then the next step, sometimes we may need to convert <br> tags back to new line characters (e.g.: user tries to edit the content). On that occasion we cannot show the <br> tags. But for this we do not have to use the javascript, we can do this on the server side. Replace(Comment.CommentValue,”<br>”,chr(13),”all”) I have done this by the ColdFusion and replacing it with chr(13) works fine.
escape(textarea.value)
if(textarea.value.indexOf(“%0D%0A”) >
-1)
else
if(textarea.value.indexOf(“%0A”) >
-1)
else
if(textarea.value.indexOf(“%0D”) >
-1)
Replace new line with <br> tag
April 23, 2008 by gayan007
Advertisement