|
|
|
|
|
by mbivert
698 days ago
|
|
Perhaps this can help to fix the ideas: here's how you could create the HTML by manipulating the DOM (the code indentation mimics the HTML indentation): let p = document.createElement("span");
let b = document.createElement("button");
b.innerText = btnText;
if (btnClass) b.classList.add(btnClass);
let m = document.createElement("span");
m.style.display = "none";
m.classList.add(modalClass);
let q = document.createElement("span");
q.classList.add(modalContentClass);
let c = document.createElement("button");
c.innerText = "×";
c.classList.add(modalBtnCloseClass);
// r is an "external" HTMLElement
r.classList.add(modalContentClass);
q.append(c, r);
m.appendChild(q);
p.append(b, m);
It's not that different from writing the HTML (with or without a template). But IMO the great thing here is that, at the end of that chunk of code, you've got pointers to every node of interest. You can then register handlers, target arbitrary nodes in such handlers, without having to navigate through the HTML (e.g. by setting ``id`` or other attributes to help identify them, or by relying on a static HTML structure).And as the parent points, this should prevent some amount of unexpected HTML injection. There's still a separation between the view and the page structure. |
|