Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.


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]);
                } 
            });    
});
share|improve this question
2  
alert() does not take multiple arguments. Use console.log() for debugging, not alert(). – Matt Ball 19 mins ago
add comment (requires an account with 50 reputation)

4 Answers

up vote 2 down vote accepted

do you mean something like this::

$(document).ready(function(){  
var cartItem = [];   
            $('.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();
                //alert(itemId);
                cartItem.push({'id':itemId,'name':firstName,'occupation':occupation,'sector':sector,'amount':userInvestment});
                //console.log( cartItem );
            }).promise().done(function(){
                 console.log( cartItem );
                for(var i=0;i<cartItem.length;i++) {
                    console.log(cartItem[i].id,cartItem[i].name,cartItem[i].occupation,cartItem[i].sector,cartItem[i].amount);
                } 
            });    
});

Updated Fiddle

share|improve this answer
there is no need to write the same code for each field/table cell, as class names are the same as assoc. array keys it make sense to use them. – vittore 9 mins ago
add comment (requires an account with 50 reputation)

I'm not sure what exactly you are doing , but consider this code (fiddle http://jsfiddle.net/vittore/vmszk/7/)

$(document).ready(function(){

var cart = []

$('.itemIndex').each(function() {
    var item = {}

    $(this).children().each(function() {
       item[this.className] = this.innerHTML;  
    })
    cart.push(item)        
})


$('pre').text(JSON.stringify(cart))


});
share|improve this answer
add comment (requires an account with 50 reputation)

There were lots of code errors as you are new to Javascript. I have fixed your errors and it is working.

Suggest you learn to use console.log instead of alert

$(document).ready(function() {   var cartItem = [];   $('.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
    });   })   for ( var i = 0; i < cartItem.length; i++) {
    alert(JSON.stringify(cartItem[i]));   } });

working fiddle: http://jsfiddle.net/vmszk/9/

share
add comment (requires an account with 50 reputation)
alert(cartItem.id[i],cartItem.name[i],cartItem.occupation[i],cartItem.sector[i],cartItem.amount[i]);

Is cartItem.id a array?

share|improve this answer
You can answer this question yourself by reading the code. – Matt Ball 15 mins ago
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.