Skip to content

HX-Trigger response header target only targets first item #3422

@flew2bits

Description

@flew2bits

I'm trying to trigger the same named event on multiple targets from an HX-Trigger response header. I'm sending back something like the following:

HX-Trigger: {"refresh": {"target": "#target-1,#target-2"}}

I've also tried the following variations:

HX-Trigger: {"refresh": [{"target": "#target-1"},{"target": "#target-2"}]}
HX-Trigger: {"refresh": {"target": ".target-shared-class"}}

What I ultimately found is that once htmx determines the target, either as the triggering element or the element named in target, the code that resolves that target DOM element calls .querySelector, which is why it only gets the first item in the list.

Is there any reason this couldn't be modified to be .querySelectorAll and use .forEach to process the event on all of the returned results?

My workaround at this point is I have another event handler that takes a targets selector and an event name and calls the trigger functionality on each of them, but it seems like this should work out-of-the-box.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions