views:

230

answers:

2

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>
A: 

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.

Nick Craver
thanks for the link Nick
FFish
+1  A: 

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

jitter
cheers! thanks for the "Why?"
FFish