Select2 "change" event does not trigger htmx
Asked Answered
F

5

11

This works, the way I want it: If the <select> gets changed, then htmx gets triggered.

<script src="https://unpkg.com/[email protected]"></script>

<table>
 <tr hx-post="//example.com" hx-trigger="change">
  <td>
    <select name="runner">
     <option value="a">a</option>
     <option value="b">b</option>
    </select>
  </td>
 </tr>
</table>

If I use a django-autocomplete-light widget, then it does not work.

I use this version: django-autocomplete-light==3.8.1

Flora answered 10/1, 2021 at 20:57 Comment(1)
can you add "select" as an hx-trigger as well as the change?Solemn
F
4

If I add this JS, then it works. Better solutions are welcome.

<script>
 window.addEventListener("DOMContentLoaded", (e) => {
  $('select').on('select2:select', function (e) {
   $(this).closest('tr').get(0).dispatchEvent(new Event('change'));
});
 })
</script>
Flora answered 10/1, 2021 at 20:57 Comment(0)
T
13

Just come across this same issue, and fixed it with the following modified version of guettli's answer.

window.addEventListener("DOMContentLoaded", (e) => {
    $('select').on('select2:select', function (e) {
        $(this).closest('select').get(0).dispatchEvent(new Event('change'));
    });
});
Tripetalous answered 5/8, 2021 at 4:22 Comment(1)
I had to tigger the change event on the element with hx-trigger attribute.Turbulence
F
4

If I add this JS, then it works. Better solutions are welcome.

<script>
 window.addEventListener("DOMContentLoaded", (e) => {
  $('select').on('select2:select', function (e) {
   $(this).closest('tr').get(0).dispatchEvent(new Event('change'));
});
 })
</script>
Flora answered 10/1, 2021 at 20:57 Comment(0)
S
2

For me it worked when also adding htmx.onLoad() using bootstrap 5 styled Select2 https://apalfrey.github.io/select2-bootstrap-5-theme/:

htmx.onLoad(() => {
   $("#collection-select").select2({
      theme: "bootstrap-5",
      closeOnSelect: true
   });
});
window.addEventListener("DOMContentLoaded", (e) => {
   $('select').on('select2:select', function (e) {
      $(this).closest('select').get(0).dispatchEvent(new Event('change'));
   });
});
Shurlock answered 2/2, 2023 at 12:24 Comment(0)
M
2

What worked for me was to trigger the change event with the htmx javascript API trigger https://htmx.org/api/#trigger

window.addEventListener("DOMContentLoaded", (e) => {
  $('select').on('select2:select', function (e) {
    htmx.trigger($(this).closest('select').get(0), 'change')
  })
})
Mixer answered 12/7, 2023 at 11:27 Comment(0)
P
2

Hey there person struggling to get a multiple select pillbox style to work. You have to listen to both select2:select and select2:unselect!

Hey there person struggling to listen to the change event from a parent component because your select isn't where your hx-trigger is. You have to make sure you fire the new change event with {bubbles: true}.

Cheers to you, friend!

$(doc).ready(function() {
  $("select").select2({placeholder: "Search..."});
  $("select").on("select2:select select2:unselect", (e) => {
    e.currentTarget.dispatchEvent(new Event("change", {bubbles: true}));
  });
});

DOM

<table>
 <tr hx-post="//example.com" hx-trigger="change">
  <td>
    <select name="runner[]" multiple="multiple">
     <option value="a">a</option>
     <option value="b">b</option>
    </select>
  </td>
 </tr>
</table>
Piet answered 3/5 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.