Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when specifying Select2 data

I'm specifying the data for a Select2 dropdown by using an external array but it keeps giving the following error. I'm using the latest Select2 v4.0.3 so I can't figure out what the problem is. Any help is appreciated. The relevant code is given below. Note that I tried using a <input> element and the code worked as expected.

select2.min.js:21 Uncaught Error: Option 'data' is not allowed for Select2 when attached to a <select> element.
    at String.<anonymous> (select2.min.js:21)
    at Function.each (jquery-1.11.2.js:384)
    at d.prepareOpts (select2.min.js:21)
    at d.prepareOpts (select2.min.js:22)
    at d.init (select2.min.js:21)
    at HTMLSelectElement.<anonymous> (select2.min.js:22)
    at Function.each (jquery-1.11.2.js:384)
    at jQuery.fn.init.each (jquery-1.11.2.js:136)
    at jQuery.fn.init.a.fn.select2 (select2.min.js:22)
    at HTMLDocument.<anonymous> (bio?id=3&action=display:773)

Here is the code.

HTML

<select id="dropdown" multiple="multiple" style="width:350px;" tabindex="2"></select>

JQuery

var arr = [{id:0, text: "red"}, {id:1, text: "blue"}]; 

$("#dropdown").select2({
        data: arr
})

As far as installation is concerned, all that I have done is copy the follwing files to my js folder and included this line <script src="js/select2.min.js"></script> in my html.

select2.js
select2.min.js
select2.full.js
select2.full.min.js
like image 878
samurdhilbk Avatar asked Aug 27 '26 10:08

samurdhilbk


1 Answers

Okay I found the error. I had included a select2.min.js file from a previous Select2 version(below v4.0) and that was overriding the actions of the newer version. When I removed that and just kept the newer version things worked fine.

like image 194
samurdhilbk Avatar answered Aug 30 '26 03:08

samurdhilbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!