TunaFFish
TunaFFish

Reputation: 11302

W3C Markup Validation errors with jQuery

Running a validation on my pages where I used jQuery gives me lots of errors.. I escaped the closing tags but keep getting errors.

<script type="text/javascript">
    $(document).ready(function() {
        $("#main").html('<p>hello world<\/p>'); 
    });
</script>

Upvotes: 2

Views: 2668

Answers (2)

jitter
jitter

Reputation: 54605

Assuming you use XHTML as DOCTYPE you should wrap js-code which contains HTML fragments with CDATA

<script type="text/javascript">
    $(document).ready(function() {
        /*<![CDATA[*/
        $("#main").html('<p>hello world</p>');
        /*]]>*/
    });
</script>

Why?: Mozilla Dev: Properly Using CSS and JavaScript in XHTML Documents

Upvotes: 4

Nick Craver
Nick Craver

Reputation: 630439

Do this:

<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {
        $("#main").html('<p>hello world</p>'); 
    });
//]]>
</script>

You can read a bit more on the topic here. The basics are that Javascript tags are CDATA elements normally, PCDATA with XHTML (so it looks inside), to be safe they need to be marked up in this way.

Upvotes: 1

Related Questions