I fetched a login form asynchronously, and setted it inside body. I declared it’s on submit form before that, and now getting a Reference error
. Is there any way to set onsubmit using html, without having to call loginForm.onsubmit = login
.
const result = await fetch('/session');
if (result) {
const login = await result.text();
document.body.innerHTML = login;
}
function login(e) {
e.preventDefault();
const loginForm = document.getElementById('loginForm');
console.log(loginForm);
const options = { method: loginForm.method, body: new FormData(loginForm) };
const loginButton = document.getElementById('loginButton');
const loginButtonHTML = loginButton.innerHTML;
loginButton.innerHTML = 'Loading...';
disableElements(loginForm);
// const result = await fetch(loginForm.action, options);
}
The html is:
<form id="loginForm" action="/user/login" method="post" onsubmit="login">
<input type="text" name="username" autocomplete="username" placeholder="Username">
<input type="password" name="password" autocomplete="current-password" placeholder="Password">
<button id="loginButton" type="submit">Login</button>
</form>