Gaurav
Gaurav

Reputation: 8487

jquery accessing all elements who have id attribute

I want to get all elements from a div who have id attribute, means i want to do something like:

$("div").find("*[id]").each(function() { alert(this.id) });

but this is not working , can anybody help me pls?

Upvotes: 3

Views: 3003

Answers (1)

gdoron
gdoron

Reputation: 150313

Your code works just fine, but you can remove the * from the selector.

Other valid options:

$("div").find("[id]").each(function() { alert(this.id) });

LIVE DEMO

Or this:

$("div *").filter("[id]").each(function() { alert(this.id) });

Live DEMO

Or this:

$("div [id]").each(function() { alert(this.id) }); // which I think is the best

Live DEMO

Upvotes: 8

Related Questions