Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Calculate sum of last column in dynamically added rows using javascript

<HTML>

<HEAD>
  <TITLE> Add/Remove dynamic rows in HTML table </TITLE>

  <SCRIPT language="javascript">
    function addRow(tableID) {
      var table = document.getElementById(tableID);
      var rowCount = table.rows.length;
      if (rowCount < 4) { // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);

        var colCount = table.rows[0].cells.length;
        row.id = 'row_' + rowCount;
        for (var i = 0; i < colCount; i++) {
          var newcell = row.insertCell(i);
          newcell.outerHTML = table.rows[0].cells[i].outerHTML;
        }
        var listitems = row.querySelectorAll("input, select");

        for (i = 0; i < listitems.length; i++) {
          listitems[i].setAttribute("oninput", "calculate('" + row.id + "')");
        }

      } else {
        alert("Maximum Passenger per ticket is 4.");

      }
    }

    function calculate(elementID) {
      var mainRow = document.getElementById(elementID);
      var myBox1 = mainRow.querySelectorAll('[name=qty]')[0].value;
      var myBox3 = mainRow.querySelectorAll('[name^=sel]')[0].value;
      var total = mainRow.querySelectorAll('[name=total]')[0];
      var myResult1 = myBox1 * myBox3;
      total.value = myResult1;
      totalvalues();// calling my function here
    }
  </SCRIPT>
</HEAD>

<BODY>

  <input type="button" value="Add" onClick="addRow('dataTable')" />

  <table id="dataTable" class="form" border="1">
    <tbody>
      <tr id='row_0'>
        <p>
          <td>
            <label>Quantity</label>
            <input type="number" required="required" name="qty" oninput="calculate('row_0')">
          </td>

          <td>
            <label for="sel">Price</label>
            <select name="sel" id="sel" oninput="calculate('row_0')" required>
              <option value="" disabled selected>Choose your option</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
            </select>
          </td>
          <td>
            <label for="total">Total</label>
            <input type="number" required="required" class="small" name="total">
          </td>
        </p>
      </tr>
    </tbody>
  </table>
</BODY>

</HTML>
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: save to text or html file very good 
Javascript :: prisma Return a relations count with include 
Javascript :: convert .js to .ts 
Javascript :: Getting The Search Params With A For Of Loop 
Javascript :: regex specific number of characters 
Javascript :: react js css style border 
Javascript :: NG0100: Expression has changed after it was checked 
Javascript :: console.log(number++); console.log(++number); console.log(number); 
Javascript :: magnetic button vanilla js 
Javascript :: onclick readmore and readless react js 
Javascript :: javascript check if array has at least one true value 
Javascript :: Backbone Render 
Javascript :: how to create existing nodes in godot 
Javascript :: backbone view initialize 
Javascript :: convert milliseconds to seconds javascript 
Javascript :: site:stackoverflow.com how to see all react dependencies 
Javascript :: How to fix prettier messing up your HTML on save 
Javascript :: convert text to number 
Javascript :: remove decimal places js 
Javascript :: newtonsoft json parse string 
Javascript :: moment max 
Javascript :: call bind apply in javascript 
Javascript :: for-in loop 
Javascript :: JavaScript (SMonkey 60.2.3) sample 
Javascript :: knockout subscribe 
Javascript :: print blade value in js 
Javascript :: JavaScript Access Symbol Description 
Javascript :: javascript AutoCorrection in Date Object 
Javascript :: setup environment variables - fastify 
Javascript :: circular object array 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =