Search code examples
javascripteventsinnerhtml

innerHTML not updating


I am trying to update the innerHTML of an element but not getting any output.

Each time an image of a taco is clicked the price of the taco should be added to the total. The values are numbers, not strings.

Any help is appreciated, I'm stumped.

(function() {

  var T1 = document.querySelector('#taco1');
  T1.addEventListener("click", function(e) {
    if (e.target.tagName === "IMG") {
      var Total = document.getElementById("Total").textContent;
      var price = document.getElementById("p1").textContent;
      var nbrprice = parseInt(price);
      var nbrTotal = parseInt(Total);
      console.log("value of nbrprice is " + nbrprice)
      console.log("nbrprice is a " + typeof(nbrprice));
      console.log("nbrTotal is a " + typeof(nbrTotal));
      var sum = Number(price) + Number(Total);
      console.log("sum is a " + typeof(sum));
      console.log("value of sum is " + sum);;
    }
    var sumtxt = (sum + nbrprice);
    Total.innerHTML = sumtxt.toString();
    console.log(Total);
  }, false);

})();
<div id='taco1'>
  <img src="https://placehold.it/300x100" width="300" />
  <p>Taco One Price: <span id="p1">4</span>
  </p>
</div>
<div id='taco2'>
  <img src="https://placehold.it/300x100" width="300" />
  <p>Taco Two Price: <span id="p2">5</span>
  </p>
</div>

<div>Total: $<span id="Total">0</span>
</div>


Solution

  • Your error was using, InnerHTML on TOTAL witch was set like this

       var Total = document.getElementById("Total").textContent;
    

    Instead of using innerHTML in just

    var Total = document.getElementById("Total");
    

    See here: When you click #taco1 you add the values to the total. Try it here:

    (function() {
    
      var T1 = document.querySelector('#taco1');
      T1.addEventListener("click", function(e) {
        if (e.target.tagName === "IMG") {
           var Total = document.getElementById("Total").textContent;
         var price = document.getElementById("p1").textContent;
          var nbrprice = parseInt(price);
          var nbrTotal = parseInt(Total);
          console.log("value of nbrprice is " + nbrprice)
          console.log("nbrprice is a " + typeof(nbrprice));
          console.log("nbrTotal is a " + typeof(nbrTotal));
          var sum = Number(price) + Number(Total);
          console.log("sum is a " + typeof(sum));
          console.log("value of sum is " + sum);;
         var sumtxt = (sum + nbrprice);
          console.log("sumtxt is a " + typeof(sumtxt));
          console.log("value of sumtxt is " + sumtxt);;
          console.log("sumtxt is a " + typeof((sumtxt).toString()));
          console.log("value of sumtxt is " + (sumtxt).toString());;
        document.getElementById("Total").innerHTML = sumtxt;
        console.log(sumtxt);
       }
      }, false);
    
    })();
    <div id='taco1'>
      <img src="https://placehold.it/300x100" width="300" />
      <p>Taco One Price: <span id="p1">4</span>
      </p>
    </div>
    <div id='taco2'>
      <img src="https://placehold.it/300x100" width="300" />
      <p>Taco Two Price: <span id="p2">5</span>
      </p>
    </div>
    
    <div>Total: $<span id="Total">0</span>
    </div>