Reputation: 845
problem
I need the return value from xhr request to be like 1,2,3
so that I can append comma separated value in textbox.
I tried but its only appending like 1, only.
input data:
9037566,
9037579,
9037577,
9030724,
9019686,
but output data should be like for example
1,2,3,4,5
json data as input
{ "keyword_search":
{"data":
[
{"JOB_NO":"9037566"},
{"JOB_NO":"9037579"},
{"JOB_NO":"9037577"},
{"JOB_NO":"9030724"},
{"JOB_NO":"9019686"}
]}}
js code i tried with
$.ajax({
url: root_url + 'mylogs/keyword',
cache: false,
type: "POST",
data: {'data': keyword},
dataType: 'json',
success: function (html) {
var keyword=html.keyword_search.data;
$.each(keyword, function (key, value){
console.log(value.JOB_NO.split(',')+',');
but when I tried to append $("#id").val(value.JOB_NO.split(',')+',');
it only appending like
9037566,
i help is most appreciated.
Upvotes: 1
Views: 188
Reputation: 103348
You are calling console.log()
for each iteration of the loop, and therefore you will get a seperate output for each piece of data.
Change the following:
success: function (html) {
var keyword=html.keyword_search.data;
$.each(keyword, function (key, value){
console.log(value.JOB_NO.split(',')+',');
To:
success: function (html) {
var keyword=html.keyword_search.data;
var jobNos = keyword.map(function(i){
return i.JOB_NO;
});
console.log(jobNos.join(","));
Simplified example here: https://jsfiddle.net/Lscewr1v/
Upvotes: 2