Home > front end >  Display script variables in table td
Display script variables in table td

Time:04-21

I want to display a calculated value in a table without using input..

Here is my script....

    <script type="text/javascript">
    function totalIt() {
     var input = document.getElementsByName("product");
     var total = 0;
     for (var i = 0; i < input.length; i  ) {
      if (input[i].checked) {
      total  = parseFloat(input[i].value);
      }
     }
     document.getElementsByName("total")[0].value = total.toFixed(0);
     }
     </script>

and my html is....

    echo "<td align='right' data-label='Option Prices'> **DISPLAY THE VALUE HERE** </td>";

CodePudding user response:

If I am reading your question correctly you can use the following (i.e., you want to display the calculated value without using an input field):

document.querySelector('[data-label="Option Prices"]').innerText = total.toFixed(0);

CodePudding user response:

If you want to format your answer like "15,900" which is a US English locale standard use the following

document.querySelector('[data-label="Option Prices"]').innerText = new Intl.NumberFormat().format(total.toFixed(0))

More types of formatting and documentation here

  • Related