HTML
<div class="row">
<div class="col-xs-5 stateName">Kuala Lumpur</div>
<div class="col-xs-7"><input id="1" placeholder="0.00" type="text"/></div>
</div>
<div class="row">
<div class="col-xs-5 stateName">Kuala Kangsar</div>
<div class="col-xs-7"><input id="2" placeholder="0.00" type="text"/></div>
</div>
MY JS
var stateArr = [],
tempObj = {};
$('.stateName').each(function(){
tempObj.id = $(this).next('div').find('input').attr('id');
tempObj.name = $(this).text();
stateArr.push(tempObj);
});
console.log(stateArr)
My result were all Kuala Kangsar , any idea what's wrong in my loop? I thought I did the loop right as I push the object into the array in the loop?
var tempObj = {};inside looptempObjcreation into your loop. As it is you have one object that you are just changing the values on.$('.stateName').each(function(){ tempObj = {}; ... });