Max Kielland
Max Kielland

Reputation: 5841

How to filter a JQuery object?

I have created a dialogue and stored it in a variable. Later on I want to run some JQuery on this object, but I can't get it to work.

Here is an example on fiddle to try out, or check the example here:

var Dialog = $('<div title="Form"><p>Foo foo, bar foo bar bar.</p><input type="checkbox" id="foo"/><label for="foo">foo</label><input type="checkbox" id="bar"/><label for="bar">bar</label></div>').dialog({
    modal: true,
    autoOpen: false
});
$(Dialog).filter("input").button();
$(Dialog).dialog("open");

I want to run .button() on all input element in the Dialog variable befor I open the dialog. The result I get now is what you would expect if you didn't apply .button() at all.

If i try $(Dialog).filter("div") in the consol I get the div element, but if I try anything within the div, such as p or label I get nothing.

Upvotes: 0

Views: 59

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388406

You need to use .find() because you are looking for a descendant element, .filter() is used to reduce the matching set to a subset which satisfies the given criteria.

$(Dialog).find("input")

Demo: Fiddle

Upvotes: 3

Related Questions