How to programmatically set a value SELECT


Topic: How to programmatically set a value SELECT

Supplier Management pro asked 7 years ago

how to select the option value "H" without using the method $('.select-dropdown li:contains("H")')?PleaseBecause I have a lot of list to select with options that start with an H.

<form class="form-inline"><select class="mdb-select colorful-select dropdown-primary" id="selectGender"><option value="" disabled selected>Sexe</option><option value="H">Homme</option><option value="F">Femme</option></select></form>                                                                    

Mikołaj Smoleński staff answered 7 years ago

I suggest to try this:
$('#InitialManager').change(function () { 
  var values = $(this).val();
  console.log(values);
});
Regards

Mikołaj Smoleński staff commented 7 years ago

If You want to select options programmatically You just need to create an array with option values and put it inside val(...)

Mikołaj Smoleński staff commented 7 years ago

For example: optionValues = ['2', '3']; $('#InitialManager').val(optionValues); var values = $('#InitialManager').val(); console.log(values);

Mikołaj Smoleński staff answered 7 years ago

Hi, Try to fix it like this: $('#selectGender li:contains("H")') By using id instead of class You apply function to exactly one select Regards
Hello Mikolaj, thanks for the response. I already tried this--> $('#selectGender li:contains("H")').trigger('click'); But no change. Is there not another possibility to select the option?   <form class=”form-inline”> <select class=”mdb-select colorful-select dropdown-primary” id=”selectGender”> <option value=”” disabled selected>Sexe</option> <option value=”H”>Homme</option> <option value=”F”>Femme</option> </select> </form>  

Mikołaj Smoleński staff answered 7 years ago

My another idea is to fix it like this: $('[id=selectGender ]').val( "H" ); Regards
Thank you for your reply. the problem was that it needed to reset the Material_select .$ ( '. Mdb-select') material_select ('destroy'); $ ( "[Id = selectGender]") val ("H").trigger( 'click'); $ ( '.mdb-select.') Material_select (). I have another question, how to select multiple options in a select? I tried this, but it does not work. for (i = 0; i < data.InitialManager.length; i++) { $("[id=SelectHospital]").val(data.InitialManager[i].Id).trigger('click'); };   <form class="form-inline"> <select class="mdb-select colorful-select dropdown-primary" id="InitialManager" multiple > <option value="" disabled selected> Gestionnaire</option> <option value="1">option 1</option> <option value="2">option 2</option> <option value="3">option 3</option> <option value="4">option 4</option> } </select> </form>
Hello Mikolaj, Thank you for this solution! Thank you also for your support.
Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue
  • User: Pro
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: Yes
  • Provided link: No