Home > other >  How can i print add value in input tag?
How can i print add value in input tag?

Time:01-20

for( let i =0; i<productChcbx.length; i  ) {
  productChcbx[i].addEventListener("click",()=> {
    let cnt = 0;
    for(let j =0; j<productChcbx.length; j  ) {
      if(productChcbx[j].checked) {
        cnt  ;
        if(cnt == 1) {
          addProduct.style.display = "flex";
          productAmount.innerHTML = "70,000원";
        } else if (cnt > 1) {
          sum=70000   60000 * (cnt-1);
          productAmount.innerHTML = number_format(sum)   "원";
        } 
      } else if (!productChcbx[j].checked) {
        if(cnt == 0) {
          addProduct.style.display = "none";
        }
      }
    }
  })
}

resultSum.value = localAmount   productAmount;

variable localAmount and productAmount is Price value and I want to sum these two variables as resultSum.value but can't add two variable I don't know how to add those.

I mean Print This code ----> [object HTMLSpanElement][object HTMLSpanElement]

my english not good so im sorry but my this problem bring me here because ran into a problem three times.. Please give me inspiration...already thank you!

CodePudding user response:

You're trying to perform math on HTML elements, not on numbers.

The code doesn't define either of these variables, but this line certainly implies that at least one of them is an element:

productAmount.innerHTML = "70,000원";

And the output demonstrates this as well:

[object HTMLSpanElement][object HTMLSpanElement]

To perform math, you want to use numbers. For example, where you do this:

productAmount.innerHTML = "70,000원";

You might also store a numeric value:

productAmount.innerHTML = "70,000원";
productAmountValue = 70000;

(As a further exercise, you can investigate how to specify the numeric value once and then format that value as a string for the HTML element.)

Do the same for localAmount, wherever/whatever that is. Then you can add the value from those numbers:

resultSum.value = localAmountValue   productAmountValue;

(This could be another opportunity for investigate formatting a number for output.)

  • Related