Topic: Selected show impossible
                  
                  Scal68
                  pro
                  asked 6 years 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 ?
                      
                      Tomek Makowski
                      staff
                        answered 6 years 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();
    }); 
  });
                    
                      
                      
                      Scal68
                      pro
                        answered 6 years 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.
                      
                      Tomek Makowski
                      staff
                        answered 6 years ago
                    
Here you have the snippet. Check it out:
https://mdbootstrap.com/snippets/jquery/tomekmakowski/915138?view=project#js-tab-view
best
                      
                      Scal68
                      pro
                        answered 6 years 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

                      
                      Tomek Makowski
                      staff
                        answered 6 years 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 6 years 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>
                    
                      FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Pro
 - Premium support: No
 - Technology: MDB jQuery
 - MDB Version: 4.8.4
 - Device: php jquery ajax
 - Browser: firefox
 - OS: win 10
 - Provided sample code: No
 - Provided link: No
 
Bartłomiej Malanowski staff commented 6 years ago
What's your code? How can we reproduce your issue?