Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 "change" event does not trigger htmx

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

like image 526
guettli Avatar asked Feb 20 '26 13:02

guettli


2 Answers

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'));
    });
});
like image 172
Dane Lewis Avatar answered Feb 22 '26 02:02

Dane Lewis


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>
like image 20
guettli Avatar answered Feb 22 '26 03:02

guettli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!