I have a JSON object that i'm try to add to. Below is the code i'm using (the commented out bit also didn't seem to do what I need) :
$('#classes tbody').on('click', 'tr', function () {
$(this).toggleClass('selected');
if ($.isEmptyObject(jsonData)) {
jsonData = {
"row": [
{
"OpenClosed": $('tr.selected td:nth-child(1)').text(),
"Section": $('tr.selected td:nth-child(2)').text(),
"CRN": $('tr.selected td:nth-child(3)').text(),
"CreditHours": $('tr.selected td:nth-child(4)').text()
}
]
};
}
else if (!$.isEmptyObject(jsonData)) {
jsonData['row'] = {
"OpenClosed": $('tr.selected td:nth-child(1)').text(),
"Section": $('tr.selected td:nth-child(2)').text(),
"CRN": $('tr.selected td:nth-child(3)').text(),
"CreditHours": $('tr.selected td:nth-child(4)').text()
};
//jsonData.row.push = {
// "OpenClosed": $('tr.selected td:nth-child(1)').text(),
// "Section": $('tr.selected td:nth-child(2)').text(),
// "CRN": $('tr.selected td:nth-child(3)').text(),
// "CreditHours": $('tr.selected td:nth-child(4)').text()
//};
}
});
The user clicks on a row and I need it to add that row where class=selected
to the JSON object. Currently it appears to append (in Chrome Dev Tools) instead of add a new row. When I selected two rows I end up with this CRN: "8063780639"
where the CRNs should be 80637 and 80639 on separate rows. I need each click to add a row
like row[0], row[1], row[2] etc...
1 Answer 1
push is not a property, it is a method. So you have to call it using (...)
jsonData.row.push({
"OpenClosed": $('tr.selected td:nth-child(1)').text(),
"Section": $('tr.selected td:nth-child(2)').text(),
"CRN": $('tr.selected td:nth-child(3)').text(),
"CreditHours": $('tr.selected td:nth-child(4)').text()
});
-
Damn, got it before me :D Exactly the same as what I was going to write.BobbyDazzler– BobbyDazzler04/15/2015 14:56:07Commented Apr 15, 2015 at 14:56
-
This is what I get when I use push correctly http://imgur.com/ITAmaznJrow– Jrow04/15/2015 15:00:12Commented Apr 15, 2015 at 15:00
-
The result shows me that you still used
jsonData.row.push = ...
instead ofjsonData.row.push(...)
devnull69– devnull6904/15/2015 15:05:58Commented Apr 15, 2015 at 15:05