Topic: Material select - How to select value from jquery

daniel-aymemediasoft-sa-ch premium asked 5 years ago


my source :

<div class="col" >
<div class="md-form">
    <i class="fa fa-balance-scale prefix grey-text"></i>
    <select id="EtatCivilEmployeur" class="mdb-select" required>
        <option value="1">Célibataire</option>
        <option value="2">Concubin(e)</option>
        <option value="3">Divorcé(e)</option>
        <option value="4">inconnu</option>
        <option value="5">Marié(e)</option>
        <option value="6">Partenariat enregistré</option>
        <option value="7">Séparé(e)</option>
        <option value="8">Veuf(ve)</option>
    </select>
    <div id="EtatCivilEmployeur_ERR" class="invalid-feedback">Veuillez saisir votre état civil</div>
</div>

$('#EtatCivilEmployeur').material_select('destroy'); $('#EtatCivilEmployeur').material_select(); $('#EtatCivilEmployeur').val("2");

I don't understand why the command $('#EtatCivilEmployeur').val("2"); doesn't work.

If someome can help me to find the problem. Thanks in advance. Best regards.


Magdalena Dembna staff answered 5 years ago


<select id="select" class="browser-default custom-select custom-select-lg mb-3">
      <option selected>Open this select menu</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
</select>
<script>
    $(document).ready(() => $(select).val("2"));
</script>

I've tested the above code and it seems to work fine, it selects the second option. Is your jQuery code wrapped in $(document).ready()?
Please share your jQuery code snippet.



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: Premium
  • Premium support: Yes
  • Technology: MDB jQuery
  • MDB Version: 4.8.2
  • Device: PC
  • Browser: Chrome
  • OS: Windows
  • Provided sample code: No
  • Provided link: No