Display selected values in multiselect


Topic: Display selected values in multiselect

Ott Kask asked 5 years ago

Hi! I'm using material design multiselect for submitting form. Since it seems to me 'selected' attribute is not working here, what would be the easiest method to iterate through the array of database values and change options to 'selected' accordingly.

{
key: [ "option1", "option3"]
}

<select class="mdb-select md-form" id="exampleID" name="key" multiple>
          <option value="" disabled selected><%= loopArray(user.key) %></option>
          <option id="option1" value="option1">option1</option><!–– selected ––>
          <option id="option2" value="option2">option2</option>
          <option id="option3" value="option3">option3</option><!–– selected ––>
          <option id="option4" value="option4">option4</option>
          <option value="Not specified">Not specified</option>
          <label class="mdb-main-label">Choose option</label>
        </select>
      <button type="button" class="btn-save btn btn-primary btn-sm">Save</button>

Thanks!


Sebastian Kaczmarek staff pro premium answered 5 years ago

To make the option selected, you need to add selected attribute to the <option>. It seems to work for me. Please see my snippet: https://mdbootstrap.com/snippets/jquery/s-kaczmarek/1367004

Demo:


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