0

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?

5
  • 1
    Why a loop? A simple counter variable should do. You increase its value by one in your event handler function - and then based on that current value, you decide what to do ... Commented Oct 4, 2021 at 9:24
  • I suggested a loop because my knowledge of JS is limited. Can you please show me an example of using a simple counter variable? Commented Oct 4, 2021 at 9:25
  • 1
    var clickCounter = 0; outside of your function (global variable - those aren't generally considered too "nice", but you can work on improving things later.) clickCounter++; first thing in your handler function. And then proceed with if/else to do different things, based on whether the value is 1 or 2 ... Commented Oct 4, 2021 at 9:29
  • 1
    Instead of a counter, you can use two event handlers and swap them. Commented Oct 4, 2021 at 9:29
  • 👆🏻 Thanks folks, this has been a huge help and helped me search for the right terms. Commented Oct 4, 2021 at 9:42

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.