...

View Full Version : Trapping for endlines



infamoustim@neo
10-13-2004, 04:33 AM
I am using javascript to show a real-time preview of a news article before the user submits it. All of the fields are properly updating, however I want to trap for endlines on the textbox. Before we go any farther, here's the related code:


<script language="JavaScript"><!--
function set_text()
{
// We need to replace any newlines
var newHTML = new String ('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
newHTML += document.form.story.value;


>>>> newHTML = newHTML.replace (/ <endline> /gi, '<br> <endline>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');


if (document.getElementById)
document.getElementById('curtext').innerHTML = newHTML;
else if (document.all)
document.all['curtext'].innerHTML = newHTML;
}
//--></script>


For completeness, here are the affected HTML elements

<div class="newstext" id="curtext">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Content of the article will go here when you finish writing the article.
Please use no more than 500 words.</div>

<!-- snip -->
<textarea rows="5" cols="50" name="story" maxlength="3000"
onBlur="set_text()">Content of the article will go here when you
finish writing the article. Please use no more than 500 words.
</textarea>

The important part is the line marked with the >>>>. What I'm wondering is what character represents the end of a line in javascript (represented by <endline>)? The javascript takes the value of the textbox, reformats it, and then replaces the inner contents of the "curtext" div with the newly formatted data.
What can I stick in there so that the script will properly recognize breaklines and make my string::replace() successful? Thanks in advance!

infamoustim@neo
10-13-2004, 05:06 AM
Nevermind, scratch that question, I figured it out.
For those of you interested, PHP was stripping out the \n and actually replacing it with a newline character, which caused javascript to choke. I put in
\\n
instead, and it worked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum