Selected show impossible

web
mobile

Topic: Selected show impossible

Scal68 pro asked 4 months ago

Hello, I just installed version 8.4 jquery and I can not display an option selected after a build of my select ajax. Can you help me ?


Bartłomiej Malanowski staff pro premium commented 4 months ago

What's your code? How can we reproduce your issue?


Scal68 pro answered 3 months ago

Hello Thanks for your help. If we take your example with the label and add selected to option 3, it does not appear at the top of the menu. Against if we remove the label it works. Is this normal?

 <select class="mdb-select md-form colorful-select dropdown-primary">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3" selected>Option 3</option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
</select>
<label class="mdb-main-label">Blue select</label>

TomekMakowski staff answered 3 months ago

Hi

Try this out

const val = $(".mdb-select option[selected]").val();
$(".mdb-select").prev().find("li").eq(val-1).click();

best, Tomek


Scal68 pro answered 3 months ago

Hello Thanks for your help. I still have the problem with the label that does not move places. I have placed your code in my jquery script. see the picture below

enter image description here


Scal68 pro answered 3 months ago

It works, I do not know why it did not work yesterday. thank you so much


TomekMakowski staff answered 3 months ago

You are welcome :D .


Scal68 pro answered 3 months ago

Hello I just found that if you put the select twice, the second selected does not work. So if there is more on the page, only the first works.


TomekMakowski staff answered 3 months ago

So then try this script

    $(document).ready(function () {
    $('.mdb-select').materialSelect();

    $(".mdb-select option[selected]").each(function(){
      let val = $(this).val();
      $(this).parents(".mdb-select").prev().find("li").eq(val-1).click();
    }); 
  });

Please insert min. 20 characters.
Status

Answered

Specification of the issue
  • User: Pro
  • Premium support: No
  • Technology: jQuery
  • MDB Version: 4.8.4
  • Device: php jquery ajax
  • Browser: firefox
  • OS: win 10
  • Provided sample code: No
  • Provided link: No