Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trapping for endlines

    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:

    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
    Code:
    <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!

  • #2
    New Coder
    Join Date
    Oct 2004
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •