Ive been looking for a way to insert HTML from an another HTML file (saved locally) - But important without Jquery or other libraries. Basically what I am trying to do is refactor from this:
First Approach (is working)
public setForm(): void {
this.appDiv.innerHTML = `<form id="registerForm">
<h1 class="h3 mb-3 font-weight-normal">Please sign up</h1>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required="" autofocus="">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control mt-1" placeholder="Password" required="">
<button id="registerButton" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>`;
}
to something like this:
Refactored version
public setForm(): void {
this.appDiv.innerHTML = document.insertFromPath('./views/register-panel.html').innerHTML;
}
Why do I want to do this?
Not only that the code looks cleaner. It allows me to encapsulate logik and each file has its own "responsibility". Javascript files tend to get messy.^^
insertFromPathover here: developer.mozilla.org/en-US/docs/Web/API/Document#Methods Where did you get that from?