Bug Tracker

Opened 17 years ago

Closed 16 years ago

Last modified 16 years ago

#48 closed bug (duplicate)

Seach scope does not work as expected with content loaded via $.get

Reported by: [email protected] Owned by:
Priority: minor Milestone:
Component: ajax Version:
Keywords: Cc:
Blocked by: Blocking:


I tried to load content from another file via $.get, find an element inside that loaded content and put that inside the current document. eg. $.get("url", function(content){

var content = $("#stuff", content).html(); do something with content


In my case, the id to search for is already in the document. As a result, $("#stuff", content).html() returns the content of #stuff inside the current document, not from the content loaded via get. It looks like a bug with the search scope. I put up an example page to demonstrate the problem:


Click the link to bug2.html to see what should be loaded into the red box.

Change History (8)

comment:1 Changed 17 years ago by john

Milestone: 1.0
Version: 1.0

comment:2 Changed 17 years ago by john

Priority: criticalminor
Type: bugenhancement

This is especially tricky, considering that XML documents don't support the ID selector naturally - or innerHTML, for that matter. I'm going to file this as an enhancement and deal with it someday.

comment:3 Changed 16 years ago by [email protected]

Type: enhancementbug

I've noticed this happening with element selectors as well:

$.get("url", function(content){

var content = $("title", content).text(); do something with content


this code obtains the current document's title instead of the retrieved document's title.

comment:4 Changed 16 years ago by [email protected]

Priority: minormajor

Actually, these alerts don't even give you the same result; the first one displays the requested page's markup, while the second one displays the current page's markup.

$.get("url", function(xml){

alert(xml); alert($(xml).html());


comment:5 Changed 16 years ago by john

Milestone: 1.0
Priority: majorminor
Type: bugenhancement
Version: 1.0

There are two different problems discussed in this report - the one originally posted about and the one supplied by frohoff. I'll talk about both.

1) Currently, this is not possible for the simple reason that XML documents, unless you've specified an explicit DTD, don't have an getElementById selector. Both #foo and .html() fail on non-HTML-DOM documents. This is a known issue, but one that probably won't be addressed until post-1.0.

It is for this reason that I'm going to re-classify this as an enhancement.

2) The second issue is with the content-type of the document. You must make sure that the document being returned from the server has the correct content type associated with it (explicitly, Content-type: text/xml). If, for some reason, you cannot change the header, and it is wrong, try this instead:

$.get("url", callback, "xml")

and it will try to parse it as an XML document instead.

comment:6 Changed 16 years ago by anonymous

Resolution: wontfix
Status: newclosed

comment:7 Changed 16 years ago by john

Resolution: wontfix
Status: closedreopened

comment:8 Changed 16 years ago by john

Resolution: duplicate
Status: reopenedclosed
Type: enhancementbug
Note: See TracTickets for help on using tickets.