Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  145] [ 1]  / answers: 1 / hits: 15912  / 12 Years ago, sun, june 24, 2012, 12:00:00

I want to add a new row to my JSON when the user clicks a link. Here's my javascript: It's not erroring, but I am not getting updated JSON in my alert.



$(document).ready( function(){

people = {
COLUMNS:[NAME,AGE],
DATA:[
[Jon,16],
[Jerry,23]
]
}

members = people.DATA;
var nc = <table border=1 width=500><tr><td>name</td><td>age</td><td></td></tr>;

for(var i=0;i<members.length;i++)
{
nc+= '<tr><td>' + members[i][0] + '</td>';
nc+= '<td>' + members[i][1] + '</td>';
nc+= '<td><a href= class=addlink>add a new person</a></td></tr>';
}

nc += </table>;

$(#result).html(nc);

$(.addlink).click( function(){

// add another row to our JSON
people.DATA['NAME'] = new;
people.DATA['AGE'] = 99;

alert(people.DATA);
return false;

});
});

More From » jquery

 Answers
15

That's not JSON, it's a Javascript object.



To add another item in the array, you create an array and add to it, as it is an array of arrays:



people.DATA.push([new, 99]);

[#84693] Friday, June 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
allanw

Total Points: 421
Total Questions: 132
Total Answers: 102

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
;