Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hx-vals="js: { a: event.target.value }" errs #1103

Open
reedspool opened this issue Oct 28, 2022 · 4 comments
Open

hx-vals="js: { a: event.target.value }" errs #1103

reedspool opened this issue Oct 28, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@reedspool
Copy link

reedspool commented Oct 28, 2022

Here's an example of the issue https://codepen.io/reedspool-gh/pen/XWYbRXo

For some reason, if clicking multiple times quickly, hx-vals="js:{ test: event.target.value }"> causes an error, TypeError: Cannot read properties of undefined (reading 'target') at htmx.org:1:29917. I narrowed this down to the execution of Function(...)() in getValuesForElement.

If you click the button slowly, the count will appear and then increase. But if you click it twice quickly, an error appears and the HTMX no longer triggers upon clicking this button ever again.

I think this is two problems:

  • If there is an htmx error, I think that should never stop the htmx event from triggering on the same element again
  • The specific error that appears must be a bug, right?

I narrowed the bug down to the second click having an event value of undefined instead of an actual event as expected.

@reedspool reedspool changed the title hx-vals errs on event.target hx-vals="js: { a: event.target.value }" errs with TypeError: Cannot read properties of undefined (reading 'target')` Oct 28, 2022
@reedspool reedspool changed the title hx-vals="js: { a: event.target.value }" errs with TypeError: Cannot read properties of undefined (reading 'target')` hx-vals="js: { a: event.target.value }" errs with TypeError: Cannot read properties of undefined (reading 'target') Oct 28, 2022
@reedspool reedspool changed the title hx-vals="js: { a: event.target.value }" errs with TypeError: Cannot read properties of undefined (reading 'target') hx-vals="js: { a: event.target.value }" errs Oct 28, 2022
@jquesada2016
Copy link

I'm having a similar issue, except in my case event is always undefined.

@FeliciousX
Copy link

the delay causing the event to be undefined definitely caught me off-guard and had to spend some time debugging

@reedspool did u manage to work around it?

@maekoos
Copy link

maekoos commented Jan 16, 2025

I am currently having this issue myself, although I'm more interested in accessing the element hx-vals is on. It would be really nice to be able to use this like in hx-on...

This could probably be done with a simple .call(elt) here:

htmx/src/htmx.js

Line 3840 in d890547

varsValues = maybeEval(elt, function() { return Function('return (' + str + ')')() }, {})

I will add a PR if a maintainer thinks this is a good idea!

@Telroshan
Copy link
Collaborator

Sounds good to me @maekoos, feel free to dive into this and open a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants