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); });
$('#mpy').val()and$('#mpg').val()?$.ready()jquery call ... basically wait for the DOM to be ready