Search code examples
jquerydjangodjango-autocomplete-light

Trying to open select2 on page load gives the error “The select2('open') method was called on an element that is not using Select2.”


Trying to open the select2 on page load using document.ready doesn't work because "autocomplete_light.js initialize function" is tied to same event and at document.ready, the componente isn't initialized yet.

So, doing as below doesn't work:

$(document).ready(function () {
    $('#select2_field').select2('open')
    //OR
    $('#select2_field').open()
});

The problem is that django autocomplete_light initializes the field on document.ready too. I need to open and focus the field on page load, same event. When I try to do so, it gives the error: The select2('open') method was called on an element that is not using Select2. If I access the method on another event, after document.ready, it works.

So, the question is, how can I open the select2 on page load, after it has been initialized by autocomplete light? Autocomplete light doesn't have any callback that I could use.


Solution

  • After posting an issue on DAL github page, I was guided to add the event notifying after the element initialization and open a pull request myself.

    I've done it and now I'm waiting for it to be accepted.

    https://github.com/yourlabs/django-autocomplete-light/pull/1246