Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allowClear gives "Uncaught TypeError: Cannot read property 'id' of undefined" when used on single select boxes with ajax load #3497

Closed
RabenErland opened this issue Jun 19, 2015 · 19 comments

Comments

@RabenErland
Copy link

We are using ajax to load data remotely to a single select box. It works just fine, but if allowClear is enabled (set to true) a javascript error is encountered in select2 when the clear mark is clicked in the select box - and the value is not cleared from the box.

The error is:
Uncaught TypeError: Cannot read property 'id' of undefined
S2.define.AllowClear._handleClear @ select2.js:1721
S2.define.Utils.Decorate.calledMethod @ select2.js:571
(anonymous function) @ select2.js:1681
n.event.dispatch @ jquery-2.1.4.min.js:3
n.event.add.r.handle @ jquery-2.1.4.min.js:3

@RabenErland RabenErland changed the title allowClear gives js-error when used on single select boxes with ajax load select2 4.0: allowClear gives js-error when used on single select boxes with ajax load Jun 19, 2015
@kevin-brown
Copy link
Member

I'm going to take a guess and say that you are not using allowClear with the placeholder option which triggers undefined behaviour on single selects.


Can you expand upon the issue you are facing by adding a few more pieces of information to your bug report?

  • What steps can be used to reproduce the issue?
  • What is the bug and what is the expected outcome?
  • What browser(s) and Operating System have you tested with?
  • Does the bug happen consistently across all tested browsers?
  • What version of jQuery are you using? And what version of Select2?
  • Are you using Select2 with other plugins?

The contributing guide contains a useful section with a few questions which can help us track down the bug and speed up the process of finding a fix for it. The creation of an isolated test case would also be useful.

@pgasidlo
Copy link

Select2: v4.0.0, jQuery: v1.11.1, Linux, Chrome

$ajax.select2({
  /* Added this */
  allowClear: true,
  ajax: {
    // from example with github repository search
  },
  // ...
  templateResult: formatRepo,
  templateSelection: formatRepoSelection
});

Clicking on clear icon raises exception, on:

Function: AllowClear.prototype._handleClear = function (_, evt)

Code:

this.$element.val(this.placeholder.id).trigger('change');

Why: this.placeholder is undefined

Workaround:

$ajax.select2({
  allowClear: true,
  /* Add this */
  placeholder: {
    id: "",
    placeholder: "Leave blank to ..."
  },
  ajax: {
    // from example with github repository search
  },
  // ...
  templateResult: function(item) {
    /* FIX */
    if (item.placeholder) return item.placeholder;
    return formatRepo(item);
  },
  templateSelection: function (item) {
    /* FIX */
    if (item.placeholder) return item.placeholder;
    return formatRepoSelection(item);
  }
});

@TimNZ
Copy link

TimNZ commented Jul 26, 2015

+1 for pgasidlo description, there is no check that this.placeholder is defined in AllowClear.prototype._handleClear

@kevin-brown
Copy link
Member

This is a duplicate of #3016, you can see the error if you add debug: true.

@ellej16
Copy link

ellej16 commented Jun 10, 2016

@pgasidlo , you're a lifesaver. +1 !

@AsyncLegs
Copy link

@pgasidlo Thank a lot buddy!

@karray
Copy link

karray commented Jan 16, 2017

using @pgasidlo's work around the initial placeholder will be overridden after clear

@akositey
Copy link

akositey commented Aug 8, 2017

Still having this issue in version 4.0.3, because the allowClear: true while placeholder is not set. Solution would be to set the placeholder even if its blank.

$(".js-example-placeholder-single").select2({
  placeholder: "",
  allowClear: true
});

or set it globally:
$.fn.select2.defaults.set( "placeholder", "" );

fumikito added a commit to tarosky/tscf that referenced this issue Oct 11, 2017
@glalwani2
Copy link

After following the above solution, when I am selecting an option after doing clear all once, I am getting two selected options : 1) which i have selected and 2) -Select-
Can anybody tell me why? I am using Select2 with AngularJs bindings

@alexweissman
Copy link
Contributor

@glalwani2 it may be a known bug. Please search the open issues.

@malas
Copy link

malas commented Nov 21, 2017

i can also confirm, that the bug is still here in v4.0.3.

@akositey proposed fix to add blank placeholder is a working workaround.

@alexweissman
Copy link
Contributor

Is this not documented anywhere on the site? Also, you know that the current version is 4.0.6-rc.0, right?

@malas
Copy link

malas commented Nov 21, 2017

@alexweissman i can confirm, that the same behaviour is in the version 4.0.6-rc.0 too.

and i did not ee anywhere mentioned that in case you use allowClear you must also provide placeholder option.

@alexweissman
Copy link
Contributor

Ok, I will reopen this so that I remember to update the docs.

@alexweissman alexweissman reopened this Nov 21, 2017
@alexweissman alexweissman changed the title select2 4.0: allowClear gives js-error when used on single select boxes with ajax load allowClear gives "Uncaught TypeError: Cannot read property 'id' of undefined" when used on single select boxes with ajax load Dec 14, 2017
@stale
Copy link

stale bot commented Mar 13, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Mar 13, 2019
@stale stale bot closed this as completed Mar 20, 2019
@ManuelHaas
Copy link

Bug still exists in 4.0.13!

@samadanc
Copy link

@ManuelHaas Can confirm to this. The bug still persists.

@vipertecpro
Copy link

vipertecpro commented Oct 1, 2020

Hello devs, this is my laravel web-pack info for select2
"select2": "^4.0.13",
Can we put some kind of check if element doesn't exist
select2

This is how i am using for multi select

import select2 from 'select2';
select2($);
 /**
     * Multiple Select
     * */
    $.fn.select2.defaults.set( "placeholder", "" );
    $(".form-select").select2({
        closeOnSelect: false,
        placeholder: "",
        multiple: true,
        allowClear: true,
    });

@robozb
Copy link

robozb commented Feb 25, 2022

The problem still persists
var data = [
{
id: 0,
text: 'enhancement'
},
{
id: 1,
text: 'bug'
},
{
id: 2,
text: 'duplicate'
},
{
id: 3,
text: 'invalid'
},
{
id: 4,
text: 'wontfix'
}
];

            $("#eventListKontener").select2({
                allowClear: true,
                data: data,
                width: "100%",
                placeholder: ""
            })

    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests