I have some <select> that needs to be populated with dynamic values, coming from an array. My code is pretty simple, the HTML is made by just some empty HTML <select> with the same class (.js-select).
The JS is quite simple:
var $select = $(".js-select"); var ioSensors = [1,2,3]; // The data I want to display in the select var $optionTpl = $("<option></option>"); for( i=0 ; i<ioSensors.length ; i++ ){ //also show a leading "None" option if(i === 0){ $optionTpl.attr('value','').text('None').appendTo($select); } $optionTpl.attr('value', ioSensors[i]-1).text(ioSensors[i]).appendTo($select); } With this code I'm having all my <select> correctly updated, but the last one is populated only with the last value of the Array, not showing even the "None" option.
Can anyone help me understand where the problem is and why is it behaving like that? Thanks!
I've made a pen Here