rtheunissen
rtheunissen

Reputation: 7435

Find elements within a set of elements

At the moment I have something like

var inputs = $("form").find("input");

How can I do something like this?

var checkboxes = inputs.find("[type=checkbox]");

Thereby not having to traverse the entire form to find all checkboxes, because we know that all elements with type=checkbox will also be inputs -- which we've already collected.

Upvotes: 1

Views: 42

Answers (1)

Ram
Ram

Reputation: 144659

You are looking for the .filter() method:

var checkboxes = inputs.filter("[type=checkbox]");

The method also accepts a callback function:

var checkboxes = inputs.filter(function() {
   return this.type === 'checkbox';
});

Upvotes: 3

Related Questions