Jquery gets each div div a child div and grabs information into an array
I have some html that looks like
<div id="main">
<div id="sub_main_1" class="sub_main">
<input type="text" class="sub_name_first" /><br />
<input type="text" class="sub_name_second" /><br />
</div>
<div id="sub_main_2" class="sub_main">
<input type="text" class="sub_name_first" /><br />
<input type="text" class="sub_name_second" /><br />
</div>
</div>
I would like to pull each sub_main divs information into an array in javascript. So far I have it like my jquery code
$('#main').find('.sub_main').each(
function() {
alert('hi');
});
A warning is just a test that should show hi twice. But that does not work. I also don't understand how I can store two inputs in a javascript array. Any help would be great! Thank,
+5
5 answers
why not just do something simple:
var firsts = [];
var seconds = [];
("#main .sub_main input").each(function(){
var $this = $(this);
if($this.is(".sub_name_first"){
firsts.push($this.val());
} else {
seconds.push($this.val());
}
});
Of course, this is not the best way, but I just wrote that after 1 minute it works
0