select2: Uncaught TypeError: Cannot read property 'current' of null
$('select.popLang').on('change', function(){
$('select.popLang').select2("destroy");
var sel = $('select.popLang'),
iAr = [];
sel.children('option').attr('disabled',false);
if (sel.hasClass('noEng')) {
sel.find('option[value="39"]').attr('disabled',true);
}
$.each(sel, function(){
var itm = $(this).find('option:selected').val();
iAr.push(itm);
});
$.each(iAr, function(key, val){
if (isNum(val)) {
sel.find('option[value="'+val+'"]').attr('disabled', true);
}
});
$('select.popLang').select2();
});
Running this code the first time returns without error. However, after that it returns the error Uncaught TypeError: Cannot read property 'current' of null starting with r.handle in jQuery. Other than this error appearing, everything works. What could be the problem?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 28 (2 by maintainers)
Commits related to this issue
- text is null then return empty string — committed to rubyist-liu/select2-rails by rubyist-liu 7 years ago
Replace: $(“select”).change(function () { … }); with: $(“select”).on(“select2:select select2:unselecting”, function () { … }); It worked for me with no errors.
@kevin-brown How many users need to write “I am also experiencing this” before you can confirm it’s a bug?
I use something like this and that error was gone, just wrap it using
setTimeoutGuys, I have figured out the workaround and the issue in my case.
I am using select2 in a react wrapper and the problem appears because during
onChangecallback, the component would be re-rendered andSelect2didn’t like that.The fix for me was to add
setTimeoutto offset the input manipulation (even though it was properly destroyed and re-initialised).I didn’t get enough time to look into this issue. But this error seems to be not affecting anything for me. So I have added a simple null check to avoid this error. I have attached the patch.
select2-v4.0.2.patch.zip
@kevin-brown and @invot FWIW, I’m running into this exact same issue with v4.0.2 of select2.
Hello, I am facing same issue in select2 4.0.3 (select2.full.js)
i am resolved this issue using below fix.
Replace - Line 4808
With -
This patch is working for me in * Select2 4.0.3
Replace ‘change’ subscription with specific ‘change.select2’ helped in my case
@Sofoklis1 Thank you so much, you solved my issue 4 years later!
I’m experiencing the same issue and @sgrgala 's solution fixed it for me as well. IMO his patch should be added to the project – I honestly can’t see any downside for that, but on the upside it will resolve an issue that many of us experience – even if it’s not easy to reproduce.
I’m also having this problem and @sgrgala’s patch also fixes it for us. It would be great for this to be merged into select2. Nice work @sgrgala 😃
Thanks @sgrgala your patch works like a charm
I got this issue when I tried to update the data of the select 2 on change of the same select.(it was a multiselect, not sure if that makes any difference.)
I guess the issue lies in that the select is not finished doing something before it starts with updating the data??
the above “solutions” didn’t fix it, or caused some other issues.
I solved the problem by changing select2:select select2:unselecting to select2:select select2:selecting
@kevin-brown Here is the information: We are setting this.dataAdapter to null and we try to access it during change event and it’s not been initialized by then. I believe it is some kind of race condition and it triggers the change event before the initialization of dataAdapter which leads to an error.
A quick fix is to add a null check and we need more investigation on why dataAdapter is not initialized.
Another fix is that we can initialize the dataAdapter if it’s null and then proceed.
Please advise what can be the best possible approach.