Reputation: 145
Sample HTML code:
<body>
<div id="div1">
<ul id="tabs_nav">
<li id="t_00">data1</li>
<li id="t_01">data2</li>
<li id="t_02">data3</li>
<li id="t_03">data4</li>
</ul>
</div>
<div id="div2">
<ul id="tabs_nav">
<li id="t_04">data5</li>
<li id="t_05">data6</li>
<li id="t_06">data7</li>
<li id="t_07">data8</li>
</ul>
</div>
</body>
Here I would like to get the all the id values in a array. As expected I should get the total id count should be 12 and able to read all the values in array.
Please help me, how to perform the above action using Jquery
?
Upvotes: 1
Views: 63
Reputation: 74420
You can map it:
$(function(){
var arrIds = $('body [id]').map(function(){
return this.id;
})/*.get()*/; // get() to get a true array
console.log(arrIds); // outputs: ["div1", "tabs_nav", "t_00", "t_01", "t_02", "t_03", "div2", "tabs_nav", "t_04", "t_05", "t_06", "t_07"]
console.log(arrIds.length); // outputs: 12
});
Upvotes: 0
Reputation: 28465
You can achieve the above using following code
var arr = [];
$( "*" ).each(function(){
var id = $(this).attr("id");
if(id !== undefined) {
arr.push(id);
}
});
For reference - http://plnkr.co/edit/3oHvyhuH5UQPv0IAYEXD?p=preview
Upvotes: 1