Is it possible to set the only first element on span in for loop?

Is it possible to set the only first element on span in for loop?



I want to set only first element on the span in for loop. Everything is working fine in the code but it's setting the last element on the span. I want to set the first element as cust_name.


cust_name


success : function(data)

console.log(data);

var jsonData = jQuery.parseJSON(JSON.stringify(data));

for (var i = 0; i <= jsonData.categories.length; i++)
var counter = jsonData.categories[i];

var cust_id = counter.cust_id;
var cust_name = counter.cust_name;
var cust_phone = counter.cust_phone;

$('input[name="customer"]').val(cust_id);
$('#customernames').text(cust_name);

if (cust_phone=="")

$("#dropselect-demo1").append('<li onclick="setcustomer('' +cust_id+ '','' +cust_name + '',''+cust_phone + '')" class="list-group-item">'+cust_name+'</li>');


else
$("#dropselect-demo1").append('<li onclick="setcustomer('' +cust_id+ '','' +cust_name + '',''+cust_phone + '')" class="list-group-item">'+cust_name+' [ '+cust_phone+' ]</li>');











Can you provide sample of html code which this uses. Also consider making it a runnable "script" - see editing toolbar.
– gorn
Aug 24 at 5:09




1 Answer
1



You can try


var cust_name = jsonData.categories[0].cust_name;
$('#customernames').text(cust_name);



As the first entry would be at 0 index in your array. Currently what happens is that it sets the text while you loop through and ends up with setting the last item in the array.


0





Thanks manoz for your help... is it possible to refresh this span everytime i call ajax
– Hitesh
Aug 24 at 5:20





how do you want a refresh?
– Manoz
Aug 24 at 5:21





Actually this span is working on a modal. so after select once i reopen that modal it should start again...
– Hitesh
Aug 24 at 5:28





are you using bootstrap modals?
– Manoz
Aug 24 at 6:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế