views:

127

answers:

2

Hi, I'm using a small snippet of JS to add a presentational element to my blockquotes (As seen here):

<script type="text/javascript"> 
 $('blockquote.message').append('<span class="arrow" />'); 
</script>

But the W3C validator hates that crap:

document type does not allow element "span" here

What am I doing wrong? Is there a way to correct this error, while maintaining the function?

Thanks!

+4  A: 

The blockquote tag can only contain block elements such as P, H1..n, OL/UL, PRE, DL, DIV, NOSCRIPT, BLOCKQUOTE, FORM, HR, TABLE, FIELDSET, ADDRESS

Rob Di Marco
The W3C validator doesn't run the JavaScript, so it doesn't know he's adding a span to a blockquote. It's seeing the "<span" piece of his code, interpreting it as HTML, and complaining about that. Put it another way: the validator doesn't automatically ignore everything between <script> and </script> tags. That's why you need CDATA.
RichieHindle
+5  A: 

I assume you're using XHTML? You need wrap your JavaScript in CDATA:

<script type="text/javascript">
//<![CDATA[
        $('blockquote.message').append('<span class="arrow" />');       
//]]>
</script>

See the XHTML reference here: http://xhtml.com/en/xhtml/reference/script/ - "If the script element contains embedded script and that script contains XHTML markup characters such as <, >, & and ", then the script should be enclosed in CDATA"

RichieHindle
Thanks! I saw your comment on the other answer too. Does adding a span to a blockquote by javascript matter? I could also us a self-closing div if that would be better.
John Stephens
I honestly don't know. It's against the spec, but the browsers will almost certainly just cope. If you want to be sure, use a div.
RichieHindle