0

How to assign single or multiple values to select2?

I use below:

<label for="input-fruits_liked">Assignment Method</label>
<select class="form-control select2" id="fruits_liked" name="fruits_liked" multiple="multiple">
     <option value="0">None</option>
     <option value="1">Orange</option>
     <option value="2">Apple</option>
     <option value="3">Grape</option>
     <option value="4">Banana</option>
</select>

<script type="text/javascript">
  $("#fruits_liked").val('@Model.fruits_liked').trigger('change');
</script>

I have tried the above, it returns Uncaught ReferenceError: $ is not defined

the value of model.fruits_like is something like the following:
fruits_like ": "[\"Apple\", \"Banana\"]

Anybody has experienced with this before?

Thank you so much in advance. Dd

FBDon
  • 23
  • 6
  • You have referenced the jQuery files? they should be referenced first before any script. – Gyanendra Singh Apr 07 '22 at 05:16
  • "Uncaught ReferenceError: $ is not defined" is a very common error and indicates jQuery isn't loaded at the time that code is running. If you search, you will find thousands of answers here on SO about that. – Don't Panic Apr 07 '22 at 05:17

0 Answers0