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 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    388
    Thanks
    73
    Thanked 0 Times in 0 Posts

    line break in string

    How do you do line breaks in strings? I have a copy to clipboard function where i add the text so the user can paste in word or notepad, however, the text when pasted prints in one line. What am i doing wrong. Tried the below but it no work.

    Code:
    var text = "Lorem ipsum dolor sit amet,\n"
             + "consectetur adipisicing elit,\n"
             + "sed do eiusmod tempor incididunt\n"
             + "ut labore et dolore magna aliqua.";
    
    
    copyToClipboard(text)

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    You do know copyToClipboard(text); is IE only?
    Try <br /> instead of \n.
    Evolution - The non-random survival of random variants.

  • Users who have thanked sunfighter for this post:

    nikko50 (09-26-2013)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by nikko50 View Post
    How do you do line breaks in strings? I have a copy to clipboard function where i add the text so the user can paste in word or notepad, however, the text when pasted prints in one line. What am i doing wrong. Tried the below but it no work.

    Code:
    var text = "Lorem ipsum dolor sit amet,\n"
             + "consectetur adipisicing elit,\n"
             + "sed do eiusmod tempor incididunt\n"
             + "ut labore et dolore magna aliqua.";
    
    
    copyToClipboard(text)
    in IE (and thus windows in general), "\n" is not enough for a new line, you need CR+LF, aka "\r\n":

    Code:
    var text = "Lorem ipsum dolor sit amet,\r\n"
             + "consectetur adipisicing elit,\r\n"
             + "sed do eiusmod tempor incididunt\r\n"
             + "ut labore et dolore magna aliqua.";
    
    
    copyToClipboard(text)
    another alternative is to inject your line breaks after:


    Code:
    var text = "Lorem ipsum dolor sit amet,\n"
             + "consectetur adipisicing elit,\n"
             + "sed do eiusmod tempor incididunt\n"
             + "ut labore et dolore magna aliqua.";
    
    
    copyToClipboard(text .split(/\r?\n/).join("\n") )
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by sunfighter View Post
    Try <br /> instead of \n.
    That will not work because...

    Quote Originally Posted by nikko50
    so the user can paste in word or notepad

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by glenngv View Post
    That will not work because...
    because what?

    word 2010 is smart enough to recognize just "\n", but pasting into notepad doesn't work without the additional "\r"s that i showed in my last post.

    pasting a "<br />" into notepad or word will not have the desired effect, showing the angle bracket markup and text all on one line.

    note the clipboard flavor seems to be text, not html, and in plain text in windows, "\r\n" is how you make a new line.

    here is a live test if you want to check it out yourself.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Read my post again. I quoted sunfighter's reply not yours. And the continuation of my sentence is the quote below it.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by glenngv View Post
    Read my post again. I quoted sunfighter's reply not yours. And the continuation of my sentence is the quote below it.
    ahh, sorry about that; we're in agreement.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

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