Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
If click count number JavaScript

I'm building a demo login form. For the demo, I'd like two things to happen:

  1. When the user clicks the login button for the first time, an error message appears.
  2. When the user clicks the login button for the second time, they are directed to another page.

Here's how my code looks so far:

function toggleError() {
    var toggleError = document.querySelector('.message--error');
    toggleError.classList.toggle('error-toggled');
}
.message {
  display: none;
}

.error-toggled {
  display: block;
}
<div class="message message--error">
    <span class="message__text">The login details you entered are incorrect.</span>
</div>

<form onsubmit="toggleError(); return false;" action="/" method="get" class="login__form" autocomplete="off">
    <label for="email" class="srt">Email</label>
    <input type="email" name="email" id="email" required placeholder="Email" class="input input--border-right">
    <label for="password" class="srt">Password</label>
    <input type="password" name="password" id="password" required placeholder="Password" class="input">
    <div class="login__actions">
        <button type="submit" class="button">Log In</button>
        <a href="index.html" class="login__anchor">Lost your password?</a>
    </div>
</form>

Using onsubmit="toggleError(); return false;" achieves my first objective of displaying an error message.

How would I extend this function so the user is directed to a page when they click the button for a second time? I'm guessing I would write some type of loop?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on first two comments here a possible way:

<script>
    var clickCounter=0;
    
    function toggleError() {
        //Increase the value by one
        clickCounter++;
        if (clickCounter == 1) {
           //what ever you want to do
           return false;
        } else if (clickCounter == 2) {
          //You need to change the value again
          ...
          var toggleError = document.querySelector('.message--error');
          toggleError.classList.toggle('error-toggled');
          return false;
        }
    }
</script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda