Want to fetch data from input and construct in JSON












0














I am trying to fetch the data which is stored in a table in a JSON format, but while it is using the .each to fetch data, then the latest value is getting repeated all the time.



I have shown the actual output where it should have different data from the table rows



function getPackTableDetails() {
var packDetailsRow = ;
var tableRowCount = $('#addPackTable tr').length;
var returnJSONFormat = "";
$('#addPackTable tr').each(function(index) {
if(index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function(idx) {
switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}
});
// console.log(JSON.stringify(packDetailsRow));
return packDetailsRow;
}


As i am using a input type text where i use value to store my data and then make it retrieve to the following jquery code



Output:
{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
}
]
}


Actual Output:



{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "20.00",
"tax_amount": "10.00",
"total_amount": "30.00"
},
{
"table_pack_name": "Demo Pack Name6",
"table_pack_type": "Demo Pack Name6",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name7",
"table_pack_type": "Demo Pack Name7",
"base_amount": "400.00",
"tax_amount": "40.00",
"total_amount": "440.00"
}
]
}









share|improve this question






















  • gj for not providing the html, makes it that challenging
    – WASD
    Dec 26 at 13:41










  • If I understand your code right, code like $('#table_pack_name').val() will get the same value for every iteration.
    – obfish
    Dec 26 at 13:41










  • yes @obfish you are right. my iteration is getting repeated
    – Kiran Bhagannavar
    Dec 26 at 13:47
















0














I am trying to fetch the data which is stored in a table in a JSON format, but while it is using the .each to fetch data, then the latest value is getting repeated all the time.



I have shown the actual output where it should have different data from the table rows



function getPackTableDetails() {
var packDetailsRow = ;
var tableRowCount = $('#addPackTable tr').length;
var returnJSONFormat = "";
$('#addPackTable tr').each(function(index) {
if(index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function(idx) {
switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}
});
// console.log(JSON.stringify(packDetailsRow));
return packDetailsRow;
}


As i am using a input type text where i use value to store my data and then make it retrieve to the following jquery code



Output:
{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
}
]
}


Actual Output:



{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "20.00",
"tax_amount": "10.00",
"total_amount": "30.00"
},
{
"table_pack_name": "Demo Pack Name6",
"table_pack_type": "Demo Pack Name6",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name7",
"table_pack_type": "Demo Pack Name7",
"base_amount": "400.00",
"tax_amount": "40.00",
"total_amount": "440.00"
}
]
}









share|improve this question






















  • gj for not providing the html, makes it that challenging
    – WASD
    Dec 26 at 13:41










  • If I understand your code right, code like $('#table_pack_name').val() will get the same value for every iteration.
    – obfish
    Dec 26 at 13:41










  • yes @obfish you are right. my iteration is getting repeated
    – Kiran Bhagannavar
    Dec 26 at 13:47














0












0








0







I am trying to fetch the data which is stored in a table in a JSON format, but while it is using the .each to fetch data, then the latest value is getting repeated all the time.



I have shown the actual output where it should have different data from the table rows



function getPackTableDetails() {
var packDetailsRow = ;
var tableRowCount = $('#addPackTable tr').length;
var returnJSONFormat = "";
$('#addPackTable tr').each(function(index) {
if(index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function(idx) {
switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}
});
// console.log(JSON.stringify(packDetailsRow));
return packDetailsRow;
}


As i am using a input type text where i use value to store my data and then make it retrieve to the following jquery code



Output:
{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
}
]
}


Actual Output:



{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "20.00",
"tax_amount": "10.00",
"total_amount": "30.00"
},
{
"table_pack_name": "Demo Pack Name6",
"table_pack_type": "Demo Pack Name6",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name7",
"table_pack_type": "Demo Pack Name7",
"base_amount": "400.00",
"tax_amount": "40.00",
"total_amount": "440.00"
}
]
}









share|improve this question













I am trying to fetch the data which is stored in a table in a JSON format, but while it is using the .each to fetch data, then the latest value is getting repeated all the time.



I have shown the actual output where it should have different data from the table rows



function getPackTableDetails() {
var packDetailsRow = ;
var tableRowCount = $('#addPackTable tr').length;
var returnJSONFormat = "";
$('#addPackTable tr').each(function(index) {
if(index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function(idx) {
switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}
});
// console.log(JSON.stringify(packDetailsRow));
return packDetailsRow;
}


As i am using a input type text where i use value to store my data and then make it retrieve to the following jquery code



Output:
{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
}
]
}


Actual Output:



{
"pack_details": [
{
"table_pack_name": "Demo Pack Name5",
"table_pack_type": "Demo Pack Name5",
"base_amount": "20.00",
"tax_amount": "10.00",
"total_amount": "30.00"
},
{
"table_pack_name": "Demo Pack Name6",
"table_pack_type": "Demo Pack Name6",
"base_amount": "200.00",
"tax_amount": "0.00",
"total_amount": "200.00"
},
{
"table_pack_name": "Demo Pack Name7",
"table_pack_type": "Demo Pack Name7",
"base_amount": "400.00",
"tax_amount": "40.00",
"total_amount": "440.00"
}
]
}






javascript jquery






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 26 at 13:34









Kiran Bhagannavar

135




135












  • gj for not providing the html, makes it that challenging
    – WASD
    Dec 26 at 13:41










  • If I understand your code right, code like $('#table_pack_name').val() will get the same value for every iteration.
    – obfish
    Dec 26 at 13:41










  • yes @obfish you are right. my iteration is getting repeated
    – Kiran Bhagannavar
    Dec 26 at 13:47


















  • gj for not providing the html, makes it that challenging
    – WASD
    Dec 26 at 13:41










  • If I understand your code right, code like $('#table_pack_name').val() will get the same value for every iteration.
    – obfish
    Dec 26 at 13:41










  • yes @obfish you are right. my iteration is getting repeated
    – Kiran Bhagannavar
    Dec 26 at 13:47
















gj for not providing the html, makes it that challenging
– WASD
Dec 26 at 13:41




gj for not providing the html, makes it that challenging
– WASD
Dec 26 at 13:41












If I understand your code right, code like $('#table_pack_name').val() will get the same value for every iteration.
– obfish
Dec 26 at 13:41




If I understand your code right, code like $('#table_pack_name').val() will get the same value for every iteration.
– obfish
Dec 26 at 13:41












yes @obfish you are right. my iteration is getting repeated
– Kiran Bhagannavar
Dec 26 at 13:47




yes @obfish you are right. my iteration is getting repeated
– Kiran Bhagannavar
Dec 26 at 13:47












2 Answers
2






active

oldest

votes


















0














because you're assigning the same values every iteration



switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}





share|improve this answer























  • No it didnt work @WASD
    – Kiran Bhagannavar
    Dec 26 at 13:44










  • I trolled by accident
    – WASD
    Dec 26 at 13:46



















0














A few things to should follow is id should be unique to an element and no two elements should have same id. Check this for further reading. You can instead use name attribute to read values as below.



$('#addPackTable tr').each(function (index) {
if (index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function (idx) {
switch (idx) {
case 0:
packDetailsObj.table_pack_name = $(this).find('[name="table_pack_name"]').val();
break;
case 1:
packDetailsObj.table_pack_type = $(this).find('[name="table_pack_type"]').val();
break;
case 2:
packDetailsObj.base_amount = $(this).find('[name="base_amount"]').val();
break;
case 3:
packDetailsObj.tax_amount = $(this).find('[name="tax_amount"]').val();
break;
case 4:
packDetailsObj.total_amount = $(this).find('[name="total_amount"]').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}





share|improve this answer























  • I guess that selectors like 'td #table_pack_name' makes no sense.
    – obfish
    Dec 26 at 14:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53932821%2fwant-to-fetch-data-from-input-and-construct-in-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














because you're assigning the same values every iteration



switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}





share|improve this answer























  • No it didnt work @WASD
    – Kiran Bhagannavar
    Dec 26 at 13:44










  • I trolled by accident
    – WASD
    Dec 26 at 13:46
















0














because you're assigning the same values every iteration



switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}





share|improve this answer























  • No it didnt work @WASD
    – Kiran Bhagannavar
    Dec 26 at 13:44










  • I trolled by accident
    – WASD
    Dec 26 at 13:46














0












0








0






because you're assigning the same values every iteration



switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}





share|improve this answer














because you're assigning the same values every iteration



switch(idx) {
case 0:
packDetailsObj.table_pack_name = $('#table_pack_name').val();
break;
case 1:
packDetailsObj.table_pack_type = $('#table_pack_type').val();
break;
case 2:
packDetailsObj.base_amount = $('#base_amount').val();
break;
case 3:
packDetailsObj.tax_amount = $('#tax_amount').val();
break;
case 4:
packDetailsObj.total_amount = $('#total_amount').val();
break;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 26 at 13:46

























answered Dec 26 at 13:40









WASD

1646




1646












  • No it didnt work @WASD
    – Kiran Bhagannavar
    Dec 26 at 13:44










  • I trolled by accident
    – WASD
    Dec 26 at 13:46


















  • No it didnt work @WASD
    – Kiran Bhagannavar
    Dec 26 at 13:44










  • I trolled by accident
    – WASD
    Dec 26 at 13:46
















No it didnt work @WASD
– Kiran Bhagannavar
Dec 26 at 13:44




No it didnt work @WASD
– Kiran Bhagannavar
Dec 26 at 13:44












I trolled by accident
– WASD
Dec 26 at 13:46




I trolled by accident
– WASD
Dec 26 at 13:46













0














A few things to should follow is id should be unique to an element and no two elements should have same id. Check this for further reading. You can instead use name attribute to read values as below.



$('#addPackTable tr').each(function (index) {
if (index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function (idx) {
switch (idx) {
case 0:
packDetailsObj.table_pack_name = $(this).find('[name="table_pack_name"]').val();
break;
case 1:
packDetailsObj.table_pack_type = $(this).find('[name="table_pack_type"]').val();
break;
case 2:
packDetailsObj.base_amount = $(this).find('[name="base_amount"]').val();
break;
case 3:
packDetailsObj.tax_amount = $(this).find('[name="tax_amount"]').val();
break;
case 4:
packDetailsObj.total_amount = $(this).find('[name="total_amount"]').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}





share|improve this answer























  • I guess that selectors like 'td #table_pack_name' makes no sense.
    – obfish
    Dec 26 at 14:01
















0














A few things to should follow is id should be unique to an element and no two elements should have same id. Check this for further reading. You can instead use name attribute to read values as below.



$('#addPackTable tr').each(function (index) {
if (index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function (idx) {
switch (idx) {
case 0:
packDetailsObj.table_pack_name = $(this).find('[name="table_pack_name"]').val();
break;
case 1:
packDetailsObj.table_pack_type = $(this).find('[name="table_pack_type"]').val();
break;
case 2:
packDetailsObj.base_amount = $(this).find('[name="base_amount"]').val();
break;
case 3:
packDetailsObj.tax_amount = $(this).find('[name="tax_amount"]').val();
break;
case 4:
packDetailsObj.total_amount = $(this).find('[name="total_amount"]').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}





share|improve this answer























  • I guess that selectors like 'td #table_pack_name' makes no sense.
    – obfish
    Dec 26 at 14:01














0












0








0






A few things to should follow is id should be unique to an element and no two elements should have same id. Check this for further reading. You can instead use name attribute to read values as below.



$('#addPackTable tr').each(function (index) {
if (index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function (idx) {
switch (idx) {
case 0:
packDetailsObj.table_pack_name = $(this).find('[name="table_pack_name"]').val();
break;
case 1:
packDetailsObj.table_pack_type = $(this).find('[name="table_pack_type"]').val();
break;
case 2:
packDetailsObj.base_amount = $(this).find('[name="base_amount"]').val();
break;
case 3:
packDetailsObj.tax_amount = $(this).find('[name="tax_amount"]').val();
break;
case 4:
packDetailsObj.total_amount = $(this).find('[name="total_amount"]').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}





share|improve this answer














A few things to should follow is id should be unique to an element and no two elements should have same id. Check this for further reading. You can instead use name attribute to read values as below.



$('#addPackTable tr').each(function (index) {
if (index != 0) {
var packDetailsObj = {};
($(this).find("td")).each(function (idx) {
switch (idx) {
case 0:
packDetailsObj.table_pack_name = $(this).find('[name="table_pack_name"]').val();
break;
case 1:
packDetailsObj.table_pack_type = $(this).find('[name="table_pack_type"]').val();
break;
case 2:
packDetailsObj.base_amount = $(this).find('[name="base_amount"]').val();
break;
case 3:
packDetailsObj.tax_amount = $(this).find('[name="tax_amount"]').val();
break;
case 4:
packDetailsObj.total_amount = $(this).find('[name="total_amount"]').val();
break;
}
});
packDetailsRow.push(packDetailsObj);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 27 at 13:35

























answered Dec 26 at 13:51









Avi

588215




588215












  • I guess that selectors like 'td #table_pack_name' makes no sense.
    – obfish
    Dec 26 at 14:01


















  • I guess that selectors like 'td #table_pack_name' makes no sense.
    – obfish
    Dec 26 at 14:01
















I guess that selectors like 'td #table_pack_name' makes no sense.
– obfish
Dec 26 at 14:01




I guess that selectors like 'td #table_pack_name' makes no sense.
– obfish
Dec 26 at 14:01


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53932821%2fwant-to-fetch-data-from-input-and-construct-in-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas