JavaScript code works only in IE - javascript

Below is the code which creates a new line of text fields, say if the row has specified 4 text fields, the below code creates additional line same as above when I click the [Add] button.
The following code is working only in IE, it does not work in any other browser:
var delName = "ID=oth4del";
var t = oth4;
t.str = "\r\n <td><table class=\"query\" width=\"100%\">\r\n <tr>\r\n <td><input type=\"text\" name=\"q269r1491i~\" size=\"22\" maxlength=\"50\">\r\n </td>\r\n <td><input type=\"text\" name=\"q270r1447i~\" size=\"22\" maxlength=\"50\">\r\n </td>\r\n <td><input type=\"text\" name=\"q2765r6758i~\" size=\"22\" maxlength=\"50\">\r\n </font></td>\r\n </tr>\r\n </table></td>\r\n ";
t.str = '<table width="100%"><tr chooserroot="true"><td><input type="button" value="Del" onclick="delrow(this)" '+delName+'></td>'+t.str+'</tr></table>';
t.cnt = 1;
document.multis.push(t);
Error I get is this:
TypeError: m.lastChild.insertAdjacentHTML is not a function [Break On
This Error]
m.lastChild.insertAdjacentHTML('beforeBegin', m.str.replace(/~/g,++m.cnt));
Code for Add button:
The following functions get called:
<table align="center"><tr><td><input type="button" value="Add" onclick="addrow(this)" ID="insurance1Add"
></td></tr></table>
document.multis = new Array();
function findparent(e,tn) {
while(e && e.parentNode && e.tagName && e.tagName.toLowerCase()!=tn) e = e.parentNode;
return e;
}
function addrow(b)
{
var m = findparent(b,"div");
m.lastChild.insertAdjacentHTML('beforeBegin', m.str.replace(/~/g,++m.cnt));
}
///////////////////////////////////DEL ROW
function delrow(b)
{
var table = findparent(b,"table");
//var t = findparent(b,"div");
//alert(t.cnt);
//alert(table.cnt);
table.parentNode.removeChild(table);
}
Uncaught TypeError: Object # has no method 'insertAdjacentHTML'
Also I checked again in Chrome and it gave me error on: when addRow is called
HTML generated code:
<DIV ID="Div_Insurance" >
<SPAN id="INSURANCE" style="display:inline;">
<div ID="insurance1"
>
<table border="1%" align="center" width="100%" style="display:none"><tr>
<td><table class="query" width="100%">
<tr>
<td >Type</td>
<td><select name="q2485i~">
<option value="5581">Pharmaceutical</option>
<option value="5582" selected>Long Term Care</option>
<option value="5583">Home Care</option>
<option value="5584">Other</option>
</select>
</td>
<td>Other</td>
<td><input type="text" name="q2764r6757i~" placeholder="Other Infomation" size="22" maxlength="50"></td>
</tr>
<tr>
<td>Name of Insurance</td>
<td><input type="text" name="q2438r5533i~" placeholder="Insurance Name" size="22" maxlength="50">
</td>
<td>Contact Person</td>
<td><input type="text" name="q2439r5534i~" placeholder="Contact Person" size="22" maxlength="50">
</td>
</tr>
<tr>
<td>Address</td>
<td><textarea cols="20" rows="4" maxlength="50" name="q2440r5535i~" placeholder="Address"></textarea>
</td>
<td>Phone</td>
<td><input type="text" name="q2441r5536i~" size="22" maxlength="50" onKeyUp="jm_phonemask(this)" onBlur="wphoneLength(this)"; placeholder="XXX-XXX-XXXX">
</td>
</tr>
<tr>
<td>Fax</td>
<td><input type="text" name="q2442r5537i~" size="22" maxlength="50" onKeyUp="jm_phonemask(this)" onBlur="wphoneLength(this)"; placeholder="XXX-XXX-XXXX">
</td>
<td>Email</td>
<td><input type="text" name="q2443r5538i~" size="22" maxlength="50" placeholder="xyz#xyz.xyz">
</td>
</tr>
</table></td>
</tr></table>
<table align="center"><tr><td><input type="button" value="Add" onclick="addrow(this)" ID="insurance1Add"
></td></tr></table>
</div>
<script>
var delName = "ID=insurance1del";
var t = insurance1;
t.str = "\r\n <td><table class=\"query\" width=\"100%\">\r\n <tr>\r\n <td >Type</td>\r\n <td><select name=\"q2485i~\">\r\n <option value=\"5581\">Pharmaceutical</option>\r\n <option value=\"5582\" selected>Long Term Care</option>\r\n <option value=\"5583\">Home Care</option>\r\n <option value=\"5584\">Other</option>\r\n </select>\r\n </td>\r\n <td>Other</td>\r\n <td><input type=\"text\" name=\"q2764r6757i~\" placeholder=\"Other Infomation\" size=\"22\" maxlength=\"50\"></td>\r\n </tr>\r\n <tr>\r\n <td>Name of Insurance</td>\r\n <td><input type=\"text\" name=\"q2438r5533i~\" placeholder=\"Insurance Name\" size=\"22\" maxlength=\"50\">\r\n </td>\r\n <td>Contact Person</td>\r\n <td><input type=\"text\" name=\"q2439r5534i~\" placeholder=\"Contact Person\" size=\"22\" maxlength=\"50\">\r\n </td>\r\n </tr>\r\n <tr>\r\n <td>Address</td>\r\n <td><textarea cols=\"20\" rows=\"4\" maxlength=\"50\" name=\"q2440r5535i~\" placeholder=\"Address\"></textarea>\r\n </td>\r\n <td>Phone</td>\r\n <td><input type=\"text\" name=\"q2441r5536i~\" size=\"22\" maxlength=\"50\" onKeyUp=\"jm_phonemask(this)\" onBlur=\"wphoneLength(this)\"; placeholder=\"XXX-XXX-XXXX\">\r\n </td>\r\n </tr>\r\n <tr>\r\n <td>Fax</td>\r\n <td><input type=\"text\" name=\"q2442r5537i~\" size=\"22\" maxlength=\"50\" onKeyUp=\"jm_phonemask(this)\" onBlur=\"wphoneLength(this)\"; placeholder=\"XXX-XXX-XXXX\">\r\n </td>\r\n <td>Email</td>\r\n <td><input type=\"text\" name=\"q2443r5538i~\" size=\"22\" maxlength=\"50\" placeholder=\"xyz#xyz.xyz\">\r\n </td>\r\n </tr>\r\n </table></td>\r\n ";
t.str = '<table width="100%"><tr chooserroot="true"><td><input type="button" value="Del" onclick="delrow(this)" '+delName+'></td>'+t.str+'</tr></table>';
t.cnt = 1;
document.multis.push(t);
</script>
</SPAN>
</DIV>

In your addrow() function, do a console.log(m.lastChild) and see what it is.
Check its nodeType. I suspect it is a Text Node (i.e. nodeType = 3).
Since it is a text node, not an Element Node, then of course it wouldn't have the insertAdjacentHTML method.
References:
lastChild: https://developer.mozilla.org/en-US/docs/DOM/Node.lastChild
nodeType: https://developer.mozilla.org/en-US/docs/DOM/Node.nodeType
insertAdjacentHTML: https://developer.mozilla.org/en-US/docs/DOM/element.insertAdjacentHTML
Here's a classic, old-school snippet you might want to keep. After you get the lastChild (or any other flavors), ensure you're working on an Element Node.
function addrow(b) {
var m = findparent(b,"div"),
lastChildElement = m.lastChild;
// the old-school routine
while (lastChildElement && lastChildElement.nodeType !== 1) {
lastChildElement = lastChildElement.previousSibling;
}
if (lastChildElement) {
lastChildElement.insertAdjacentHTML('beforeBegin', m.str.replace(/~/g,++m.cnt));
} else {
// handle this case...
}
}
And as you might have already guessed, the opposite end would involve firstChild and nextSibling.

Related

how to add array values in text box

I have three text box that given as an array, I want to add two text box and get a result in the third text box as shown in below.
<tbody>
<?php
$i=1;
foreach ($sub as $row)
{
?>
<tr>
<td><?php echo $i++;?>
</td>
<td> <div><span style="border: none;"><input type="text" name="subject[]" class="textbox" value="<?php echo $row->subject_name;?>"></span></div></td>
<td><input type="text" name="mark-1[]" value="" id="mark1"></td>
<td><input type="text" name="mark-2[]" value="" id="mark2"></td>
<td><input type="text" name="total-mark[]" value="" id="totmark" onclick="add_number()"></td>
</tr>
<?php } ?>
<tr>
<th colspan="4">Total Mark</th>
<td></td></tr>
</tbody>
i have used the below script and its running but getting a result in the first loop
<script type="text/javascript">
function add_number() {
var first_number = parseInt(document.getElementById("mark1").value);
var second_number = parseInt(document.getElementById("mark2").value);
var result = first_number + second_number;
document.getElementById("totmark").value = result;
}
</script>
please help us get an answer
html
<tbody>
<?php
$i=1;
$k=1;
foreach ($sub as $row)
{ $k++;
?>
<tr>
<td><?php echo $i++;?>
</td>
<td> <div><span style="border: none;"><input type="text" name="subject[]" class="textbox" value="<?php echo $row->subject_name;?>"></span></div></td>
<td><input type="text" name="mark-1[]" value="" id="mark1-<?php echo $k;?>"></td>
<td><input type="text" name="mark-2[]" value="" id="mark2-<?php echo $k;?>"></td>
<td><input type="text" name="total-mark[]" value="" id="totmark-<?php echo $k;?>" onclick="add_number(<?php echo $k;?>)"></td>
</tr>
<?php } ?>
<tr>
<th colspan="4">Total Mark</th>
<td></td></tr>
</tbody>
Script
<script type="text/javascript">
function add_number(k) {
var first_number = parseInt(document.getElementById("mark1-"+k).value);
var second_number = parseInt(document.getElementById("mark2-"+k).value);
var result = first_number + second_number;
document.getElementById("totmark-"+k).value = result;
}
</script>
The problem is the repeated IDs for each iteration, mark1, mar2, mark1, mark2, and so on. The function getElementById is returning the first element.
An alternative is to use the function closest('tr') and then find by name [name="mark-1[]"] and [name="mark-2[]"]. Then find the field to store the result using this .querySelector('[name="total-mark[]"]').
Finally, using the function addEventListener you can bind the event click to the whole set of elements [name="total-mark[]"].
document.querySelectorAll('[name="total-mark[]"]').forEach(function(elem) {
elem.addEventListener('click', add_number);
});
function add_number() {
var tr = this.closest('tr');
var first_number = tr.querySelector('[name="mark-1[]"]').value;
var second_number = tr.querySelector('[name="mark-2[]"]').value;
var result = Number(first_number) + Number(second_number);
this.value = result;
}
<table>
<tbody>
<tr>
<td><input type="text" name="mark-1[]" value="" ></td>
<td><input type="text" name="mark-2[]" value="" ></td>
<td><input type="text" name="total-mark[]" value="" placeholder='click to totalize'></td>
</tr>
<tr>
<td><input type="text" name="mark-1[]" value="" ></td>
<td><input type="text" name="mark-2[]" value="" ></td>
<td><input type="text" name="total-mark[]" value="" placeholder='click to totalize'></td>
</tr>
</tbody>
</table>
A recommendation is using the current index from the PHP variable $i and add specific IDs to every field, that way you will be able to get the elements directly. For example, the two fields would have these IDs: mark-1-1[], mark-1-2[] and mark-2-1[], mark-2-2[], and so on.
This approach uses the attribute dataset.
document.querySelectorAll('[name="total-mark[]"]').forEach(function(elem) {
elem.addEventListener('click', add_number);
});
function add_number() {
var field = this.dataset.field;
var first_number = document.getElementById('mark-1-' + field + '[]').value;
var second_number = document.getElementById('mark-2-' + field + '[]').value;
var result = Number(first_number) + Number(second_number);
this.value = result;
}
<table>
<tbody>
<tr>
<td><input type="text" id="mark-1-1[]" value="" ></td>
<td><input type="text" id="mark-2-1[]" value="" ></td>
<td><input type="text" data-field='1' name="total-mark[]" value="" placeholder='click to totalize'></td>
</tr>
<tr>
<td><input type="text" id="mark-1-2[]" value="" ></td>
<td><input type="text" id="mark-2-2[]" value="" ></td>
<td><input type="text" data-field='2' name="total-mark[]" value="" placeholder='click to totalize'></td>
</tr>
</tbody>
</table>

