1

I'm trying to make a basic ROI calculator and having trouble getting any kind of math function to work. I will only require two user variable inputs and want my coding to pass through a simple math equation and post the answer. The math formula will change once I can get my two input variables to at least multiply against each other.

Upon hitting submit, no output is given and the input fields reset. Clicking reset does clear the fields. I want the input (#output) to display the answer.

I don't care about CSS or final formula at this point.

HTML Type:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script src="calculator.js" type="text/javascript"></script>
    <title>Calculator</title>
</head>

<body>
    <form id="calculator">
        <input type="text" id="Occupancy" name="occupancy">
        <input type="text" id="rooms" name="rooms">
        <button id="submit" type="submit" name="submit">Submit</button>
        <button id="reset" type="reset" name="reset">Reset</button>
        <input type="text" id="output" name="output">
    </form>
</body>
</html>

JavaScript code below:

// JavaScript Document
$(document).ready(function() {
    $('#calculator').submit(function() {
        var occupancy = $('#Occupancy').val();
        var rooms = $('#rooms').val();
        var output =occupancy * rooms;
        output = output.toFixed(2);
        $('#output').val(output);
    });
});
5
  • 2
    Describe what happens vs. what's expected. Commented Dec 31, 2013 at 3:12
  • Upon hitting submit, no output is given and the input fields reset. Clicking reset does clear the fields. I want the input (#output) to display the answer. Commented Dec 31, 2013 at 3:17
  • Thank you. Removing the line "output=output.toFixed(2);" does not populate the answer field after submitting values. Commented Dec 31, 2013 at 3:24
  • 1
    @JoelCornett Because of the multiplication, typeof output evaluates to "number" and therefor toFixed() is valid in this context. Commented Dec 31, 2013 at 3:56
  • @Wynand Ah I see. Silent type conversions are still something I'm getting used to in js. Commented Dec 31, 2013 at 5:17

4 Answers 4

3

Your script works fine. What happens is, the script runs and calculates the result. But then, the form is submitted to the server and reloads the page.

To prevent this, you must prohibit the form from submitting to the server

$('#calculator').submit(function(evt) {
    evt.preventDefault();
    var occupancy = $('#Occupancy').val();
    var rooms = $('#rooms').val();
    var output = occupancy * rooms;
    output = output.toFixed(2);
    $('#output').val(output);
});

See JSFiddle

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

Comments

0

try to change

var occupancy = $('#Occupancy').val();

to var occupancy = parseFloat($('#Occupancy').val());

and var rooms = $('#rooms').val();

to var rooms = parseFloat$('#rooms').val());

1 Comment

Same results. Thank you though.
0

Try this:

// JavaScript Document
$(document).ready(function() {
    $('#submit').click(function() {
        var occupancy = $('#Occupancy').val();
        var rooms = $('#rooms').val();
        var output =occupancy * rooms;
        output = output.toFixed(2);
        $('#output').val(output);
    });
});

1 Comment

Good idea, although the form will still submit and by default, clear the value of output on page reload.
0

If there's no requirement to submit the form:

$('#calculateButton').click(function() {
  var occupancy = $('#Occupancy').val();
  var rooms = $('#rooms').val();
  var output = +occupancy * +rooms;

  $('#output').val(output.toFixed(2));
});

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.