0

I created a page with a yes or no question. If you answer yes and hit the submit button it will direct you to enter.html if you hit no and hit the submit button it will direct you to donotenter.html.

For some reason the submit button does not work on a single click. The user has to double click it in order to work. I was not able to find an answer on anyone else's question.

function check() {
  var c = 0
  var q1 = document.quiz.question1.value;
  if (q1 == "Yes") {

    document.getElementById("myButton").onclick = function() {
      window.location = 'enter.html';
    };
  } else {
    document.getElementById("myButton").onclick = function() {
      window.location = 'donotenter.html';
    };

  }
}
<form name="quiz" id="quiz">
  <div>
    <p> 1. What is the answer to this question:</p>
    <P><input type="radio" name="question1" value="Yes">A. Yes</P>
    <P><input type="radio" name="question1" value="No">B. NO</P>
  </div>
  <input type="button" name="" value="Submit" id="myButton" onclick="check()">
</form>

1
  • Your first click calls your function which then sets up the other event hanlders, which you don't even need. Commented Jul 20, 2020 at 15:23

3 Answers 3

1

Because the first click doesn't actually redirect the user. The first click does this:

document.getElementById("myButton").onclick = function () {
    window.location = 'enter.html';
};

It attaches a new click handler, which will (on the next click) redirect the user.

Instead of attaching the click handler, just perform the redirect:

function check(){
    var q1 = document.quiz.question1.value;
    if(q1 == "Yes") {
        window.location = 'enter.html';
    } else {
        window.location = 'donotenter.html';
    }
}

Alternatively, you can attach the click handler in this way, but put all of your logic within that handler and remove the onclick attribute from the button:

<input type="button" name="" value="Submit" id="myButton">

and:

document.getElementById("myButton").onclick = function () {
    var q1 = document.quiz.question1.value;
    if(q1 == "Yes") {
        window.location = 'enter.html';
    } else {
        window.location = 'donotenter.html';
    }
};

This has the benefit of separating the JavaScript from the HTML. The HTML is just the markup of the display and doesn't indicate what JavaScript logic to invoke, it becomes the job of the JavaScript to target the HTML and apply its logic. (Which makes the HTML more easily re-usable.)

Sign up to request clarification or add additional context in comments.

Comments

0

In your case, the redirection is triggered only after the second click, because you set the check function on the first call.

<input type="button" name="" value="Submit" id="myButton" onclick="check()">

In this call, you replace the click handler with a new function that is only called on the second press.

To make your code work as intended by you, you should replace your check function with this one:

function check() {
  var q1 = document.quiz.question1.value;
  if (q1 == "Yes") {
      window.location = 'enter.html';   
  } else {   
      window.location = 'donotenter.html'; 
  }
}

1 Comment

Thank yoy for your help!
0

You can use this

    <!Doctype  html>
    <html>
    <head>
    <title>Quiz</title>
    <link rel="stylesheet" type="text/css" href="quiz.css">
    <script type="text/javascript" src="quiz.js"></script>
    </head>
    <body>
        <form name="quiz" id="quiz">
            <div>
              <p> 1. What is the answer to this question:</p> 
              <p><input type="radio"name="question1" value="Yes">A. Yes</p>
              <p><input type="radio"name="question1" value="No">B. NO</p>
            </div>
            <input type="button" name="" value="Submit" id="myButton" onclick="check()">
        </form>
        <script>
            function check(){
                var c = 0; 
                var q1 = document.quiz.question1.value;
    
                if(q1 === "Yes") {
                   window.location = 'enter.html';
                } else if(q1 === ""){
                  alert('Please Choose Select Input First!');
                } else {
                   window.location = 'donotenter.html';
                }
            }
        </script>
    </body>
    </html

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.