jquery calculate on dynamically added rows

I`ve dynamiclally added rows, and i want to auto calculate values on it.
Here are part of my code:
<table id="itemsTable" class="general-table">
<thead>
<tr>
<th></th>
<th>Код на продукт</th>
<th>Описание</th>
<th>Брой</th>
<th>Цена</th>
</tr>
</thead>
<tbody>
<tr class="item-row">
<td></td>
<td><input name="itemCode[]" value="" class="tInput" id="itemCode" tabindex="1" /> </td>
<td><input name="itemDesc[]" value="" class="tInput" id="itemDesc" readonly="readonly" /></td>
<td><input name="itemQty[]" value="" class="tInput" id="itemQty" tabindex="2"/></td>
<td><input name="itemPrice[]" value="" class="tInput" id="itemPrice" readonly="readonly" /> </td>
</tr>
</tbody>
</table>
<span> <img src="images/icon-plus.png" alt="Add" title="Add Row" /> Добави ред</span>
<div style="float: right; width: 100px;">
<div id="container">Общо: <span style="clear:both;" id="added"></span><br></div>
</div>
And jquery
$('input').keyup(function(){ // run anytime the value changes
var firstValue = parseFloat($('#itemQty').val()); // get value of field
var secondValue = parseFloat($('#itemPrice').val()); //
$('#added').html(firstValue * secondValue); // add them and output it
});
The problem is that that script calculate only the first row.
You have repeated IDs in your webpage. And whenever you will use repeated IDs it will always treated for the first Element. So, you have to make dynamic ids for this purpose:
Making HTML with PHP:
<table id="itemsTable" class="general-table">
<thead>
<tr>
<th></th>
<th>Код на продукт</th>
<th>Описание</th>
<th>Брой</th>
<th>Цена</th>
</tr>
</thead>
<tbody>
<?php
foreach( $yourArray as $key=>$item){}
?>
<tr class="item-row">
<td></td>
<td><input name="itemCode[]" value="" class="tInput" id="itemCode_<?php echo $key;?>" tabindex="1" /> </td>
<td><input name="itemDesc[]" value="" class="tInput" id="itemDesc_<?php echo $key;?>" readonly="readonly" /></td>
<td><input name="itemQty[]" value="" class="tInput" id="itemQty_<?php echo $key;?>" tabindex="2"/></td>
<td><input name="itemPrice[]" value="" class="tInput" id="itemPrice_<?php echo $key;?>" readonly="readonly" /> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<span> <img src="images/icon-plus.png" alt="Add" title="Add Row" /> Добави ред</span>
<div style="float: right; width: 100px;">
<div id="container">Общо: <span style="clear:both;" id="added"></span><br></div>
</div>
Javascirpt/JQuery:
$(document).ready(function(){
$('input').keyup(function(){ // run anytime the value changes
// getting parent TR of current Input
var parent_tr = $(this).closest('tr');
//gettting parent TR's child input named as 'itemQty[]'
var firstValue = parseFloat($("input[name='itemQty[]']", parent_tr).val()); // get value of field
//gettting parent TR's child input named as 'itemPrice[]'
var secondValue = parseFloat($("input[name='itemPrice[]']", parent_tr).val()); //
$('#added').html(firstValue * secondValue); // add them and output it
});
});
Demo will Static HTML 2 rows: http://jsfiddle.net/Lvjff4ga/
$('input','#itemsTable').change(function(){
var tot = 0;
$('tbody tr','#itemsTable').each(function(){
tot += $('[name=itemQty\[\]]', this).val() * $('[name=itemPrice\[\]]', this).val();
});//each
$('#added').html(tot);
});

Get all input values from table excluding first column

I want to alert the input value of each row except the first column.
Here is my code.
<table id="table">
<tr>
<td><input type="text" size="5" value="name"/></td>
<td><input type="text" size="5" value="number"/></td>
<td><input type="text" size="5" value="class"/></td>
</tr>
<tr>
<td><input type="text" size="5" value="bbbbb"/></td>
<td><input type="text" size="5" value="1"/></td>
<td><input type="text" size="5" value="2"/></td>
</tr>
<tr>
<td><input type="text" size="5" value="cccc"/></td>
<td><input type="text" size="5" value="5"/></td>
<td><input type="text" size="5" value="2"/></td>
</tr>
<tr>
<td><input type="text" size="5" value="ddddd"/></td>
<td><input type="text" size="5" value="1"/></td>
<td><input type="text" size="5" value="2"/></td>
</tr>
$(document).ready(function() {
$("#table tr:gt(0) td:gt(0) ").each(function()
{
a=$(this).find('input[type="text"]').val();
alert(a);
});
});
While running this code all value alerted except "bbbb". (ie,ccccc ,dddd alerted.). I only need each rows second one column onwords.
fiddle: https://jsfiddle.net/k1tfo5kb/
This should work:
https://jsfiddle.net/23jcejbm/
$(document).ready(function() {
var $cells = $("#table tr td").not(':first-child'),
$inputs = $cells.find('input[type="text"]');
$.each($inputs, function(){
var value = this.value;
alert(value);
});
});
Here you go: https://jsfiddle.net/pukw0uLz/1/
I changed alert to console.log, much less aggressive. Make sure to open you web console!

jquery nextall first get id

I am trying to get the id of the next input field when user exit the input field:
$("input").blur(function(){
var theid = $(this).attr('id');
var thefield = $(this).nextAll('input').first().attr('id');
alert(thefield);
});
Currently it returns undefined.
The HTML code is:
<table><tr>
<td>Number</td><td>Name</td>
<tr><td>1 <input type='text' name='deltagernummer[]' value='' size='6' id='r0' tabindex='1' /></td>
<td><input type='text' name='deltagernavn[]' value='' id='rr0' tabindex='-1' /></td></tr>
<tr><td>2 <input type='text' name='deltagernummer[]' value='' size='6' id='r1' tabindex='2' /></td>
<td><input type='text' name='deltagernavn[]' value='' id='rr1' tabindex='-1' /></td></tr>
</table>
What is the problem?
$("input").blur(function() {
var theid = $(this).attr('id');
var thefield = $(this).nextAll('input').first().attr('id');
alert(thefield);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>Number</td>
<td>Name</td>
<tr>
<td>1 <input type='text' name='deltagernummer[]' value='' size='6' id='r0' tabindex='1' /></td>
<td><input type='text' name='deltagernavn[]' value='' id='rr0' tabindex='-1' /></td>
</tr>
<tr>
<td>2 <input type='text' name='deltagernummer[]' value='' size='6' id='r1' tabindex='2' /></td>
<td><input type='text' name='deltagernavn[]' value='' id='rr1' tabindex='-1' /></td>
</tr>
</table>
.nextAll() scans for siblings. Although your <input>s are in the same level, they're not siblings. To "visualize", here's pseudo-HTML:
Child and child's sibling:
<parent>
<child>
<childs-sibling>
</parent>
Your case:
<parent>
<descendant>
<input>
</descendant>
<descendant-sibling>
<totally-unrelated-to-input>
</descendant-sibling>
</parent>
What you can do is climb up to the <tr> level, succeeding rows, get all inputs, get the first one and then get the id.
var id = $(this)
.closest('tr') // Get to the row level
.nextAll('tr') // Get all succeeding rows
.find('td > input') // Find the inputs. You may adjust the selector.
.eq(0) // Get the first input
.attr('id'); // Get its id, if any
Nextall() goes through siblings, your inputs have no siblings.
$("input").blur(function(){
var theid = $(this).attr('id');
var thefield = $(this).closest('tr').next().find('input').attr('id');
alert(thefield);
});
$("input").blur(function() {
var theid = $(this).attr('id');
var thefield = $(this).closest('tr').next().find('input').attr('id');
alert(thefield);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>Number</td>
<td>Name</td>
<tr>
<td>1 <input type='text' name='deltagernummer[]' value='' size='6' id='r0' tabindex='1' /></td>
<td><input type='text' name='deltagernavn[]' value='' id='rr0' tabindex='-1' /></td>
</tr>
<tr>
<td>2 <input type='text' name='deltagernummer[]' value='' size='6' id='r1' tabindex='2' /></td>
<td><input type='text' name='deltagernavn[]' value='' id='rr1' tabindex='-1' /></td>
</tr>
</table>

Using the same jQuery in multiple operations

I have a small question and I hope someone can help me with it :D
I’m trying to create a product table, in which a user just add the quantity of a product and the jquery makes the multiplication to its value and gives the result
I already made this, and it works well:
<script>
$(document).ready(function(){
$('#quantity_1').keyup(function(){
var price_1 = $("#price_1").val();
var quantity_1 = $("#quantity_1").val();
quantity_1 = quantity_1.replace(/[^0-9]+/g, '');
$("#quantity_1").val(quantity_1);
var total_1 = price_1 * quantity_1;
$( "#total_1" ).val( total_1.toFixed(2) );
});
});
</script>
<table border="1" cellpadding="5px" cellspacing="0" >
<tr>
<td>Product</td>
<td>Price</td>
<td>Quantity</td>
<td>Total</td>
</tr>
<tr>
<td>Product Name</td>
<td><input type="hidden" name="product[1][price]" id="price_1" value="10.00" />10.00</td>
<td><input type="text" name="product[1][quantity]" id="quantity_1" /></td>
<td><input type="text" name="product[1][total]" id="total_1" value="0" /></td>
</tr>
</table>
Working demo here:
http://jsfiddle.net/EnterateNorte/9kswL0gf/
But I would like to be able to add more than one line, like so:
<table border="1" cellpadding="5px" cellspacing="0" >
<tr>
<td>Product</td>
<td>Price</td>
<td>Quantity</td>
<td>Total</td>
</tr>
<tr>
<td>Name 1</td>
<td><input type="hidden" name="product[1][price]" id="price_1" value="10.00" />10.00</td>
<td><input type="text" name="product[1][quantity]" id="quantity_1" /></td>
<td><input type="text" name="product[1][total]" id="total_1" value="0" /></td>
</tr>
<tr>
<td>Name 5</td>
<td><input type="hidden" name="product[5][price]" id="price_5" value="10.00" />23.00</td>
<td><input type="text" name="product[5][quantity]" id="quantity_5" /></td>
<td><input type="text" name="product[5][total]" id="total_5" value="0" /></td>
</tr>
<tr>
<td>Name 3</td>
<td><input type="hidden" name="product[3][price]" id="price_3" value="130.00" />10.00</td>
<td><input type="text" name="product[3][quantity]" id="quantity_3" /></td>
<td><input type="text" name="product[3][total]" id="total_3" value="0" /></td>
</tr>
<tr>
<td>Name 4</td>
<td><input type="hidden" name="product[4][price]" id="price_4" value="12.00" />10.00</td>
<td><input type="text" name="product[4][quantity]" id="quantity_4" /></td>
<td><input type="text" name="product[4][total]" id="total_4" value="0" /></td>
</tr>
</table>
And if it isn’t to much trouble, I would be awesome if it would SUM all the totals and show a gran total at the end of the table :)
Use:
$(document).ready(function(){
$('[name*=quantity]').keyup(function(){
var price = $(this).parent().prev().find('input').val();
var quantity = $(this).val();
var total = price * quantity;
$(this).parent().next().find('input').val( total.toFixed(2) );
});});
Working Demo
Update: For showing Grand Total
var sum = 0;
//iterate through each textboxes and add the values
$('[name*=total]').each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseInt(this.value);
}
});
Working Demo
What you can do is to find the elements using their relative position instead of hard coded ids
$(document).ready(function () {
$('input[id^="quantity_"]').keyup(function () {
var $tr = $(this).closest('tr');
var price = $tr.find('input[id^="price_"]').val();
var quantity = this.value;
var total = (price * quantity) || 0;
$tr.find('input[id^="total_"]').val(total.toFixed(2));
});
});
Demo: Fiddle
I've updated your code in Fiddle. You need to change in your markup a bit.
<tr>
<td>Product Name</td>
<td><input type="hidden" class="price" ... />10</td>
<td><input type="text" class="quantity" ... /></td>
<td><input type="text" class="total" ... /></td>
</tr>
$(document).ready(function(){
$('.quantity').keyup(function(){
var parent = $(this).closest("tr");
var price = $(".price", parent).val();
var quantity = $(".quantity", parent).val();
var total = price * quantity;
$(".total", parent).val(total.toFixed(2));
});
});
I would recommend you to use common class
HTML:
<tr>
<td>Name 5</td>
<td>
<input type="hidden" class="price" value="10.00" />10.00</td>
<td>
<input type="text" class="quantity" />
</td>
<td>
<input type="text" class="total" value="0" />
</td>
</tr>
<tr>
<td>Name 3</td>
<td>
<input type="hidden" class="price" value="130.00" />130.00</td>
<td>
<input type="text" class="quantity" />
</td>
<td>
<input type="text" class="total" value="0" />
</td>
</tr>
Script:
$('.quantity').keyup(function () {
var tr = $(this).closest('tr');
var price = tr.find('.price').val();
var quantity = $(this).val();
var total = price * quantity;
tr.find('.total').val(total.toFixed(2));
});
DEMO
Modify your table:
<table border="1" cellpadding="5px" cellspacing="0" id='table' >
<!-- your rows with inputs -->
<tr>
<td>
<input id='totalSum' value='0' type='text' />
</td>
</tr>
</table>
Make all your 'total' inputs readonly by adding 'readonly' attribute.
js code:
$(document).ready(function(){
$('[name*=quantity]').keyup(function(){
var total = 0;
$('#table tr').each(function(i,item){
var price = parseFloat($(item).find('[name*=price]').val().replace(',','.'));
var quantity = parseInt($(item).find('[name*=quantity]').val());
if(!isNaN(price) && !isNan(quantity)){
total += price*quantity;
}
});
$("#totalSum").val(total.toFixed(2));
});
});

Categories

Resources