I think it's perfectly valid to have a div inside a script tag (or at
least useful), if a div makes sense to the TYPE you defined for the
script. For example, John Resig uses a script tag with type "text/
html" in his micro-templating solution:
http://ejohn.org/blog/javascript-micro-templating/

In this instance though (and in reply to the original author) you add
an ID to the SCRIPT tag, and refer to that (I don't see why it
wouldn't work with that facebook type instead of html - but you'd
probably want to test it in a few different browsers ;).  For the
example you gave, you can get a reference to the DIV by doing:

<script type="text/fbml" id="test">
        <div>some test</div>
</script>
$(function(){
        alert($( $( '#test' ).html() ).text() ); //alerts "some text"
});

The "trick" is to get the HTML of the script tag and turn in into DOM
elements with jQuery - but remember, because you are passing all the
HTML into the jQUery function then you are immediately selecting ALL
of the top level elements. In this case, there is just one DIV - so
you are just selecting that.

On Sep 10, 4:16 am, "Cesar Sanz" <the.email.tr...@gmail.com> wrote:
> Why in the earth must you have a div inside <scipt> tags?
>
> ----- Original Message -----
> From: "Nick Fitzsimons" <n...@nickfitz.co.uk>
> To: <jquery-en@googlegroups.com>
> Sent: Wednesday, September 09, 2009 6:55 AM
> Subject: [jQuery] Re: HTML code inside script tag, how access to it with
>
> DOM???
>
> 2009/9/9 Mariano <mariano.calan...@gmail.com>:
>
> > I have an HTML page that looks like this:
>
> > <html>
> > <head></head>
> > <body>
> > <script type="text/fbml">
> > <div id="test">some test</div>
> > </script>
> > </body>
> > </html>
>
> > Now I have the will to access to the text inside test div but the code
> > $('#test').text();
> > doesn't work, matter of fact nothing is returned.
>
> > If my div is dragged outside of script tag, js code return me "some
> > test" correctly, but alas, div MUST be inside script.
>
> That isn't valid HTML; even if you manage to get it to work in one
> browser, it may well behave differently in others. A script element
> can only contain script code:
> <http://www.w3.org/TR/html401/interact/scripts.html#edef-SCRIPT>
>
> Regards,
>
> Nick.
> --
> Nick Fitzsimonshttp://www.nickfitz.co.uk/
>
>

Reply via email to