0

I would like to have Select2.js working with Django. There's django-select2 available for this but for the things I am doing, the author advises that I use native Select2.

I was referring to basic code example here https://select2.org/getting-started/basic-usage

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>

<!--Select2.js-->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
<select class="js-example-basic-multiple" multiple="multiple" name="lv0">
  <option value="US">US</option>
  <option value="UK">UK</option>
</select>
    <script type="text/javascript">
$(document).ready(function() {
    $('.js-example-basic-multiple').select2({
        placeholder: 'Select an Lv0'
    });
}); 
</script>

However, I only get below.

enter image description here

Console log below. I am not sure if it is because select2 files are not loaded? I added them in bower_components folder of the project, but can't successfully referring to them using local path as error shows file not found.

jquery.min.js:2 jQuery.Deferred exception: $(...).select2 is not a function TypeError: $(...).select2 is not a function
    at HTMLDocument.<anonymous> (http://localhost:8000/test3:45:37)
    at l (https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js:2:29375)
    at c (https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js:2:29677) undefined
w.Deferred.exceptionHook @ jquery.min.js:2
jquery.min.js:2 Uncaught TypeError: $(...).select2 is not a function
    at HTMLDocument.<anonymous> (test3:45)
    at l (jquery.min.js:2)
    at c (jquery.min.js:2)

What am I missing here? Thanks.

May Y
  • 179
  • 1
  • 20

1 Answers1

0

Just defer select2.

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js" defer></script>

Thanks to Select2() is not a function

May Y
  • 179
  • 1
  • 20