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

Extension to customize hx-confirm? #434

Closed
BoPeng opened this issue Mar 23, 2021 · 5 comments
Closed

Extension to customize hx-confirm? #434

BoPeng opened this issue Mar 23, 2021 · 5 comments

Comments

@BoPeng
Copy link
Contributor

BoPeng commented Mar 23, 2021

hx-confirm is nice but window.confirm is a bit primitive. I am trying to write an extension for bootlox.confirm but not sure how to do it (e.g. which event to intercept and how to return confirmed status). Any suggestion on how I should proceed?

Something like htmx.config.confirmFunction would also be possible though.

@BoPeng
Copy link
Contributor Author

BoPeng commented Mar 23, 2021

I think a solution to this problem could be generalized to solve #401 (trigger of JS-based form validation before hx-post). That is to say, hx-confirm could

  1. get user confirmation from window.confirm (default)
  2. call a JS script function to get user confirmation (this ticket)
  3. call a JS script to validate the form (Support hx-trigger condition valid #401)

basically a generalized way to determine if hx-post should be triggered.

@jreviews
Copy link
Contributor

What if you re-define the confirm function?

window.confirm = function(msg, callback) {
  // ...
 }

@BoPeng
Copy link
Contributor Author

BoPeng commented Mar 24, 2021

There are actually some stackoverflow discussions on overriding window.confirm, so in theory it should work. It also provides an ugly solution to #401, namely passing certain string to a customized confirm function to validate forms before hx-post is triggered.

But I suppose overriding a system function should be avoided if at all possible.

@1cg
Copy link
Contributor

1cg commented Mar 24, 2021

I had an old example done in intercooler that used events for something like this:

https://intercoolerjs.org/examples/bootstrap_confirm.html

It can probably be ported to htmx pretty easily. Might be worth looking at.

@BoPeng
Copy link
Contributor Author

BoPeng commented Mar 24, 2021

Thanks for the information, which actually help answer one of my old questions, namely how to trigger hx-get or hx-post from JS code (#339). I will adapt the example for my case.

@BoPeng BoPeng closed this as completed Mar 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants