5

I am using Notepad++ to create a simple web page where a user types in two numbers into a text box, and then presses a button. When they press the button something comes up that tells them whether the first or second number is greater. I have the following code but cant get anything to come up. Does anyone know whats wrong?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
   <title>Assignment 10 Form</title>

    <script type="text/javascript">

        function greaterNum(){
        var value1;
        var value2;
        value1 = document.First_num.value;
        value2 = document.last_num.value;
        if (value1 > value2){
        alert('Value 1 is greater than value 2');
        document.body.style.background = "orange";
        }

    }


</script> 


<style type="text/css">
  body{background-color: #40FF00;
    margin-left: auto;
    margin-right: auto;
    width: 60%;
    }

#container{
    border: 2px solid yellow;
    padding: 20px;
    }

</style>

</head>

<body>
<h1>Assignment 10</h1>

<div id="container">
    <div class="Num">
    <form>
<label class="label1">Enter Value 1:</label>
<input type="text" name="First_num" value=" " />
<label class="label1">Enter Value 2:</label>
<input type="text" name="last_num" value=" " />
<br/>
<input type="button" value=" Which number is greater? " onclick="greaterNum();" />
</form>

</body>
</html>

4 Answers 4

5

Values from inputs are retrieved as strings, you need to convert it to number. Try this:

value1 = +document.First_num.value;
value2 = +document.last_num.value;

Also, try to be consistent when naming your input, why caps for one and lowercase for the other?

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

1 Comment

@dewyze FYI, the ' .value' at the last is what completes the trick !
0

Convert to floats.

value1 = parseFloat(document.First_num.value);
value2 = parseFloat(document.last_num.value);

Comments

-1

You may need to use:

value1 = document.getElementById("First_num").value;

However, this only works if you assign the elements id attributes:

<input type="text" id="First_num" name="First_num" value=" " />

Otherwise, use the getElementsByName.

Hope this helps!!

Comments

-1

Html is a string, not a value. You need to parse it in someway.

EDIT:

The problem seems to be grabbing the values, change it to getElementById and it works fine:

http://jsfiddle.net/fHtZU/10/

HTML

<h1>Assignment 10</h1>

<div id="container">
    <div class="Num">
    <form>
<label class="label1">Enter Value 1:</label>
<input type="text" id="First_num" name="First_num" />
<label class="label1">Enter Value 2:</label>
<input type="text" id="last_num" name="last_num" />
<br/>
<input type="button" id="clickme" value="Which number is greater?" onclick="greaterNum()" />
</form>

js

function greaterNum(){
    var value1;
    var value2;
    value1 = document.getElementById("First_num").value;
    value2 = document.getElementById("last_num").value;
    if (value1 > value2){
        alert(value1 - value2);
        document.body.style.background = "orange";
    }

}

4 Comments

How do we parse this HTML?
I don't mean to be a downer, but in the future, please at least post the raw code for something and test on the local machine if possible. Thanks for the update!! :-)
Oh, you're totally right. There aren't always a lot of questions I really know how to answer, I got excited.
I would add that you need to check if the values entered by the user are numbers. You can use isNan function for that. You can do it using the function isNaN. if (isNaN(value1) || isNan(value2)) { alert('Enter numbers'); }

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.