1

I'm having a lot of errors when I run W3C validator on an html page with jquery code. This is an example of the jquery code I'm using:

jQuery(document).ready(function($) 
{
    /*<![CDATA[*/

    $("body").append("<div id='mainBody'>");
    $("#mainBody").append("<h1 align='center'>SCORM Authoring Tools");

    $("#mainBody").append("<table id='myTable' border='1' />");

    $("#myTable").append("<tr id='tr1' />");
    /*]]>*/
});

This is an example of these errors:

Line 4, Column 267: end tag for element "TD" which is not open
append("<td class='rows'>Product</td>");    
$("#tr1").append("<td class='rows'>…

I'm using <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">; Thanks

FadelMS
  • 2,027
  • 5
  • 25
  • 42
  • The question is what should I do to get red of these errors? Or how to make my document valid? – FadelMS Aug 13 '11 at 08:22
  • It would help if you posted the errors. – Kevin Bowersox Aug 13 '11 at 08:23
  • This is an example of these errors: append("Product"); $("#tr1").append("… – FadelMS Aug 13 '11 at 08:24
  • 1
    Probably not related but the `/*<![CDATA[*/` comments are unnecessary. They have no meaning in HTML. – Felix Kling Aug 13 '11 at 08:24
  • You do not need CDATA sections when you're using an HTML 4 doctype... – BoltClock Aug 13 '11 at 08:24
  • 1
    This validates for me: https://gist.github.com/48f70d9921ddaaca088d I just copied/pasted your code. Did you get your ` – Benjamin Atkin Aug 13 '11 at 08:31
  • 1
    @Ben: And it validates also without the CDATA comments (as expected). – Felix Kling Aug 13 '11 at 08:34
  • @Felix I knew that. It's just late and I didn't bother to double-check. :) It's one reason JSON has optional escaping of the forward slash, which doesn't need to be escaped, so I can use "<\/script>". Doesn't apply to XML. http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag/1450633#1450633 – Benjamin Atkin Aug 13 '11 at 08:43

2 Answers2

4

It isn't the code you pasted that's causing the issue. It's the code you didn't paste. The code below validates for me, with warnings, on http://validator.w3.org/#validate_by_input :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>JS Bin</title>
<script type="text/javascript">
jQuery(document).ready(function($) 
{
    /*<![CDATA[*/

    $("body").append("<div id='mainBody'>");
    $("#mainBody").append("<h1 align='center'>SCORM Authoring Tools");

    $("#mainBody").append("<table id='myTable' border='1' />");

    $("#myTable").append("<tr id='tr1' />");
    /*]]>*/
});
</script>
</head>
<body>
  <p id="hello">Hello World</p>
</body>
</html>

I think it's probably missing or incorrect <script> tags.

As Felix King pointed out, you can also remove the CDATA comments, since you're validating with HTML rather than XML.

Benjamin Atkin
  • 14,071
  • 7
  • 61
  • 60
  • Ben You are right. The portion of code I posted is OK and the problem was somewhere else. Somehow I managed to solve the problem or it seems so. I removed all close tags and amazing the validator was so happy. But I don't know in fact whether this the right way to do it. – FadelMS Aug 13 '11 at 09:03
  • This code didn't validate: jQuery(document).ready(function($) { $("body").append("
    "); $("#mainBody").append("

    SCORM Authoring Tools

    "); $("#mainBody").append("
    "); $("#myTable").append(""); $("#tr1").append("Product"); });
    – FadelMS Aug 13 '11 at 09:33
  • @FadelMS - See the first section at http://www.htmlhelp.com/tools/validator/problems.html – Alohci Aug 13 '11 at 10:16
1

You'd want to put it around all your code:

/*<![CDATA[*/
jQuery(document).ready(function($) 
{

    $("body").append("<div id='mainBody'>");
    $("#mainBody").append("<h1 align='center'>SCORM Authoring Tools");

    $("#mainBody").append("<table id='myTable' border='1' />");

    $("#myTable").append("<tr id='tr1' />");
});
/*]]>*/
jfriend00
  • 683,504
  • 96
  • 985
  • 979