-1

I was making a simple program "Find the Largest Number". The problem is, I can't display the output of my answer. Is there anything wrong on my code? Thanks

<html>
<head>
<title> Short Lab Activity 1 </title>

<style>
#compute{
text-align: center;
}
</style>
</head>

<body>
<div id="compute">
Input 1st Number: <input type="integer" id="num1" size="20"/> </br></br>
Input 2nd Number: <input type="integer" id="num2" size="20"/> </br></br>
Answer: <input type="integer" id="answer" size="20"/>
</div>
<center><input type="button" id="find" value="Find the Largest" onClick()="find()"/></center>

<script type="text/javascript">
     function find() {
     var numb1 = document.getElementById("num1").value;
     var numb2 = document.getElementById("num2").value;
     var answer;

     if(numb1 > numb2)
        document.getElementById("answer").value = numb1;
     if(numb2 > numb1)
        document.getElementById("answer").value = numb2;
         }

</script>
</body>
</html>
4
  • 1
    if(numb1 > numb2) { condition } else{ else condition} Commented Oct 10, 2015 at 2:01
  • The equal condition is missed here Commented Oct 10, 2015 at 2:05
  • <center> is deprecated! Welcome to stackoverflow :) Commented Oct 10, 2015 at 2:15
  • This will not help solve your problem but the type='integer' is invalid (the browser will convert this to text). A list of correct types, and their use, can be found at: html5doctor.com/html5-forms-input-types Commented Oct 10, 2015 at 2:16

4 Answers 4

1

Change onClick()="find()" to onClick="find()"

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

Comments

0

Your error is here:

<input type="button" id="find" value="Find the Largest" onClick()="find()"/>

Change it to:

<input type="button" id="find" value="Find the Largest" onClick="find()"/>

You do not need the parenthesis ()

Comments

0

The obvious mistake is that instead of onClick() = "find()" it should be onclick = "find()". .

Comments

0

I would not recommend using onclick in the html. Instead you could use addEventListener.

As for modifications to your function, if statement could be written like this,

if (numb1 > numb2) {
    document.getElementById("answer").value = numb1;
} else if (numb2 > numb1) {
    document.getElementById("answer").value = numb2;
}

Now after your function, you can add the event listener like this,

var el = document.getElementById("find");
el.addEventListener("find", find, false);

You can learn more about the addEventListener function here.

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.