efficiencyIsBliss
efficiencyIsBliss

Reputation: 3093

How do I use jQuery on an HTML document instead of the DOM?

I'm trying to modify an HTML document, but its not the same as the DOM. However, I found that the syntax that I used with the DOM itself doesn't work with the other document. For example, I could find the title using $("title") with the DOM. However, with the document I have to use doc.find("title"). There are other, more involved changes and I'm stuck on how to proceed with this. Is there a reference I can look at? I know about the jQuery documentation, but that assumes that you're manipulating the DOM itself.

Upvotes: 1

Views: 88

Answers (2)

Andri
Andri

Reputation: 1503

From what I've gathered the $ function assumes it's default scope for selectors is the active document. if you create jQuery objects that are not in the body of the document, the $(object).find(selector) is your best way to select within that object. The rest of the functions should work fine, at least the ones dealing with selecting, transversing and manipulation.

I should add that if the object is a jQuery object, there is no need to use the $. All the jQuery functions will be available to that object. So if you create:

var obj = $('<div>some text</div>');

you can use obj.addClass('cool) to get a jQuery object containing

<div class="cool">some text</div>

Upvotes: 2

JoshStrange
JoshStrange

Reputation: 1129

You can use jQuery on your doc by using the following

$("title", doc)

You pass the string "doc" to jQuery and it will use that instead of the DOM

Upvotes: 0

Related Questions