123

Trying to use Select2 and getting this error on multiple item input/text field:

"query function not defined for Select2 undefined error"

13 Answers 13

251

Covered in this google group thread

The problem was because of the extra div that was being added by the select2. Select2 had added new div with class "select2-container form-select" to wrap the select created. So the next time i loaded the function, the error was being thrown as select2 was being attached to the div element. I changed my selector...

Prefix select2 css identifier with specific tag name "select":

$('select.form-select').select2();
Sign up to request clarification or add additional context in comments.

7 Comments

somehow fixed it for me, in my case I was cloning a row of form inputs which included select2-ified select menus and all sorts of weird stuff was happening after the first clone.
Also has to be inside $(document).ready(function() { $('select.form-select').select2()})
How can the same problem be solved in Angular UI Select2 directive?
Just came across this same problem today - although the same code previously had no issues (perhaps an update of select2?). Anyways, this solved it for me as well and working fine again. Great answer!
This problem usually happens if the select control has already been initialized by the .select2({}) method. A better solution would be calling the destroy method first. Ex: $("#mySelectControl").select2("destroy").select2({});
|
20

This error message is too general. One of its other possible sources is that you're trying to call select2() method on already "select2ed" input.

Comments

15

In case you initialize an empty input do this:

$(".yourelement").select2({
 data: {
  id: "",
  text: ""
 }
});

Read the first comment below, it explains why and when you should use the code in my answer.

2 Comments

I had this problem when I migrated a select2 box from an actual select element to a input type=hidden element that I was populating later. In stepping through the select2 code, this error is thrown because there is no query, ajax, data, or tags value passed in.
This should be the accepted answer for this question. Not sure why select2() won't simply just accept empty parameters
12

I also had this problem make sure that you don't initialize the select2 twice.

2 Comments

same for me, but the error wasn't a problem to me. s2 works anyway
how to do this?
7

For me this issue boiled down to setting the correct data-ui-select2 attribute:

<input type="text" data-ui-select2="select2Options.projectManagers" placeholder="Project Manager" ng-model="selectedProjectManager">


$scope.projectManagers = { 
  data: []  //Must have data property 
}

$scope.selectedProjectManager = {};

If I take off the data property on $scope.projectManagers I get this error.

Comments

5

This issue boiled down to how I was building my select2 select box. In one javascript file I had...

$(function(){
  $(".select2").select2();
});

And in another js file an override...

$(function(){
    var employerStateSelector = 
                $("#registration_employer_state").select2("destroy");
    employerStateSelector.select2({
    placeholder: 'Select a State...'
    });
});

Moving the second override into a window load event resolved the issue.

$( window ).load(function() {
  var employerStateSelector = 
              $("#registration_employer_state").select2("destroy");
  employerStateSelector.select2({
    placeholder: 'Select a State...'
  });
});

This issue blossomed inside a Rails application

Comments

4

I also got the same error when using ajax with a textbox then i solve it by remove class select2 of textbox and setup select2 by id like:

$(function(){
  $("#input-select2").select2();
});

Comments

3

It seems that your selector returns an undefined element (Therefore undefined error is returned)

In case the element really exists, you are calling select2 on an input element without supplying anything to select2, where it should fetch the data from. Typically, one calls .select2({data: [{id:"firstid", text:"firsttext"}]).

Comments

2

Also got the same error when using ajax.

If you're using ajax to render forms with select2, the input_html class must be different from those NOT rendered using ajax. Not quite sure why it works this way though.

1 Comment

You mean like, "<input type='hidden' class='foo' ...." where if you have multiple select2's, they can't all be 'foo'? This didn't work for me.
1
if (typeof(opts.query) !== "function") {
    throw "query function not defined for Select2 " + opts.element.attr("id");
}

This is thrown becase query does not exist in options. Internally there is a check maintained which requires either of the following for parameters

  • ajax
  • tags
  • data
  • query

So you just need to provide one of these 4 options to select2 and it should work as expected.

Comments

0

I got the same error. I have been using select2-3.5.2

This was my code which had error

    $('#carstatus-select').select2().val([1,2])

Below code fixed the issue.

    $('#carstatus-select').val([1,2]);

1 Comment

I would use $('#carstatus-select').select2("val", [1,2]); as mention in the docs
0

I have a complicated Web App and I couldn't figure out exactly why this error was being thrown. It was causing the JavaScript to abort when thrown.

In select2.js I changed:

        if (typeof(opts.query) !== "function") {
            throw "query function not defined for Select2 " + opts.element.attr("id");
        }

to:

        if (typeof(opts.query) !== "function") {
            console.error("query function not defined for Select2 " + opts.element.attr("id"));
        }

Now everything seems to work properly but it is still logging in error in case I want to try and figure out what exactly in my code is causing the error. But for now this is a good enough fix for me.

Comments

0

if you see this problem before like this "jquery.min.js:917 Uncaught ReferenceError: select2_dc80d1e9 is not defined. then finally i check all stuff and all results. So i come realizie maybe something wrong happyen in "/vendor/kartik-v/yii2-krajee-base/src/WidgetTrait.php" . As you can see in last plugin js. the $pos return 1. so i overwrite "$view->registerJs($js, $pos, $key);" to "$view->registerJs($js, View::POS_LOAD, $key);" OR "$view->registerJs($js, View::POS_END, $key);" problem resolved. hope you enjoy.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.