25 lines
599 B
JavaScript
25 lines
599 B
JavaScript
export function loadHtmxFormsBehaviour(){
|
|
document.querySelectorAll(".form").forEach(el => {
|
|
initHtmxForm(el);
|
|
})
|
|
|
|
}
|
|
|
|
function initHtmxForm(el){
|
|
el.addEventListener("htmx:responseError", (e) => {
|
|
el.querySelector(".form-errors").innerHTML = e.detail.xhr.response;
|
|
});
|
|
|
|
const formEl = el.querySelector("form");
|
|
|
|
if(!formEl.getAttribute("hx-redirect")){
|
|
return;
|
|
}
|
|
el.addEventListener("htmx:afterOnLoad", (e) => {
|
|
if(e.detail.successful){
|
|
return window.location.href = formEl.getAttribute("hx-redirect");
|
|
}
|
|
});
|
|
|
|
}
|