How can i send form by type into input using htmx?

I have a search input and sort select. I wrapped it into form. How can i send form by typing into search input (hx-trigger="keyup changed delay:150ms") or selecting another item in select (hx-trigger="changed"). I need to send data from both of this elements.
Here is my code:

<form hx-get="{% url 'search-spec' %}" hx-target="#search-results" hx-trigger="keyup delay:150ms changed">
    <div class="search w-100 mt-4 d-flex justify-content-center">
        <div class="input-group rounded w-75 shadow-4-soft">
            <span class="input-group-text me-2" id="search-addon">
                <i class="fas fa-search"></i>
            </span>
            <input type="search" class="form-control rounded" placeholder="Начните набирать..." name="q" id="q"/>
        </div>
    </div>
    <div class="search w-100 mt-2 d-flex justify-content-center">
        <span class="input-group-text me-2" id="search-addon">
            <i class="fas fa-sort"></i>
        </span>
        <select name="sort" id="sort" value="0" class="form-select w-50">
            <optgroup label="По дате публикации">
                <option value="0">Последние публикации</option>
                <option value="1" selected>Первые публикации</option>
            </optgroup>
            <optgroup label="По обозначению">
                <option value="2">Обозначение 0-9</option>
                <option value="3">Обозначение 9-0</option>
            </optgroup>
            <optgroup label="По наименованию">
                <option value="4">Наименование А-Я</option>
                <option value="5">Наименование Я-А</option>
            </optgroup>
        </select>
    </div>
</form>

>Solution :

You can define both hx tags seperately in the input and select and then use

hx-include="closest form"

in both, to include all field values from that form in the request.

This should also give you better control over hx-target and how you trigger changes, when you have them defined separately.

Ref:
https://htmx.org/attributes/hx-include/

Leave a Reply