www.webdeveloper.com
Results 1 to 11 of 11

Thread: Line break in Text area

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    Line break in Text area

    Hi all,

    I'm strugelling with this script.
    I'm retrieving text from a text area, sending to google to translate and retriving it and putting it back a text box.

    But when it comes back, i do not have any line breaks anymore, making the text hard to read :-(

    function TranslateFrom(lang) {
    var strtext = document.getElementById("txtmessage").innerHTML;
    var strtextor = document.getElementById("txtmessage").innerHTML;
    google.language.translate(strtext, 'en', 'fr', function(result) {
    if (!result.error) {
    var container = document.getElementById("txtmessage");
    container.innerHTML = strtextor + result.translation;
    }
    });
    }

    both my strtextor and result.translation do not have any line break
    any one got a idea what to do?

    thanks a lot,

    Mike

  2. #2
    Join Date
    Aug 2009
    Posts
    13
    Are you sending the html? As in ..

    sentence 1 <br>
    sentence 2 <br>

    ?

    You know simple textarea do not preserve line breaks.

  3. #3
    Join Date
    Aug 2009
    Posts
    2

    no html

    Hi,

    no i'm not sending, html.
    I'm just retrieving some like

    "Hi john,

    how are you. did you recieve the order okay.

    Best regards,
    Mike"

    and i get it back all in on sentence.

    I there a way to force like a line breack on all , or . then maybe?


    thanks mike

  4. #4
    Join Date
    Aug 2009
    Posts
    593
    Try a string replace like:

    Code:
    var x = "Hi John etc...";
    x = x.replace(/\n/g, "<br />");
    alert(x);
    This will change all new lines "\n" to <br />

  5. #5
    Join Date
    Aug 2009
    Posts
    13
    WYSIWYG editor will preserve line breaks, so instead of using <textarea> use any open source WSIWYG editor, it will preserve <br> tags on its own.

    And, you can always use a delimiter (. or ,) and replace (e.g. Replace method) it with a .<br> or ,<br>.

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    change the linebreaks to " ^ ", translate, then reverse it on the result.

    depending on the service, you might need a different symbol, but I'll bet you can find one.
    even " ZqZ " should work, the replacement doesn't have to be a special char.

  7. #7
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by thraddash View Post
    Try a string replace like:

    Code:
    var x = "Hi John etc...";
    x = x.replace(/\n/g, "<br />");
    alert(x);
    This will change all new lines "\n" to <br />
    and \n does not represent a new line in about 90% of computers on the internet, remember that \r\n is what a windows based system recognises as a new line.

    Have you considered encoding important characters? like %0D (hex) for new line?

  8. #8
    Join Date
    Jun 2007
    Posts
    667
    If container is an actual <textarea>, try
    Code:
    container.value = strtextor +  result.translation;
    Y_U U_G_A_E_U_ B_S_A_D_

  9. #9
    Join Date
    Aug 2009
    Posts
    593
    Quote Originally Posted by JunkMale View Post
    and \n does not represent a new line in about 90&#37; of computers on the internet, remember that \r\n is what a windows based system recognises as a new line.
    So what kind of systems do 90% of the internet computers use then?

    I am aware that windows requires the additional code (ascii 10) to represent a new line. But the new line character does still exist (ascii 13).

    I have tested this method on both Windows and Unix systems without fail.
    Last edited by thraddash; 08-16-2009 at 08:52 AM.

  10. #10
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by thraddash View Post
    So what kind of systems do 90&#37; of the internet computers use then?

    I am aware that windows requires the additional code (ascii 10) to represent a new line. But the new line character does still exist (ascii 13).

    I have tested this method on both Windows and Unix systems without fail.
    it depends on what you're trying to do.
    \r\n contains \n, so splitting on \n will still split the lines on windows.
    a simple \n creates a newline in a textarea as well, but a file with just \n (no \r) will not display the lines in notepad, though copying and pasting the garbled text from notepad into a textarea will fix the problem and show the lines as expected.


    the "best" splitter would be /\r?\n/g, which works on all systems.

  11. #11
    Join Date
    Aug 2009
    Posts
    593
    Based on the original post I am obviously not formatting the text for windows. It is to make the text readable in the browser which only shows line-breaks using <br /> or <pre> tags. \r is not necessary in this situation.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles