Quiero calcular el texto interno de algún elemento html.
para eso escribí una función que tomará la palabra clave "argumentos" como parámetro. Para que pueda pasar tantos elementos como sea posible.
El cuerpo de la función es como a continuación:
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') pero la función me da un error que dice: Uncaught TypeError: Cannot read property 'innerText' of null
Pero si escribo la función como a continuación, funciona:
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; }¿Cuál es el problema con la primera función? alguien me puede ayudar?
Está utilizando la palabra clave arguments de manera incorrecta.
No tienes que escribirlo explícitamente.
simplemente use:
function totalCalculation() { 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')Pase el argumento como matriz:
totalCalculation(['total-price', 'first-product', 'second-product', 'delivery']);Su problema tiene que ver con el hecho de que solo se declara 1 argumento en la función, pero pasa varios argumentos cuando lo invoca.
La solución es pasar una matriz como 1 argumento a totalCalculation Después de hacer eso, puede iterar sobre ellos como lo hace en el cuerpo de la función.
Código actualizado:
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; } // Total Calculation now has 1 argument as an array, that can then be iterated over. totalCalculation(['total-price', 'first-product', 'second-product', 'delivery'])