Topic: Set selected Value not working

cestradawiboo pro asked 6 years ago


Good day to all, I need to select a value of a basic select but I can not get it to work, I have tried with the following code: <select class=\"mdb-select\" id=\"sexo\">                                 <option value=\"\" disabled selected>Seleccion tipo de Sexo</option>                                 <option value=\"1\">Masculino</option>                                 <option value=\"2\">Femenino</option> </select> i want to select value \"2\" $(‘.select-dropdown li:contains(“2”)’).trigger(‘click’); ---> not working
$(\"#sexo\").val(\'2\'); --> not working
I appreciate your comments

Mikołaj Smoleński staff answered 6 years ago


  Hello, You can change input value using the following code: HTML:
<select class=”mdb-select”>
<option value=””disabled selected>Choose your option</option>
<option value=”1″>Option 1</option>
<option value=”2″>Option 2</option>
<option value=”3″>Option 3</option>
</select>
<label>Example label</label>
<button id=”change-value” data-value=”Option 2″ class=”btn btn-primary”>Change value to Option 2</button>
JS:
$(document).ready(function() {
  $(‘.mdb-select’).material_select();
  $(‘#change-value’).on(‘click’, function(){
    let newValue = $(this).attr(‘data-value’);
    $(‘.select-dropdown’).val(newValue);
  })
});
Regards


Please insert min. 20 characters.

FREE CONSULTATION

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

Status

Answered

Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: Yes
  • Provided link: No
Tags