You can use the hmpl-js package to load HTML from the server. It works on fetch, so it can help you avoid writing a bunch of code:
<script src=“https://unpkg.com/hmpl-js”></script>
<script>
const templateFn = hmpl.compile(
`<div>
<button class=”getHTML”>Get HTML!</button>
<request src=”/api/test” after=”click:.getHTML”></request>
</div>`
);
const wrapper = document.getElementById(“wrapper“);
const elementObj = templateFn({
credentials: “same-origin“,
get: (prop, value) => {
if (prop === “response“) {
if (value) {
wrapper.appendChild(value);
}
}
},
});
</script>
or
const templateFn = compile(
`<div>
<button class=”getHTML”>Get HTML!</button>
<request src=”/api/test” after=”click:.getHTML”></request>
</div>`
);
const wrapper = document.getElementById(“wrapper“);
const elementObj = templateFn({
credentials: “same-origin“,
get: (prop, value) => {
if (prop === “response“) {
if (value) {
wrapper.appendChild(value);
}
}
},
});
The function will fire dynamically, depending on the update of properties in the object.