I try to some form input values when I try to use this in the callback function this always refers to the window
object, not loginForm
const loginForm = document.forms["loginForm"];
const handleLogin = (e) => {
e.preventDefault();
try {
console.log(this); // always window {} ?
const email = this.elements.email.value;
const password = this.elements.password.value;
const res = await axios.post("http://localhost:3000/api/v1/users/login", {
email,
password,
});
if (res.data.status === "success") {
window.setTimeout(() => window.location.assign("/"), 1500);
}
} catch (error) {
console.log(error);
}
};
loginForm && loginForm.addEventListener("submit", handleLogin.bind(loginForm));