Tuesday, 6 August 2013

Cannot push values with keys in an array using jQuery

Cannot push values with keys in an array using jQuery


I am trying to push some values with keys to a array and I want to fetch
the details with keys to show theme in the alert message;
Here is my code which I tried
Jsfiddle
Html
<table>
<tr class='itemIndex'>
<td class="id">18445</td>
<td class="firstName">Kakali</td>
<td class="occupation">Poultry farm</td>
<td class="sector">Business</td>
<td class="userInvestment">200</td>
</tr>
<tr class='itemIndex'>
<td class="id">18560</td>
<td class="firstName">Jamuna</td>
<td class="occupation">Handloom Work</td>
<td class="sector">Business</td>
<td class="userInvestment">300</td>
</tr>
<tr class='itemIndex'>
<td class="id">18562</td>
<td class="firstName">Champa</td>
<td class="occupation">Paddy processing</td>
<td class="sector">Business</td>
<td class="userInvestment">200</td>
</tr>
</table>
Script
$(document).ready(function(){
var cartItem = [{id:''},{name:''},{occpation:''},{sector:''},{amount:''}];
$('.itemIndex').each(function(){
var element = $(this);
var itemId= element.children('.id').text();
var firstName = element.children('.firstName').html();
var occupation = element.children('.occupation').html();
var sector = element.children('.sector').html();
var userInvestment =
element.children('.userInvestment').html();
cartItem.push[{'id':+itemId},{'name':+firstName},{'occpation':+occupation},{'sector':+sector},{'amount':+userInvestment}];
}).promise().done(function(){
for(var i=0;i<cartItem.length;i++)
{
alert(cartItem.id[i],cartItem.name[i],cartItem.occupation[i],cartItem.sector[i],cartItem.amount[i]);
}
});
});

No comments:

Post a Comment