Ticket #7456 (closed bug: invalid)
jQuery removes body-element when parsing string with html or xml
| Reported by: | mekwall | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.5 |
| Component: | manipulation | Version: | 1.4.4rc |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
When passing a string with well formed xml or html jQuery will remove any body-element, but leave all children nodes intact.
This can easily be reproduced by doing the following:
var xml = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Why is it not hitting this?</body></note>";
console.log($('to, from, heading, body', xml));
Change History
comment:2 follow-up: ↓ 3 Changed 3 years ago by dmethvin
- Status changed from new to closed
- Resolution set to invalid
There is no signature of jQuery() that accepts an xml string.
comment:3 in reply to: ↑ 2 Changed 3 years ago by mekwall
Replying to dmethvin:
There is no signature of jQuery() that accepts an xml string.
Well I also stated normal html, not only xml. I've also noticed that it removes head as well as seen here: http://jsfiddle.net/mekwall/s2K2x/
Is there a reason for this behavior?
Edit: Also it's removing the html-element. What if you would want to access the attributes of these elements?
comment:4 Changed 3 years ago by dmethvin
- Keywords needsdocs added
- Priority changed from undecided to low
- Component changed from unfiled to manipulation
I've marked this ticket needsdocs to have this paragraph from .load() converted to an "Additional Notes" entry and added to all applicable methods.
jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser. -- http://api.jquery.com/load/
comment:5 follow-up: ↓ 6 Changed 2 years ago by florian.assmann@…
When using a CUSTOM tag Instead of DIV the content still includes HTML, HEAD and BODY tags. Cheers Florian
comment:7 Changed 2 years ago by addyosmani
- Keywords needsdocs removed
Updated: http://api.jquery.com/jQuery/
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

For the lazy, jsfiddle test case here: http://jsfiddle.net/mekwall/xKEqa/