tags:

views:

132

answers:

2

jQuery append function causes validation error. How to fix it?

append("<span>");

causes Document type does not allow element here

+3  A: 

It has nothing to do with jQuery, it's the string in the script that is causing it.

If you are using XHTML, put CDATA tags in the script to keep it from breaking the validation:

<script type="text/javascript">
<![CDATA[

]]>
</script>

Or if the XHTML is served with the content type text/html (which is common):

<script type="text/javascript">
//<![CDATA[

//]]>
</script>
Guffa
+1 forgot about CData, that's the solution of course.
Pekka
A: 

You're being very unclear about when this happens and what validation you are talking about, but I am assuming you are validating the whole document using the W3C validator.

In that case, this has actually nothing to do with jQuery, but with the fact that the validator validates this despite it being in a JavaScript block. I had a similar problem a few months ago, see here.

Edit: @Guffa provides the perfect solution in his answer.

Pekka