Skip to main content

Bug Tracker

Side navigation

#12882 closed bug (notabug)

Opened November 11, 2012 06:14PM UTC

Closed November 11, 2012 06:48PM UTC

Last modified November 11, 2012 07:51PM UTC

DOM traversal predictably breaks when $ instantiated with a string of HTML

Reported by: Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.2
Keywords: Cc:
Blocked by: Blocking:

When passing the jQuery object a HTML string, DOM traversal works unexpectedly.

// the result for these is "undefined."  if the first two were standalone documents that loaded in
// jquery, the result would not be "undefined."
console.log($('<html><body><div id="test">test</div></body></html>').find('body').html());
console.log($('<html><body><div id="test">test</div></body></html>').find('#test').html());
console.log($('<div id="test">test</div>').find('#test').html());

// the result to these is correct.  the only addition was adding a plain wrapper div.
console.log($('<html><body><div><div id="test">test</div></div></body></html>').find('#test').html());
console.log($('<div><div id="test">test</div></div>').find('#test').html());
Attachments (0)
Change History (3)

Changed November 11, 2012 06:48PM UTC by dmethvin comment:1

resolution: → notabug
status: newclosed

See the docs and ask for help on the forum if you need it:

Changed November 11, 2012 07:47PM UTC by comment:2

i don't understand how this is not a bug. is it a feature to work incorrectly? thanks for the helpful comments on why exactly this is not a bug, when it works the opposite of logic. go ahead, test it out

Changed November 11, 2012 07:51PM UTC by dmethvin comment:3

"When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, we use the browser's .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <html>, <title>, or <head> elements. As a result, the elements inserted may not be representative of the original string passed."