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>');
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.
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