Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a select2 input that I'm using and on 'change' I'm grabbing the values and performing an action. I'm trying to clear that select2 without triggering a change event. Is this possible?

To clear the select2 I'm using the following code:

$docInput.select2('data', null);

This clears the input as desired, but also triggers a change event which runs through my other code. There must be a way to silence the trigger. Any ideas?

You could do a test in your onchange event and make sure that the input isn't empty before you do whatever it is you're doing in that function. – Jnatalzia Jul 23, 2013 at 19:53 @MikeFielden You're right, you need to explicitly pass false github.com/ivaynberg/select2/blob/… it is worth reporting a bug. – Fabrício Matté Jul 23, 2013 at 20:09 This works because it specifically triggering the change event with the namespace that Select2 is looking for (.select2), so other event handlers will not be triggered. This namespace might change in the future, but it's pretty unlikely. – Kevin Brown-Silva Oct 12, 2015 at 19:33 This is the only solution that worked without triggering the 'change' event on the original <select> elements. Thanks! – Edd Dec 15, 2015 at 15:25 $('.select_auto').on('select2:selecting', function(e) { When trigger change, above function should be trigger, but its not. I changed selecting to change but still not working – Wasim A. Jan 4, 2018 at 12:07

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.