I am trying to create a calculator style thing, where you put in how many years into a input, you hit submit and it gives you different results below without reloading the page.
I have all the calculations working right now, but I just cant get the input number variable to update when the submit button is clicked, and then print the correct results on the page. I have been googling for an hour and I cant seem to get it right, Im learning JS still.
Here is my JS:
// Get Years
var years = (document.getElementById('years').value);
// Variables
var years;
var gallons = 1100 * 365;
var grain = 45 * 365;
var forest = 30 * 365;
var co2 = 20 * 365;
var animal = 1 * 365;
// Calculations
var gallonsTotal = years * gallons;
var grainTotal = years * grain;
var forestTotal = years * forest;
var co2Total = years * co2;
var animalTotal = years * animal;
// Functions
function calc() {
var years = (document.getElementById('years').value);
//Prints
document.querySelector('.gallons').innerHTML = "Gallons " + gallonsTotal;
document.querySelector('.grain').innerHTML = "Gains " + grainTotal;
document.querySelector('.forest').innerHTML = "Forest " + forestTotal;
document.querySelector('.co2').innerHTML = "Co2 " + co2Total;
document.querySelector('.animals').innerHTML = "Animals " + animalTotal;
};
HTML:
<div class="small-4 columns">
<input type="number" id="years" min="1" max="99">
</div>
<div class="small-8 columns">
<a href="#" class="button postfix submit" onclick="calc()">Submit</a>
</div>
Here is a pen of my current progress: http://codepen.io/LukeD1uk/pen/BNwXMX