Laurent Laurent - 6 months ago 61
jQuery Question

Using jquery plugin select2 with bootstrap 3

I am trying to use

select2
, but I can't.

I am using the following code in my
<head>
section:

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" />
<link rel="stylesheet" type="text/css" href="select2-bootstrap.css">

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>


and this is my
HTML
and
JS
Code:

<script type="text/javascript">
$('locationMultiple').select2({
theme: "bootstrap"
});
</script>

<p>
<select class="locationMultiple form-control" multiple="multiple">
<option value="1d4h7g">Abondance (74360)</option>
<option value="lf9k9d">Alby-Sur-Cheran (74540)</option>
...
</select>
</p>


But my select desperately stays as multiple rows.

Can any one help me to know where and what I am doing wrong ?

Answer

This Code definitely work for you...

You need to write this code in Javascript:

$(document).ready(function () {
    $.fn.select2.defaults.set("theme", "bootstrap");
    $(".locationMultiple").select2({
         width: null
    })
});

Live Example : https://jsfiddle.net/rajnikpatel/1vje5rxo/