jquery.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <section>
  2. <h2 id="events-public">
  3. Public jQuery events
  4. </h2>
  5. <h3>
  6. What events will Select2 trigger?
  7. </h3>
  8. <p>
  9. Select2 will trigger a few different events when different actions are taken using the component, allowing you to add custom hooks and perform actions.
  10. </p>
  11. <dl class="s2-docs-panels">
  12. <dt>change</dt>
  13. <dd>Triggered whenever an option is selected or removed.</dd>
  14. <dt>select2:close</dt>
  15. <dd>Triggered whenever the dropdown is closed.</dd>
  16. <dt>select2:closing</dt>
  17. <dd>Triggered before the dropdown is closed. This event can be prevented.</dd>
  18. <dt>select2:open</dt>
  19. <dd>Triggered whenever the dropdown is opened.</dd>
  20. <dt>select2:opening</dt>
  21. <dd>Triggered before the dropdown is opened. This event can be prevented.</dd>
  22. <dt>select2:select</dt>
  23. <dd>Triggered whenever a result is selected.</dd>
  24. <dt>select2:selecting</dt>
  25. <dd>Triggered before a result is selected. This event can be prevented.</dd>
  26. <dt>select2:unselect</dt>
  27. <dd>Triggered whenever a selection is removed.</dd>
  28. <dt>select2:unselecting</dt>
  29. <dd>Triggered before a selection is removed. This event can be prevented.</dd>
  30. </dl>
  31. <h3>
  32. Does Select2 include extra information in these events?
  33. </h3>
  34. {% include options/not-written.html %}
  35. <h3>
  36. How can I attach listeners for these events?
  37. </h3>
  38. {% highlight js linenos %}
  39. $('select').on('select2:select', function (evt) {
  40. // Do something
  41. });
  42. {% endhighlight %}
  43. {% include options/not-written.html %}
  44. <h3>
  45. What events does Select2 listen for?
  46. </h3>
  47. <p>
  48. Select2 will listen for the <code>change</code> event on the
  49. <code>&lt;select&gt;</code> that it is attached to. If you make any
  50. external changes that need to be reflected in Select2 (such as changing the
  51. value), you should trigger this event.
  52. </p>
  53. {% highlight js linenos %}
  54. $('select').val('US'); // Select the option with a value of 'US'
  55. $('select').trigger('change'); // Notify any JS components that the value changed
  56. {% endhighlight %}
  57. <h3>
  58. Can I trigger an event other than <code>change</code> to notify Select2 of changes?
  59. </h3>
  60. <p>
  61. It's common for other components to be listening to the <code>change</code>
  62. event, or for custom event handlers to be attached that may have side
  63. effects. Select2 does not have a custom event (like
  64. <code>select2:update</code>) that can be triggered other than
  65. <code>change</code>. You can rely on jQuery's event namespacing to limit
  66. the scope to Select2 though by triggering the <code>change.select2</code>
  67. event.
  68. </p>
  69. {% highlight js linenos %}
  70. $('select').val('US'); // Change the value or make some change to the internal state
  71. $('select').trigger('change.select2'); // Notify only Select2 of changes
  72. {% endhighlight %}
  73. <h3>
  74. What events can be prevented? How can I prevent a selection from being made?
  75. </h3>
  76. {% include options/not-written.html %}
  77. </section>