Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#12882 closed bug (notabug)

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

Reported by: [email protected] 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());

Change History (3)

comment:1 Changed 9 years ago by dmethvin

Resolution: notabug
Status: newclosed

See the docs and ask for help on the forum if you need it: http://api.jquery.com/jQuery/

comment:2 Changed 9 years ago by [email protected]

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 http://jsfiddle.net/8zvWX/

comment:3 Changed 9 years ago by dmethvin

"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."

-- http://api.jquery.com/jQuery/#jQuery2

Note: See TracTickets for help on using tickets.