infamoustim@neo
10-13-2004, 03: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 (' ');
newHTML += document.form.story.value;
>>>> newHTML = newHTML.replace (/ <endline> /gi, '<br> <endline>
');
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">
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!
<script language="JavaScript"><!--
function set_text()
{
// We need to replace any newlines
var newHTML = new String (' ');
newHTML += document.form.story.value;
>>>> newHTML = newHTML.replace (/ <endline> /gi, '<br> <endline>
');
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">
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!