2

I'm making a new web app and I need to dinamically create and populate new bootstrap-select objects with jQuery and asyncronous calls.

I've tried to use .selectpicker('val', ) and .selectpicker().val() methods without success. I also tried this: How to set selected value on select using selectpicker plugin from bootstrap If I use the same code at page loading or in Firefox console it works.

function getOre (select)
{
   ...
   //per ogni ora del giorno
   for (i=0; i<data.length; i++)
   {
      var ora = '<option class="new_ora_options" value='+data[i].id+'>'+data[i].inizio+'</option>';
      select.append(ora);
   }
   ...
}

...
var sel = "<select class='selectpicker ora' id='ora_1'></select>";
$('.table').append(sel);
id_ora = "#new_ora_1";
$(id_ora).selectpicker({noneSelectedText:"Scegli la prenotazione",});
getOre(id_ora);
$(id_ora).selectpicker('refresh');
$(id_ora).selectpicker('val', 4);
....

Thanks

thomas alt
  • 41
  • 5
  • Possible duplicate of [How to set selected value on select using selectpicker plugin from bootstrap](https://stackoverflow.com/questions/14804253/how-to-set-selected-value-on-select-using-selectpicker-plugin-from-bootstrap) – imvain2 Apr 02 '19 at 19:58
  • I've tried it but it doesn't work. If I use it in Firefox console all is fine (the same as selectpicker('val', val) ) but in JS function it doesn't work. – thomas alt Apr 02 '19 at 20:20
  • Is this the actual code that you are using? Because in all of the examples 'refresh' comes AFTER the selectpicker('val',4). – imvain2 Apr 02 '19 at 20:48
  • Yes, even if I tried to insert refresh after selectpicker ('val', 4) without success. – thomas alt Apr 02 '19 at 21:14

1 Answers1

2

After several several attempts I founded the problem: the asyncrounous call with select.append(ora) finished after $(id_ora).selectpicker('val', 4). I changed the code inserting the append instruction in the success function of the asyncronous call and all now all is working fine.

thomas alt
  • 41
  • 5