-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
8 changed files
with
115 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
+++ | ||
title = "Reset user input" | ||
template = "demo.html" | ||
+++ | ||
|
||
This example shows how to easily reset user inputs using [`hx-on`](@/attributes/hx-on.md), | ||
allowing users to make multiple requests without having to manually delete their previous inputs. | ||
|
||
The inline script will run on the [`afterRequest`](@/events.md#htmx:afterRequest) event and ensures | ||
that the form will reset to its initial state as long as the response has a 20x status code: | ||
|
||
```html | ||
<form hx-post="/note" | ||
hx-target="#notes" | ||
hx-swap="afterbegin" | ||
hx-on::after-request="if(event.detail.successful) this.reset()"> | ||
<div class="form-group"> | ||
<label>Add a note</label> | ||
<input type="text" name="note-text" placeholder="blank canvas"> | ||
</div> | ||
<button class="btn">Add</button> | ||
</form> | ||
<ul id="notes"><!-- Response will go here --></ul> | ||
``` | ||
|
||
The `reset()` method is only available on `form` elements. | ||
For other elements, the input value can explicitly selected and reset to a default to achieve the same result. | ||
The following code is functionally equivalent: | ||
|
||
```html | ||
<div> | ||
<label>Add a note</label> | ||
<input id="note-input" type="text" name="note-text" placeholder="blank canvas"> | ||
</div> | ||
<button class="btn primary" | ||
hx-post="/note" | ||
hx-target="#note" | ||
hx-swap="afterbegin" | ||
hx-include="#note-input" | ||
hx-on::after-request="if(event.detail.successful) | ||
document.getElementById('note-input').value = ''"> | ||
Add | ||
</button> | ||
<ul id="notes"><!-- Response will go here --></ul> | ||
``` | ||
|
||
{{ demoenv() }} | ||
|
||
<script> | ||
|
||
//========================================================================= | ||
// Fake Server Side Code | ||
//========================================================================= | ||
|
||
// routes | ||
init("/demo", function(request) { | ||
return formTemplate(); | ||
}) | ||
|
||
onPost("/note", function(request, params) { | ||
var note = params['note-text']; | ||
if (note) { | ||
return `<li>${note}</li>`; | ||
} | ||
}) | ||
|
||
// templates | ||
function formTemplate() { | ||
return ` | ||
<form hx-post="/note" hx-target="#notes" hx-swap="afterbegin" hx-on::after-request="if(event.detail.successful) this.reset()"> | ||
<div class="form-group"> | ||
<label>Add a note</label> | ||
<input type="text" name="note-text" placeholder="blank canvas"> | ||
</div> | ||
<button class="btn primary">Add</button> | ||
</form> | ||
<ul id="notes"> </ul>`; | ||
} | ||
</script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{% extends "htmx-theme/templates/base.html" %} | ||
|
||
{% block title %} | ||
{% set html_title = "</> htmx ~ 404 Not found " %} | ||
{% endblock title %} | ||
|
||
{% block content %} | ||
<h1 style="margin-bottom: 500px;"> | ||
404 Not Found | ||
</h1> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters