Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
function not working on "submit" event, but works when ran in dev console

I am rendering the form below, and below that I have a script. When I load the page, type in the email and password, then submit, it appears to just reload the page (regardless of credentials).

When I type in the email and password, and then execute the function in the dev console by typing loginUser() it executes as expected and if creds are correct it redirects me as it should. If credentials are wrong the alert events fire.

I have put in breakpoints, and on "submit" it gets the email and password that are in the form, but then seems to jump back up to the start of the function (regardless of credentials).

let form = document.getElementById('loginform');
form.addEventListener('submit', loginUser);

function loginUser() {
  let email = document.getElementById('email').value;
  let password = document.getElementById('password').value;
  firebase.auth().signInWithEmailAndPassword(email, password).then(() => {
    window.location.href = "/";
  }).catch(error => {
    console.log(error)
    let errorCode = error.code;
    let errorMessage = error.message;
    if (errorCode == 'auth/wrong-password') {
      window.alert('Entered password was incorrect.');
    } else if (errorCode == 'auth/user-not-found') {
      window.alert('Entered email is invalid.');
    } else {
      window.alert('Please check your login credentials.');
    }
  });
};
firebase.auth().onAuthStateChanged(user => {
  if (user) {
    console.log(user)
    user.getIdToken().then(function(token) {
      //save the token in a cookie  
      document.cookie = "token=" + token;
    });
  } else {
    console.log("What up dude?");
  }
});
<form id="loginform" action="" method="post">
  <div class="form-group">
    <label for="email">Email</label> <input class="form-control" id="email" name="email" required="required" type="text" value="">
  </div>
  <div class="form-group">
    <label for="password">Password</label> <input class="form-control" id="password" name="password" required="required" type="password" value="">
  </div>
  <div class="form-group">
    <input class="btn btn-primary" id="submit" name="submit" type="submit" value="Login">
  </div>
</form>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is imperative to REMOVE or RENAME id="submit" name="submit"

Calling anything submit in a form hides the submit method

Then add preventDefault() to stop submission:

function loginUser(e) {
  e.preventDefault()
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda