4

I have a textarea with the following content (innerHTML):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>

</body>
</html>

To prepare the content for DOM parsing with jQuery I do the following (temp is just a div):

$('#temp').append(input.val());

console.log($('#temp').html());

As pointed out at jQuery object from complete HTML document, the head, body, html and doctype tags are gone:

<meta charset="utf-8">
<title></title> 

So is there any other way not to append to the DOM and still get DOM elements to work with? Iframe may be a last resort but that would be ugly.

18
  • 3
    What exactly do you want to accomplish? Commented Mar 16, 2011 at 17:41
  • 1
    you can just have them in a variable, or even store them using data Commented Mar 16, 2011 at 17:48
  • 1
    @DADU What changes do you want to make on that HTML string? Can you give some examples? Commented Mar 16, 2011 at 17:52
  • 1
    @DADU jQuery cannot parse HTML, HEAD, BODY and DOCTYPE elements. Therefore, you cannot use jQuery's HTML parsing abilities in your case. What you need is a script that can parse a full HTML string into a document fragment object. I am not aware of such a script. Commented Mar 16, 2011 at 18:28
  • 2
    @DADU Check out this article: developer.mozilla.org/en/Code_snippets/HTML_to_DOM Commented Mar 16, 2011 at 18:59

1 Answer 1

4

You could do this..

 $(input.val()).find('your-selector')

Or, you could pass it as context..

$('your-selector', $(input.val()))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.