line breaks in alert box from html script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Humphrey Shires
    New Member
    • Oct 2011
    • 3

    line breaks in alert box from html script

    I'm building an HTML page with javascript. All is fine in the document.write until I want to create an alert box from an onClick on the page.
    Here's the script(part) so far:"<td><a href onclick=alert(' Some&nbsp;list& nbsp;here')>The list</a></td>".
    This brings back alert box with - Some list here - OK but I want a line break where the second &nbsp; is. I've tried all the \n \r <br> and <p> and %0A combinations I can think of.
    Any ideas please. Remember that " doesnt work because the script is aleady within double quotes. ps I cant get a var to work from inside the script either !?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    in an alert \n should work:

    Code:
    alert('foo\nbar');
    and of course can you use a variable, JavaScript is a programming language so you would be lost without that possibility.

    Comment

    • Humphrey Shires
      New Member
      • Oct 2011
      • 3

      #3
      Many thanks gits but that doesn't work, the problem is how html reads the script passed over from js - but I've now sussed it (after a few hours) -- the \n needs an escape in front of it ie \\n.
      Posted for others is the part of document.write that applies (note the quotes):
      "<table><tr><td ><a href onclick=alert(' Some&nbsp;list\ \nhere')>The list</a></td></tr></table>".
      Try this in a table created from js and click on the words 'The list'
      Thnx to all who looked at it for me

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        basically the following is not really valid:

        Code:
        <a href onclick=alert('Some&nbsp;list\\nhere')>
        and should be:

        Code:
        <a href onclick="alert('Some&nbsp;list\nhere')">
        is the shown code a snippet in a php script? in case it is then in fact it will turn out as:

        Code:
        <a href onclick=alert('Some&nbsp;list\nhere')>
        later when the page is rendered. but attribute values should be enclosed in (double-) quotes
        Last edited by gits; Oct 14 '11, 06:37 PM.

        Comment

        • Humphrey Shires
          New Member
          • Oct 2011
          • 3

          #5
          Hi gits, I've now replaced the text(between the 's in the alert box) with a variable. Had problem getting this to work until I entered the variables in the <script> section of the page <head>. I couldn't use double quotes in your example because all this snippet was inside a set of doubles to produce a row of a table. The project is for in-house so whatever works without bringing down the ceiling should be OK. The \\n works in the text variable and (bonus) &nbsp; is not needed: eg var strText = 'amounts\\n\\n1 st\\nDue - $120.37'
          Once again many thanks

          Comment

          Working...