I'm trying to let the user create an account, when the user has entered their details and clicks the button, an object is made based on the User class, and this should then be added to the 'users' array. When I run it, objects are created correctly however are not added to the array. Does anyone know why is this is? Thanks
const users = []
class User {
constructor(f, s, u, e) {
this.firstName = f
this.surName = s
this.userName = u
this.email = e
}
}
function formSubmit() {
let fname = document.getElementById("firstname").value
let sname = document.getElementById("surname").value
let email = document.getElementById("email").value
let uname = document.getElementById("username").value
if ((fname.length == 0) || (sname.length == 0) || (email.length == 0)) {
window.alert("Some boxes were unfilled")
} else {
if (checkEmail(email) == true) {
//document.getElementById("output").innerHTML = "Account created successfully"
window.alert("Account created successfully")
} else {
window.alert("Email address invalid")
}
}
users.push(new User(fname, sname, uname, email))
}
<form>
<label for="fname"></label>
<input type=t ext id="firstname" placeholder="First name" required>
<p></p>
<label for="sname"></label>
<input type=t ext id="surname" placeholder="Surname" required>
<p></p>
<label for="uname"></label>
<input type=t ext id="username" placeholder="Username" required>
<p></p>
<label for="email"></label>
<input type=t ext id="email" placeholder="Email" required>
<p id="eMessage"></p>
<button onclick="formSubmit()" type="submit">Create account</button>
</form>