m33bo
m33bo

Reputation: 1354

Select all HTML ID's as a jQuery Object

Is it possible with jQuery to select all "IDS" of a specific block at the same time? I know that you can specify for example $('div') but I would like to select the ID's in my object. Is there an easy way to do this in jQuery? Something like:

$object = $('.wrapper');

$object.find(function(){

//GET ALL THE IDS..somehow?! 

});

Upvotes: 1

Views: 128

Answers (2)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107536

I'm a little curious of its purpose, but you could try this:

var ids = $('[id]', $object).map(function() {
    return this.id;
});

It uses $object to provide the context for your selector, finds all elements within said context that have an id attribute, and then builds an array of the id values.

FYI, the resulting ids variable is a jQuery array-like object. If you just want a plain JS array, add a .get() after the map function:

var ids = $('[id]', $object).map(function() {
    return this.id;
}).get();
// ^

jsFiddle Demo

Upvotes: 10

moonwave99
moonwave99

Reputation: 22817

$object.find('[id]').each(function(){
  //this.id is your man
});

Upvotes: 2

Related Questions