Home > Back-end >  Change value based on Closure Counter in JavaScript
Change value based on Closure Counter in JavaScript

Time:02-28

How to change the Total price value based on No. of boxes value.

the code I tried

function NegboxENtryValue() {
  let inputval = document.getElementsByClassName("boxENtryValue")[0].value
  let prodPrice = document.getElementById("productTotalPrice").innerHTML.split(/[\s&] /)
  prodPrice[1] = prodPrice[1] / inputval
  document.getElementById("productTotalPrice").innerHTML = `Rs. ${prodPrice[1]} /-`
}

function PosboxENtryValue() {
  let inputval = document.getElementsByClassName("boxENtryValue")[0].value
  let prodPrice = document.getElementById("productTotalPrice").innerHTML.split(/[\s&] /)
  prodPrice[1] = prodPrice[1] * inputval
  document.getElementById("productTotalPrice").innerHTML = `Rs. ${prodPrice[1]} /-`
}

where boxENtryValue is the value of no. of boxes. its working, but when decrease to 1, the value doesnt change, still on x2 value.

Note: Total price should be multiplication or division of Price/Box

CodePudding user response:

  function NegboxENtryValue() {
         let inputval = document.getElementsByClassName("boxENtryValue")[0].value
         let prodPrice = document.getElementById("productPrice").innerHTML.split(/[\s&] /) // here use product price not product total price
         prodPrice[1] = prodPrice[1] * inputval
         document.getElementById("productTotalPrice").innerHTML = `Rs. ${prodPrice[1]} /-`
       }

function PosboxENtryValue() {
      let inputval = document.getElementsByClassName("boxENtryValue")[0].value
      let prodPrice = document.getElementById("productPrice").innerHTML.split(/[\s&] /) // here use product price not product total price
      prodPrice[1] = prodPrice[1] * inputval
      document.getElementById("productTotalPrice").innerHTML = `Rs. ${prodPrice[1]} /-`
    }

Please use product price in the calculation.

  • Related