0

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...

asked Apr 15, 2015 at 14:52

1 Answer 1

2

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()
 }); 
answered Apr 15, 2015 at 14:55
3
  • Damn, got it before me :D Exactly the same as what I was going to write. Commented Apr 15, 2015 at 14:56
  • This is what I get when I use push correctly http://imgur.com/ITAmazn Commented Apr 15, 2015 at 15:00
  • The result shows me that you still used jsonData.row.push = ... instead of jsonData.row.push(...) Commented Apr 15, 2015 at 15:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.