beginner javascript question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jasonchan
    New Member
    • Oct 2006
    • 16

    beginner javascript question

    Need some help fixing this. basically, i want the second document.write to start on a new line and i am using the "\n" escape code but it is not working. I tried putting the <pre> tag around the script and it still doesnt work. Whats going on here?

    <html>
    <head>
    <title>Javascri pt Run</title>

    </head>
    <body>

    <script type="text/javascript">
    var firstName = prompt("Please type in your name", "What is your first name?");
    var thirdLetter = firstName.charA t(2);

    alert("Hello "+firstName );


    document.write( "The third letter of your name is "+thirdLetter\n );
    document.write( "That is your character right ?");
    </script>


    </body>

    </html>
  • vituko
    New Member
    • Dec 2006
    • 48

    #2
    \n => <br/>

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Two things, one is as vituko mentioned: change \n to <br>
      Secondly, it must be included within the string:
      Code:
      document.write("The third letter of your name is "+thirdLetter+"<br>");
      document.write("That is your character right ?");
      or
      Code:
      document.write("The third letter of your name is "+thirdLetter);
      document.write("<br>That is your character right ?");

      Comment

      • jasonchan
        New Member
        • Oct 2006
        • 16

        #4
        when do you use the "\n" then? I have played around with it before and it did work but not for this example..

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          You can use "\n" in a popup box, e.g. alert, prompt or confirm to give your information/question over two or more lines.

          Comment

          Working...