I have a set of HTML number inputs with ID's
YXS, YSM, YMED
I can get the values of each input, add them together, and display the result to #output.
HOWEVER, it only works when entered in order YXS, YSM, YMED
If i remove YSM so its blank, i only get YXS as a value output to #output.
Im trying to work it, so what ever values are entered, they're either added together and displayed, or if only 1 value is entered, then that value is displayed to #output regardless of the order of input.
Ive been scratching my head with it all day, so looking for some help where possible!
Here is my code :
$('#YXS').on("input", function() {
var yxsInput = this.value;
console.log(yxsInput);
var Silver = (yxsInput / 100) * 90;
var Gold = (yxsInput / 100) * 85;
var Plat = (yxsInput / 100) * 80;
var totalPrice = Number(yxsInput);
$('#output').text(yxsInput);
$("#output_Silver").html(Silver.toFixed(2));
$("#output_Gold").html(Gold.toFixed(2));
$("#output_Plat").html(Plat.toFixed(2));
$('#YSM').on("input", function() {
var ysmInput = this.value;
console.log(ysmInput);
var totalPrice = Number(yxsInput)+Number(ysmInput);
var Silver = (totalPrice / 100) * 90;
var Gold = (totalPrice / 100) * 85;
var Plat = (totalPrice / 100) * 80;
$('#output').text(totalPrice);
$("#output_Silver").html(Silver.toFixed(2));
$("#output_Gold").html(Gold.toFixed(2));
$("#output_Plat").html(Plat.toFixed(2));
$('#YMED').on("input", function() {
var ymedInput = this.value;
console.log(ymedInput);
var totalPrice = Number(yxsInput)+Number(ysmInput)+Number(ymedInput);
var Silver = (totalPrice / 100) * 90;
var Gold = (totalPrice / 100) * 85;
var Plat = (totalPrice / 100) * 80;
$('#output').text(totalPrice);
$("#output_Silver").html(Silver.toFixed(2));
$("#output_Gold").html(Gold.toFixed(2));
$("#output_Plat").html(Plat.toFixed(2));
});
});
I have tried getting the inputs first
$('#YXS').on("input", function() {
var yxsInput = this.value;
console.log(yxsInput);
$('#YSM').on("input", function() {
var ysmInput = this.value;
console.log(ysmInput);
$('#YMED').on("input", function() {
var ymedInput = this.value;
console.log(ymedInput);
Which didnt work out either and console would tell me that yxsInput (for example) is not defined.
Hope ive explained well enough what my end goal is!
Many thanks,