jfoutch
jfoutch

Reputation: 1398

Why is parseInt not working for me?

No matter what I try I get NAN when I try to parse an input value into a variable. I am trying to make a calculator to determine fuel economy.

<!DOCTYPE html>
<html>
    <head>
        <title>Fuel Calculator</title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="description" content="Demo project">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
        <link rel="stylesheet" href="style.css">
        <style type="text/css"></style>
    </head>
    <body>
        <div class="wrapper">
            <h1>Fuel Usage</h1>
            <div class="row">
                <div class="col-md-12">

                        <form>
                          <div class="form-group">
                            <label>Miles Driven Per Year</label>
                            <input type="text" class="form-control" id="mpy">
                          </div>
                          <div class="form-group">
                            <label>MPG</label>
                            <input type="text" class="form-control" id="mpg">
                          </div>
                           <p>Gallons Used: <span id="used"></span</p>
                          <div class="form-group">

                          </div>


              <button id="submit" type="submit" class="btn btn-default">Submit</button> 
                        </form>
                </div>



                </div>
                </div>

            </div>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
        <script src="main.js" type="text/javascript"></script>
    </body>

</html>

js file

var mpy = parseInt($('#mpy').val(), 10);
var mpg = parseInt($('#mpg').val(), 10);
var gallonsUsed = mpy/mpg;

$('#submit').click(function(){  
    $('#used').text(gallonsUsed);
}); 

Upvotes: 0

Views: 563

Answers (2)

DinoMyte
DinoMyte

Reputation: 8858

Since you have the input fields declared globally, the initial value in the text value is empty. So the result of parseInt would give you a NAN value.
You need to calculate the parsed values once you invoke the click event.

$('#submit').click(function(e){  
    var mpy = parseInt($('#mpy').val(), 10);
    var mpg = parseInt($('#mpg').val(), 10);
    var gallonsUsed = mpy/mpg;
    $('#used').text(gallonsUsed);
}); 

Working example : https://jsfiddle.net/DinoMyte/b9bhs4s3/

Upvotes: 2

Jaromanda X
Jaromanda X

Reputation: 1

The way you have your code, mpy/mpg/gallonsUsed are all calculated when the page is loaded (before any user input can even take place)

Your code would make more sense like this

$('#submit').click(function(){  
    var mpy = parseInt($('#mpy').val(), 10);
    var mpg = parseInt($('#mpg').val(), 10);
    var gallonsUsed = mpy/mpg;
    $('#used').text(gallonsUsed);
}); 

So that the calculation is done once user has entered data and hit submit

Upvotes: 2

Related Questions