htmx.ajax, HtmxAjaxHelperContext typedef requires target, specfying source element #2698
Unanswered
anentropic
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hey, I'm the one who wrote this JSDoc and you're right, this must have been an oversight, it's completely optional as htmx uses the regular hx-target as a fallback: Line 4062 in 6c2f9ea I also agree that the Feel free to open a documentation fix PR if you feel like it! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using
htmx.ajax
in an event handler to load a new page.I want the request to inherit the
hx-*
properties of the ancestor element like it would do if I was making it via anhx-get
attribute.The third arg to
htmx.ajax
accepts acontext
object with various properties... e.g.target
,select
andswap
have the same effect as settinghx-target
,hx-select
,hx-swap
attrs on the element or its ancestors.It's not really spelled out in the docs but I also found that setting
source
element seems to allow the ajax request to inherit thehx-*
properties (for examplehx-push-url="true"
) of its ancestor - 👍 exactly what I wanted, rather than having to pass them down into my component in order to send them in the ajax context.So I have an event handler that looks something like
(e) => htmx.ajax("get", url, {event: e, source: e.target})
and it works as I want.The only issue I have is when working in TypeScript, the third argument is typed as a
HtmxAjaxHelperContext
. The definition is like:So this type makes
target
a required field. But I don't want to specify thetarget
, I want it to inherit thehx-target
from the ancestor ofsource
.And if I just ignore/workaround the typedef and pass
{event: e, source: e.target}
as my context then it seems to work fine.So I'm asking if perhaps
HtmxAjaxHelperContext
typedef should make all fields optional?Beta Was this translation helpful? Give feedback.
All reactions