Example 1 — Official Demo: List div[id]
Select every div that has an id, then list those ids (official idea).
var ids = $("div[id]").map(function () {
return this.id || "(empty)";
}).get().join(", ");
console.log(ids);
console.log("count:", $("div[id]").length); How It Works
- Limit to divs. The
divtag keeps the search focused. - Require an id.
[id]keeps only nodes that have the attribute. - Map values. Reading
this.idlists which ids were found.
![Decorative image with the word [NAME]](/images/jquery-selectors.webp)
