Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var bookingitems = new Array();
var bookingitem = {};
$("[id*=gvBookingDt] tr").each(function () {
bookingitem.ItemNo = $(this).find("span[class*=ItemNo]").html();
bookingitem.ItemDes = $(this).find("td:nth-child(4)").html();
bookingitem.NoofPieces = $(this).find("td:nth-child(5)").html();
bookingitem.Weight = $(this).find("td:nth-child(6)").html();
bookingitems.push(bookingitem);
});


My Gridview contains two records:

itemNo Itemdesc noofpcs weight
1 mobile 12 20kg
2 laptop 13 50 kg

when i am trying to find array details
for(int i=0;i<=bookingitems.Count-1;i++){}
Getting olny last row value two times

What I have tried:

I have tried to put an alert inside (loop of gridview using jquery).In alert box,it's showing both two rows data,but why it's not able to push both item in arraylist.I am not getting any idea.

Any help.please......

Thanks in advance
Posted
Updated 22-Dec-16 19:30pm

1 solution

By placing this
var bookingitem = {};
outside of the "each" loop function (in fact any functions), it becomes a global object, the result of which is that the "each" loop always overwrites the properties of this same object.
You should put it inside the each function like this:
$("[id*=gvBookingDt] tr").each(function () { 
    var bookingitem = {};
    // ...

so that a new local object is created on each iteration.
 
Share this answer
 
v3
Comments
priya231 23-Dec-16 2:08am    
Thank u so much for your prompt reply.
priya231 23-Dec-16 2:09am    
Thank you so much for your prompt reply

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900