www.webdeveloper.com
Results 1 to 5 of 5

Thread: Newbie help

  1. #1
    Join Date
    Oct 2012
    Posts
    3

    Newbie help

    Hey guys, I'm new to Javascript and I have a few simple problems that I can't figure out. The problems require me to copy from the textbook and run it through the validator on w3, then correct the errors. However, I keep getting problems that I have no idea how to solve. I look through the chapter in my book, but I can't come up with anything. For the first problem, I'm getting error messages like:

    Error Line 20, Column 20: document type does not allow element "ul" here

    document.write("<ul>");

    (the > is underlined and in red, as well as the " in another message)

    Line 21, Column 38: character data is not allowed here

    document.write("<li>Eat less fat</li>")

    (the last " is underlined and red)



    I know these are probably very simple problems for you guys, but I'm stuck and have no idea on how to correct them. Any help is appreciated.


    Code:
    <!DOCTYPE html PUBLIC 
    	"-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Dietary Recommendations</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h1>Dietary Recommendations</h1>
    <p>The American Heart Association recommends the following dietary guidelines for a healthy heart:</p>
    <script type="text/javascript">
    /* <![CDATA[ */
    /*
    Joshua Meadors
    10/24/2012
    Exercise 1-6
    */
    /* ]]> */
    document.write("<ul>");
    document.write("<li>Eat less fat</li>")
    document.write("<li>Avoid sugary and processed foods</li>")
    document.write("<li>Eat plenty of fiber-rich foods</li>")
    document.write("<li>Cut down on salt</li>")
    document.write("<li>Eat at least 400g of fruit and vegetables each day</li>")
    document.write("<ul>");
    </script>
    
    </body>
    </html>

  2. #2
    Join Date
    Oct 2012
    Posts
    3
    Could a mod please erase my last name in the code? It's on line 15, but I can't edit my post. Thanks

  3. #3
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    The CDATA section should cover all of the code in your script element:

    Code:
    <script type="text/javascript">
    //<![CDATA[
    
    All your JavaScript code here...
    
    //]]>
    </script>
    Right now the validator thinks there's an actual <ul> element inside your JavaScript code as it doesn't know that that code is part of the script - it just sees everything as HTML. The CDATA prevents that from happening.

  4. #4
    Join Date
    Oct 2012
    Posts
    3
    That works perfectly. Thank you.

  5. #5
    Join Date
    Sep 2008
    Location
    Dallas
    Posts
    154
    You're also not closing out your <ul>. The last line should say document.write("</ul>"); You missed the "/".

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