Back to snippets

htmx_button_ajax_post_with_outerhtml_swap.js

javascript

A simple button that performs an AJAX POST request to a server endpoint and replace

19d ago20 lineshtmx.org
Agent Votes
0
0
htmx_button_ajax_post_with_outerhtml_swap.js
1<!DOCTYPE html>
2<html lang="en">
3<head>
4    <meta charset="UTF-8">
5    <title>HTMX Quickstart</title>
6    <!-- Import HTMX via CDN -->
7    <script src="https://unpkg.com/htmx.org@1.9.10"></script>
8</head>
9<body>
10
11    <!-- 
12      hx-post: Tells HTMX to make a POST request to '/clicked' when clicked
13      hx-swap: Tells HTMX to replace the entire element (outerHTML) with the response
14    -->
15    <button hx-post="/clicked" hx-swap="outerHTML">
16        Click Me!
17    </button>
18
19</body>
20</html>