Use Fragment Identifier in hx-get
etc. to Conveniently Specify hx-select
#3066
TheDocTrier
started this conversation in
Ideas
Replies: 1 comment 4 replies
-
Here is a contrived example which demonstrates the behavior, each button selects part of the page using the URL fragment identifier: <!DOCTYPE HTML>
<html>
<head>
<script src="https://unpkg.com/htmx.org@2.0.3" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>
<script>
htmx.defineExtension('get-select', {
onEvent: function(name, evt) {
if (name === 'htmx:beforeProcessNode') {
var elt = evt.detail.elt;
var a = elt.attributes["hx-get"] || elt.attributes["hx-post"];
if (a) {
var h = new URL(a.value, window.location).hash;
if (h) {
elt.setAttribute("hx-select", h);
}
}
}
}
});
</script>
<style>
#red {
border: red solid 4px;
padding: 1ch;
}
#both {
border: black solid 4px;
padding: 1ch;
}
</style>
</head>
<body hx-ext="get-select">
<div id="red">
<button hx-get="#red" hx-trigger="click" hx-target="closest div">
Nest RED!
</button>
</div>
<div id="both">
<button hx-get="" hx-trigger="click" hx-target="closest div">
Nest both!
</button>
</div>
</body>
</html> Edit: demonstrate both positive and negative case. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For example,
would be equivalent to
Would this conflict with any current behavior? The essay on Template Fragments references some Java code which appears to do exactly this:
Discussion #1055 mentions that Unpoly selects the
main
tag by default, which seems to be a more limited version of this idea. I think the general behavior I described above would make it even easier to use HTMX with existing endpoints and in static sites which may have difficulty specifyingHX-Reselect
.Beta Was this translation helpful? Give feedback.
All reactions