Hey i'm still relatively new to js and i've been trying to build this price calculator, that should output the result of the 2 fields (without having to submit anything possibly), but i'm struggling with calculating the inputted data! Can anybody tell me what i'm doing wrong and if it can be done in vanilla js? Thanks in advance
// (should) calculate
const priceCalculator = (base, height) => {
const outcome = (base * height) / 100;
return outcome;
}
// gets & stores base value
const getBase = function() {
const base = document.getElementsByClassName('field')[0].value;
console.log( base );
return base;
}
// gets & stores height value
const getHeight = function() {
const height = document.getElementsByClassName('field')[1].value;
console.log( height );
return height;
}
let result = priceCalculator(getBase, getHeight);
// result
document.getElementById('get-result').innerHTML += result;
<div class="calculator-wrapper">
<form class="calculator" action="somewhere.html" method="get">
<p>Base cm</p>
<input type="number" class="field" id="base-field" value="" onblur="getBase()">
<p>Height cm</p>
<input type="number" class="field" id="height-field" value="" onblur="getHeight()">
<h2>Your price is: <span id="get-result"></span>€</h2>
</form>
</div>