5

I have to form a JSON object like

var SelectedRows=   {
        "item1":{"id":"1","name":"jhon","phone":"6699"},
        "item2":{"id":"2","name":"Aron","phone":"7799"},
        "item2":{"id":"3","name":"Iyan","phone":"8899"},
        }

On check box click event, I need to add the grid row values to the JSON list. I am trying with this code:

var SelectedRows={};

$(this).delegate(":checkbox", "click", function() {        
    var j=0;
    var item=[];
    SelectedRows[item]={};        *//I guess this line creating problem*     
    $(this).closest("tr").find("td:visible").each(function(){            
        var key=headerRow[j]["i"];            
        if(j == 0)
        {              
        }
        else
        {   
            SelectedRows[item][key]=$(this).text(); 
        }
        j=++j; 
     });

After multiple checkbox click events happening,SelectedRows contains only last click event data.

How get all the checkboxes click events data?

5
  • This is a funny JSON object you're trying to create. Why not use an array instead of adding elements with dummy keys item1, item2, item3 etc.? And why does each item consist of an array with just a single element? Commented Jan 1, 2013 at 9:12
  • var item[]; is not valid Javascript, what are you trying to do with that? Commented Jan 1, 2013 at 9:15
  • @Barmar, It's typo error, i edited that. Commented Jan 1, 2013 at 9:20
  • Now you're using an undefined variable item. Commented Jan 1, 2013 at 9:22
  • @Codo, I am using these keys(id,name,phone) on server side, so i decide to create json object. above json object format might be wrong(dummy keys item1,item2, item 3 etc).I need JSON object like asp.net list. Commented Jan 1, 2013 at 9:27

3 Answers 3

2

You can create an json array like this

var SelectedRows=   [
    item1:{id:"1",name:"jhon",phone:"6699"},
    item2:{id:"2",name:"Aron",phone:"7799"},
    item2:{id:"3",name:"Iyan",phone:"8899"}
]
Sign up to request clarification or add additional context in comments.

Comments

1

replace var item[]; by var item = 'item'+(j+1); and result should like..

var SelectedRows=   {
    "item1":{"id":"1","name":"jhon","phone":"6699"},
    "item2":{"id":"2","name":"Aron","phone":"7799"},
    "item2":{"id":"3","name":"Iyan","phone":"8899"},
    }

Comments

0

I done like this. it is displaying exactly above format.

var SelectedRows=[];
var rowCount=1;

Used extra count variable to from item1, item2, item3 etc..

    $(this).delegate(":checkbox", "click", function() {
        var j=0;
        var item={};    
            $(this).closest("tr").find("td:visible").each(function(){
               var key=headerRow[j]["i"];
                   if(j == 0){ }
                   else {
                          item[key]=$(this).text();
                        }
               j=++j; 
            });
            SelectedRows["item"+rowCount]=item;
            rowCount=++rowCount;
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.