jQuery addClass example

Change class name on click in jQuery

by Dmitri Ganenco

HTML

<table id="myTable" class="form">
  <thead>
    <tr>
    </tr>
  </thead>

  <tbody>
    <tr class="">
      <td>
        <input type="number" name="quantity" id="item-0-quantity">
      </td>

      <td>
        <input type="number" name="unit_price" id="item-0-unit_price">
      </td>
    </tr>
    <tr class="">
      <td>
        <input type="number" name="quantity" id="item-1-quantity">
      </td>

      <td>
        <input type="number" name="unit_price" id="item-1-unit_price">
      </td>
    </tr>
  </tbody>
</table>

<input type='text' id='Total' disabled value='0'>

JavaScript

$('#myTable').on('input', 'input', function(e) {
debugger;
  let anotherValNotParsed = $(this).closest('tr').find('input').not(this).val();
  let thisVal, parsedAnother, parsedTotal;
  try {
    thisVal = parseFloat($(this).val());
  } catch (e) {
    console.log(e);
    thisVal = 0;
  }
	
	try {
    parsedAnother = parseFloat(anotherValNotParsed);
  } catch (e) {
    console.log(e);
    parsedAnother = 0;
  }

  let multiplication = thisVal * parsedAnother;
	
	if(Number.isNaN(multiplication)) { // return if is NAN
		return; 
	}
	
	try {
    parsedTotal = parseFloat($('#Total').val());
		$('#Total').val(parsedTotal + multiplication);
  } catch (e) {
    console.log(e);
  }
});