clear all select2 options
Asked Answered
P

2

5

I can't find on select2 documentation a way to remove all the options in a select2 form. The old stckoverflow answers to this questions seem not to work since the new version (I'm using 4.0.3) has a different behavior

Perfumer answered 27/6, 2017 at 8:24 Comment(0)
G
16

Just

$('#id').empty().trigger("change");

Where '#id' is the jQuery selector for your select2 element.

Grizel answered 12/12, 2017 at 9:58 Comment(2)
Sorry, I sent it without finishing the last sentence. Is it ok now?Grizel
Sorry … don't seems to work on 4.1.0-RC1 maybe an RC issueDispermous
H
0

Previous answer will delete all values, but in the official documentation it says:

$("#id").val(null).trigger("change");

Hessian answered 14/6, 2018 at 22:18 Comment(1)
It will only clear the selected items not to remove the options.Springhalt

© 2022 - 2024 — McMap. All rights reserved.