tags:

views:

70

answers:

1

I have the following html snippet. I am trying to let a user enter some html into the textarea, then I will parse out their style block and do some things with their styles. How can I pass the html string from the #html textarea into jQuery so I can parse it?

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script src="script/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function ParseIt() {
            alert($("#html").val());
            $("style", $("#html")).each(function(){ alert($(this).text());});
        }
    </script>
</head>
<body>
        <textarea id="html" name="html" class="email_body " rows="25" cols="100"></textarea>
        <br />
        <input type="button" value="Parse It" onclick="ParseIt();" />
</body>
</html>

Have also tried this function with no luck:

<script type="text/javascript">
    function ParseIt() {
        alert('parsing');
        $($("#html").val()).find('style').each(function () {
            alert('found style');
            alert($(this).text());
        });
    }
</script>
+3  A: 

The snippit below might be better for ya.

$( $("#html").val() ).find('style').each(function(){
  // do stuff
});
BBonifield
Tried that and some similar variations but it doesn't seem to work. Tried searching for just a div or anything and it never seems to work using very simple html examples.
brendan
Seems to work fine here - http://jsfiddle.net/eMbDP/1/
BBonifield
Thanks for following up! You're right - it creates the jquery object, but I can't seem to locate any style elements in it - see http://jsfiddle.net/eMbDP/5/
brendan
http://jsfiddle.net/eMbDP/6/ - It logs as length == 1 there when using the HTML snippit.
BBonifield
Interesting. Your html snippet works but my full html page does not: http://jsfiddle.net/eMbDP/8/
brendan
Works if I just wrap it in a div like this: var htmlText = "<div>" + $("#html").val() + "</div>"; $(htmlText).find('style').each(function () { alert($(this).text()); });
brendan