Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!