0

I have a input box and submit button as html, I would like to enter any number in the input box and then the alert message will display if it's even or odd. If it's even than one of the paragraph text color will change to red else blue. Here's my code:

    <!doctype html>
    <html lang="en">
    <head>
    <script src="jsfile.js"></script>
    </head>

    <body>
    <input type="text" id="inputBox"/>
    <button type="button" onclick="AlertBox()">Submit</button>

    <p id="p12">My first paragraph</p>
    </body>

    </html>

External js file:

    function AlertBox()
    {
        var number=document.getElementById("inputBox").value; 
        if (number / 2 == 0)
        {
            alert(number + " is even and ok to go."); 
            document.getElementById("p12").style.color = "red";

        } else
        {
            alert(number + " is not even and not ok to go."); 
            document.getElementById("p12").style.color = "blue"; 
        }
    }
2
  • Try var number=parseInt(document.getElementById("inputBox").value); Also, number / 2 == 0 won't get you what you want, try the modulus operator: if(number % 2 == 0){...} Commented Jun 26, 2015 at 18:31
  • Remember to pick your answer. Commented Jun 29, 2015 at 10:03

2 Answers 2

2

function AlertBox() {
  var number = document.getElementById("inputBox").value;
  if (number % 2 == 0) {
    alert(number + " is even and ok to go.");
    document.getElementById("p12").style.color = "red";

  } else {
    alert(number + " is not even and not ok to go.");
    document.getElementById("p12").style.color = "blue";
  }
}
<!doctype html>
<html lang="en">

<head>
  <script src="jsfile.js"></script>
</head>

<body>
  <input type="text" id="inputBox" />
  <button type="button" onclick="AlertBox()">Submit</button>

  <p id="p12">My first paragraph</p>
</body>

</html>

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

Comments

2

You can't test for a number being even with number/2 == 0. You need to use the modulo operator: number % 2 == 0.

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.