LoginSignup
1
0

More than 5 years have passed since last update.

select2 で allowClear オプションだけを指定したときのエラー回避法

Posted at

次のように allowClear オプションを指定すると

application.js
$('.js-searchable').select2({allowClear: true})

次のエラーがでます。

select2.js?86fe:1809 Uncaught TypeError: Cannot read property 'id' of undefined

この場合 placeholder オプションも一緒に指定するとエラーを回避できます。

毎回、引数に placeholder を指定してもいいのですが、次のようにグローバルな初期値として指定しておく方が安心でしょう。

$.fn.select2.defaults.set("placeholder", "")

参照

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0