Alternatives to htmx:prompt for Confirmations in htmx


  1. User Interaction
    When the user clicks on the element, htmx displays the prompt message.

  2. htmx:prompt Event
    After the user interacts with the prompt (either confirms or cancels), the htmx:prompt event fires. This event provides details about the prompt interaction.

  • detail.target: This indicates the target of the AJAX request associated with the element.
  • detail.elt: This refers to the specific element that triggered the prompt (the button or link you defined with hx-prompt).
  1. Handling the Event
    You can listen for the htmx:prompt event using JavaScript. Within the event handler, you can access the detail object to check if the user confirmed the prompt (detail.elt.confirmed would be true) and then decide whether to proceed with the AJAX request.

By using this event, you can control what happens after the user interacts with the prompt, allowing for more dynamic and user-friendly confirmation flows in your htmx applications.



HTML

<button hx-get="/delete/item/1" hx-prompt="Are you sure you want to delete this item?">
  Delete Item
</button>

JavaScript

htmx.on("htmx:prompt", function(evt) {
  if (evt.detail.elt.confirmed) {
    console.log("User confirmed deletion!");
    // Proceed with the AJAX request (already defined with hx-get)
  } else {
    console.log("User canceled deletion.");
  }
});
  • If canceled, another message is logged.
  • If confirmed, a message is logged and the AJAX request (to delete the item) proceeds.
  • It checks if the user confirmed the deletion (evt.detail.elt.confirmed).
  • The JavaScript code listens for the htmx:prompt event.
  • It also has hx-prompt="Are you sure you...?" which sets the confirmation message.
  • The button has hx-get="/delete/item/1" which defines the AJAX request to be triggered on click.

This example shows customizing the prompt behavior based on user confirmation.

HTML

<form hx-post="/submit-data">
  <input type="text" name="data">
  <button hx-prompt="Confirm submission?" hx-target="#result">
    Submit
  </button>
</form>
<div id="result"></div>
htmx.on("htmx:prompt", function(evt) {
  if (evt.detail.elt.confirmed) {
    // Submit the form data (already defined with hx-post)
    console.log("Data submitted!");
    evt.detail.target.textContent = "Data successfully submitted!";
  } else {
    console.log("Submission canceled.");
    evt.preventDefault(); // Prevent the form submission
  }
});
  • If canceled, submission is prevented using evt.preventDefault().
  • If confirmed, the form is submitted and a success message is displayed in the target element (#result).
  • The JavaScript code listens for htmx:prompt.
  • The button has hx-prompt="Confirm submission?" and hx-target="#result" for displaying the submission status.
  • This example uses a form to submit data.


  1. hx-confirm Attribute

This built-in htmx attribute provides a simpler approach for confirmation prompts. It uses the browser's default confirm dialog box to display the message before the AJAX request.

<button hx-get="/delete/item/1" hx-confirm="Are you sure?">Delete Item</button>

This achieves a similar confirmation flow as htmx:prompt but without the need for custom JavaScript logic.

  1. JavaScript confirm Dialog
<button onclick="return confirm('Are you sure you want to delete?')">Delete Item</button>
// Inside your JavaScript code
function deleteItem() {
  if (confirm('Are you sure you want to delete?')) {
    // Make the AJAX request to delete the item
  } else {
    // User canceled deletion
  }
}

This approach gives you more control over the dialog's appearance and behavior but requires more manual coding compared to htmx:prompt.

  1. Custom Modal Components

If you need a more visually appealing and interactive confirmation experience, you can create custom modal components using libraries like Bootstrap or vanilla JavaScript. This allows for richer user interaction with the confirmation process.

  • htmx:prompt offers a middle ground, allowing for custom logic based on user confirmation while keeping the interaction within the htmx framework.
  • If you want complete control over the dialog or require more complex interaction, use JavaScript's confirm function or build custom modal components.
  • If you need a simple confirmation without extensive customization, hx-confirm is a good option.