Search code examples
javascripthtmlhtmx

htmx: hx-trigger for a change event in a child element


I use htmx together with the Django forms library.

Here is my template:

<table>
 <tr hx-post="{{ object.get_absolute_url }}" hx-swap="outerHTML" 
     hx-trigger="changed">
  <th>{{ object.leg.start }}</th>
  <th>--&gt;</th>
  <th>{{ object.leg.end }}</th>
  <th>{{ object.leg.distance }}m</th>
  <th>{{ object.leg.difficulty_verbose }}</th>
  <td>{{ form.runner }} {{ form.runner.errors }}</td></tr>
</table>

Here is the created html:

<table>
 <tr hx-post="/leg/155/Talfreunde/ %}" hx-swap="outerHTML" hx-trigger="changed">
  <th>Schöneck</th>
  <th>--&gt;</th>
  <th>Mühlleithen</th>
  <th>13400m</th>
  <th>hard</th>
  <td>
    <select name="runner" required id="id_runner">
     <option value="">---------</option>
     ...
    </select>
  </td>
 </tr>
</table>

I want the <tr> to act like a form.

I tried to find a way to tell hx-trigger to listen for the change event of the <select>.

How to tell htmx to submit the data as soon as the select was changed?

Background: This is a Relayrace and each leg will be a row in the table.


Solution

  • You need to use the change event. The term changed does mean something different.

    <script src="https://unpkg.com/htmx.org@1.1.0"></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>