javascript - How to automatically sum input value boxes on each row in a HTML table -


i have html table columns of input boxes (quantity, price, discount , total). made js function adding many rows need. make total column, each row, display automatically value of formula:

(quantity * price) - ( quantity * price * (discount/100)) .

i tried numerous javascript code, have been unable correctly work.

html:

<table id="mytable"> <tr> <th width="65%"></th> <th align="center" width="5%">q<br>-ty</th> <th align="center" width="10%">price,</br>$</th> <th align="center" width="5%">discount,<br>%</th> <th align="center" width="15%">total, $<br>(without tax)</th> </tr> <tr> <td width="65%"><input class="left" size="100" type="text" name="description"></td> <td align="center" width="5%"><input type="number" name="quantity" min="1" max="99"></td> <td align="center" width="10%"><input type="number" name="summ" min="0" max="999999"></td> <td align="center" width="5%"><input type="number" name="rate" min="0" max="100"></td> <td align="center" width="15%"><input align="center" type="number" name="total" min="0" max="99999999"></td> </tr> </table> <button onclick="addrow()">add row</button> 

javascript:

function addrow() {     var table = document.getelementbyid("mytable");     var row = table.insertrow();     var cell1 = row.insertcell(0);     var cell2 = row.insertcell(1);     var cell3 = row.insertcell(2);     var cell4 = row.insertcell(3);     var cell5 = row.insertcell(4);     cell1.innerhtml = "<input class=\"left\" size=\"100\" type=\"text\" name=\"description\">";     cell2.innerhtml = "<div align=\"center\"><input type=\"number\" name=\"quantity\" min=\"1\" max=\"99\"></div>";     cell3.innerhtml = "<div align=\"center\"><input type=\"number\" name=\"summ\"  min=\"0\" max=\"999999\"></div>";     cell4.innerhtml = "<div align=\"center\"><input type=\"number\" name=\"rate\"  min=\"0\" max=\"100\"></div>";     cell5.innerhtml = "<div align=\"center\"><input type=\"number\" name=\"total\" min=\"0\" max=\"99999999\"></div>"; } 

my code on fiddle.

there little different example. remove div js code.

function calc(id) {  var row=id.parentnode.parentnode;  var quant=row.cells[1].getelementsbytagname('input')[0].value;  var price=row.cells[2].getelementsbytagname('input')[0].value;  var disc=row.cells[3].getelementsbytagname('input')[0].value;  if(disc==null || disc=='') {   res=parsefloat(quant)*parsefloat(price);  } else {   var res=(parsefloat(quant)*parsefloat(price))-(parsefloat(quant)*parsefloat(price)*(parsefloat(disc)/100));  }  row.cells[4].getelementsbytagname('input')[0].value=res;     }     function addrow() {      var table = document.getelementbyid("mytable");      var row = table.insertrow();      var cell1 = row.insertcell(0);      var cell2 = row.insertcell(1);  	var cell3 = row.insertcell(2);  	var cell4 = row.insertcell(3);  	var cell5 = row.insertcell(4);      cell1.innerhtml = "<input class=\"left\" size=\"100\" type=\"text\" name=\"description\">";      cell2.innerhtml = "<input onkeyup=\"calc(this);\" onchange=\"calc(this);\" type=\"number\" name=\"quantity\" min=\"1\" max=\"99\">";  	cell3.innerhtml = "<input onkeyup=\"calc(this);\" onchange=\"calc(this);\" type=\"number\" name=\"summ\"  min=\"0\" max=\"999999\">";  	cell4.innerhtml = "<input onkeyup=\"calc(this);\" onchange=\"calc(this);\" type=\"number\" name=\"rate\"  min=\"0\" max=\"100\">";  	cell5.innerhtml = "<input type=\"number\" name=\"total\" min=\"0\" max=\"99999999\">";  }
 body {      font-family: "calibri"; margin:0;}    #intotal {  	font-weight: bold;  	border-bottom: 2px solid black;  }  .nr {  	font-weight: bold;  }  input {      border: none;      background: transparent;  	text-align: center;  }  table, th, td {      border: 1px solid black;	      border-collapse: collapse;  }  .left {  	text-align: left;  	margin-left: 5px;  }
<table id="mytable">  <tr>                <th width="65%"></th>    <th align="center" width="5%">q<br>-ty</th>        <th align="center" width="10%">price,</br>$</th>    <th align="center" width="5%">discount,<br>%</th>    <th align="center" width="15%">total, $<br>(without tax)</th>    </tr>  <tr>  <td width="65%"><input class="left" size="100" type="text" name="description"></td>                    <td align="center" width="5%"><input onkeyup="calc(this);" type="number" name="quantity" min="1" max="99" onchange="calc(this);"></td>                        <td align="center" width="10%"><input onkeyup="calc(this);" type="number" name="summ" min="0" max="999999" onchange="calc(this);"></td>                            <td align="center" width="5%"><input onkeyup="calc(this);" type="number" name="rate" min="0" max="100" onchange="calc(this);"></td>                                <td align="center" width="15%"><input align="center" type="number" name="total" min="0" max="99999999"></td>        </tr>                                </table>                            	<button onclick="addrow()">add row</button>

i add 1 more function calculating formula on every change or number inputed.

that function placed in every input expect last 1 (onchange="calc(this) , onkeyup=calc(this);). put onkeyup on beginning of input. in case make calucation after number entered.

code is, can see, js.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -