I want to calculate the innerText of some html element.
for that I wrote a function that will take "arguments" keyword as parameter. So that I can pass as many element possible.
function body is like below:
function totalCalculation(arguments) {
let total = 0;
for (i = 0; i < arguments.length; i++) {
// getting all the required fields to be calculated from the arguments
const fieldAmount = parseInt(document.getElementById(arguments[i]).innerText);
total += fieldAmount;
}
document.getElementById('total-price').innerText = total;
document.getElementById('grand-total-price').innerText = total;
}
totalCalculation('total-price', 'first-product', 'second-product', 'delivery')
but the function is giving me error saying : Uncaught TypeError: Cannot read property 'innerText' of null
But if I write the function like below it works:
function totalCalculation() {
const totalPrice = parseInt(document.getElementById('total-price').innerText);
const firstProductPrice = parseInt(document.getElementById('first-
product').innerText);
const secondProductPrice = parseInt(document.getElementById('second-
product').innerText);
const deliveryCharge = parseInt(document.getElementById('delivery').innerText);
const total = totalPrice + firstProductPrice + secondProductPrice +
deliveryCharge
document.getElementById('total-price').innerText = total;
document.getElementById('grand-total-price').innerText = total;
}
what is the wrong with first function? Can somebody help me out?