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

Fetch hx-vals programatically #2717

Open
metametadata opened this issue Jul 12, 2024 · 2 comments
Open

Fetch hx-vals programatically #2717

metametadata opened this issue Jul 12, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@metametadata
Copy link

I want to make an AJAX request from JS using some data from a server which is already in HTML in the hx-vals attribute.

There seems to be an internal API function getExpressionVars for this purpose. It could be convenient to make it public.

@MichaelWest22
Copy link
Contributor

That function is published via the internalAPI as you found. You can access it by creating your own simple custom htmx extension as these are trivial to create. The internalAPI is passed to the extension init command and you can store this for later use. You can probably then export this and start using it. But you can also maybe do an onEvent function as well that will intercept standard htmx issued events so when the user interacts with the element with your extension tagged on it you can perform your own custom Ajax request for your use case using all the internalapi helpers to make things simpler and more htmx compatible. So instead of having your own custom vanilla JS trying to do actions scoped to the whole page you can move it to a more locality of behavior model where your custom functionality only applies to the few page elements you tag with hx-ext="my-cust-hack"

You can also just put your ext on the body to apply everywhere and create your own custom event name and just listen for this name and use the internal api functions and trigger this event from your custom JS.

@metametadata
Copy link
Author

metametadata commented Jul 17, 2024

Thank you for the suggestions!

The internalAPI is passed to the extension init command and you can store this for later use

I guess it could work, but it would be a hack.

when the user interacts with the element with your extension tagged on it you can perform your own custom Ajax request for your use case using all the internalapi helpers

I don't think in my case creating a custom htmx extension would look like a natural design.

For now my workaround is to use invisible input tags instead of hx-vals and then collect all the data from the form via htmx.values helper. But this takes more code and doesn't allow dynamic JS calculations as in hx-vals.

P.S. If internalAPI is allowed to be used by consumers in their custom extensions then by definition this API is public and its contracts should not be broken between library versions.

P.P.S. I'm in general not a fan of over-protectiveness in third-party libraries. For me, as a consumer, it'd be more convenient to have access to private functions when I understand the risks and really need them. So for example, instead of hiding foo, I'd make it available to consumers, but add a prefix to signal it's a non-public API: _foo.

@Telroshan Telroshan added the enhancement New feature or request label Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants