Add to select and display on screen

Asked

Viewed 38 times

0

I have a select:

<select class="form-control" id="dataInstalacao">
<option value="0/0" selected disabled>Quando podemos instalar?</option>
<option value="6/9">6/9</option>
<option value="7/9">7/9</option>
<option value="8/9">8/9</option>
<option value="9/9">9/9</option>                                                     
</select>

I use the following code to add more options :

var newOption = new Option("teste", data.id, false, false);
$('#dataInstalacao').append(newOption).trigger('change');

What happens is that when I inspect the element, the new option is there, but what is displayed on the screen is a list UL>LI and select has display:noneand that list does not contain the new item.

inserir a descrição da imagem aqui

How shall I proceed?

  • Wouldn’t it be simpler to do it this way? $('#dataInstalacao').append(new Option('Foo', 'foo', true, true));

1 answer

3


By the class of <ul> I can see that you are using a library to change the native selects. From what I saw is the Nice Select.

According to the documentation just call the function update for the plugin to update to <ul>.

Then your code would look something like this:

var newOption = new Option("teste", data.id, false, false);
$('#dataInstalacao').append(newOption).niceSelect('update');
  • That’s right, thank you!!!!

  • If the answer helped mark as correct so future visitors know that it worked.

  • I had not scored yet because did not give the time kkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.