LoginSignup
0
0

More than 1 year has passed since last update.

Select2でプログラム的にタグを削除する

Last updated at Posted at 2022-08-13

はじめに

以前「Select2でプログラム的にタグを設定する」というネタをやったが、今回はその削除版。

環境

  • Select2 4.0.X

方法

全てのタグを削除するには、以下のように、設定されている全てのタグのidを取得し、それらに対してunselectイベントを設定すればよい。

function remove_data(){
    const selections = $("js-example-basic-multiple1").select2('data').map((element) => element.id);
	for(var i =0; i<selections.length;i++){
		$("." + id).select2("trigger", "unselect", {data: { id: selections[i] } });					
	}
}		

また、特定のタグのみを消したい場合、タグに対応するidに対してのみunselectイベントを設定すればよい。

0
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
0
0