How do I store data in array within a loop?
var images;
var i = 0;
$('#cover div').each(function()
{
alert($(this).attr('id'));
//I should store id in an array
});
<div id="cover">
<div id="slider_1"><p class="content">SLIDER ONE</p></div>
<div id="slider_2"><p class="content">SLIDER TWO</p></div>
<div id="slider_3"><p class="content">SLIDER THREE</p></div>
</div>
Try this,
var arr = [];
i = 0;
$('#cover div').each(function()
{
alert($(this).attr('id'));
arr[i++] = $(this).attr('id');
//I should store id in an array
});
other Way for getting id using javascript object instead of jquery for increasing performance.
var arr = [];
i = 0;
$('#cover div').each(function()
{
arr[i++] = this.id;
});
Edit You can also use jQuery map()
Live Demo
arr = $('#cover div').map(function(){
return this.id;
});
javascript Arrays have a method push(el) like this:
var images;
var i = 0;
$('#cover div').each(function()
{
alert($(this).attr('id'));
images.push($(this).attr('id'));
});
<div id="cover">
<div id="slider_1"><p class="content">SLIDER ONE</p></div>
<div id="slider_2"><p class="content">SLIDER TWO</p></div>
<div id="slider_3"><p class="content">SLIDER THREE</p></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With