Hi I have a table where a user can add more rows to it. Added in each row is an input box called totalcost which is created using the following code. However
[CODE]// right cell
var cellRight3 = row.insertCell(3);
cellRight3.className = ‘results’;
var el3 = document.createElement(‘input’);
el3.type = ‘text’;
el3.name = ‘totalcost[]’;
el3.id = ‘totalcost[]’;
el3.size = 6;
el3.className = ‘inputbox2’;
cellRight3.appendChild(el3);
[COLOR=”Red”]el3.onblur = updateTotal();[/COLOR]
function updateTotal(){
var s = “totalcost[]”;
alert(s);
var newTotal=0;
var allEls=document.getElementsByTagName(“*”);
for(var i=0;i<allEls.length;i++)
{
if(allEls[i].getAttribute(“name”)!=s)continue;
if(isNaN(allEls[i].value))continue;
newTotal+=new Number(allEls[i].value);
}
document.getElementById(‘subtotalCell’).value=newTotal;
var totalvat =(newTotal / 100) * 17.5;
totalvat = eval(totalvat);
var total = totalvat + newTotal;
document.getElementById(‘vat’).value= “£” +totalvat;
document.getElementById(‘total’).value=”£” +total;
}
Now as you can see my intention is once the inputbox totalcost has been updated I want it to run the updateTotal function. However this is not happening instead it is being run as the inputbox is being created. Ive inserted an alert message to prove this. Has anyone got any ideas?
el3.onblur = function() {updateTotal();};
[CODE]
function updateTotal(){
var s = "totalcost[]";
alert(s);
var newTotal=0;
var allEls=document.getElementsByTagName("*");
for(var i=0;i<allEls.length;i++)
{
if(allEls[i].getAttribute("name")!=s)continue;
if(isNaN(allEls[i].value))continue;
[COLOR="Red"]alert(Number(allEls[i].value));[/COLOR]
newTotal+=new Number(allEls[i].value);
}
document.getElementById('subtotalCell').value=newTotal;
var totalvat =(newTotal / 100) * 17.5;
totalvat = eval(totalvat);
var total = totalvat + newTotal;
document.getElementById('vat').value= "£" +totalvat;
document.getElementById('total').value="£" +total;
}
[/CODE]
function updateTotal(){
var newTotal=0;
var allEls=document.getElementsByName("totalcost[]");
for(var i=0;i<allEls.length;i++)
{
if(!isNaN(allEls[i].value))
newTotal+=new Number(allEls[i].value);
}
document.getElementById('subtotalCell').value=newTotal;
var totalvat =(newTotal / 100) * 17.5;
var total = totalvat + newTotal;
document.getElementById('vat').value= "u00A3" +totalvat.toFixed(2);
document.getElementById('total').value="u00A3" +total.toFixed(2);
}
[CODE]
<?php
session_start();
$unexpected="You must be logged in to access this part of Down Town Administrators Centre";
if (!(isset($_SESSION['townadmindown'])))
{
header("Location: ../login.php?error=$unexpected");
}
else{
include("../connection.inc");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Down Town: Adminstrators Centre</title>
<link href="../css/downtown.css" rel="stylesheet" type="text/css" />
<link href="../css/style_nav.css" rel="stylesheet" type="text/css" />
<script src='ac.js' type='text/javascript'></script>
<script src='showdate.js'></script>
<script src='showsponsor.js'></script>
<script type="text/javascript">
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
lastRow = lastRow -4;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
// left cell
var cellLeft1 = row.insertCell(1);
var textNode1 = document.createTextNode(iteration);
cellLeft1.appendChild(textNode1);
cellLeft1.className = 'results';
// right cell
var cellRight = row.insertCell(2);
cellRight.className = 'results';
var el = document.createElement('input');
el.type = 'text';
el.name = 'description[]';
el.id = 'description[]';
el.size = 70;
el.className = 'inputbox2';
cellRight.appendChild(el);
// right cell
var cellRight2 = row.insertCell(3);
cellRight2.className = 'results';
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'unitcost[]';
el2.id = 'unitcost[]';
el2.size = 6;
el2.className = 'inputbox2';
cellRight2.appendChild(el2);
// right cell
var cellRight3 = row.insertCell(3);
cellRight3.className = 'results';
var el3 = document.createElement('input');
el3.type = 'text';
el3.name = 'totalcost[]';
el3.id = 'totalcost[]';
el3.size = 6;
el3.className = 'inputbox2';
cellRight3.appendChild(el3);
el3.onblur = function() {updateTotal();};
el3.onchange = function() {updateTotal();};
}
function removeRowFromTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
lastRow = lastRow - 4;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
function updateTotal(){
var s = "totalcost[]";
alert(s);
var newTotal=0;
var allEls=document.getElementsByTagName("*");
for(var i=0;i<allEls.length;i++)
{
if(allEls[i].getAttribute("name")!=s)continue;
if(isNaN(allEls[i].value))continue;
alert(Number(allEls[i].value));
newTotal+=new Number(allEls[i].value);
}
document.getElementById('subtotalCell').value=newTotal;
var totalvat =(newTotal / 100) * 17.5;
totalvat = eval(totalvat);
var total = totalvat + newTotal;
document.getElementById('vat').value= "£" +totalvat;
document.getElementById('total').value="£" +total;
}
</script>
</head>
<body>
<div id='top'>
<div class='title' style='float:left;'><img src='../images/dlib.jpg'/></div>
<div class='date'>
<?php
echo date("l");
echo" ";
echo date("j");
echo date("S");
echo" ";
echo date("F");
echo" ";
echo date("Y");
?>
</div>
</div>
<div id='inner'>
<form name='register' action='updateinvoice.php' method='post'>
<div class='label'>Company:</div>
<div class='input'>
<select name='company' width:'130px' class='select'>
<option value='none'>Please select</option>
<?php
include("../connection.inc");
$sql="SELECT ID, company FROM company order by company";
$result=mysql_query($sql) or die ("Error!! BAD SELECT SEARCH STATEMENT!!");
$nrows = mysql_num_rows($result);
if (isset($_POST['company'])) {
$name = $_POST['company'];
for($i=0;$i<$nrows;$i++){
$row = mysql_fetch_array($result);
extract($row);
if ($name==$ID) {
echo"<option value='$ID' SELECTED>$company</option>";
}
else {
echo"<option value='$ID'>$company</option>";
}
}
}
else {
for($i=0;$i<$nrows;$i++){
$row = mysql_fetch_array($result);
extract($row);
echo"<option value='$ID'>$company</option>";
}
}
?>
</select>
</div>
<br/>
<center>
<table id="tblSample">
<tr>
<td> </td>
<td class='tableheader'>Item</td>
<td class='tableheader'>Description</td>
<td class='tableheader'>Unit Cost</td>
<td class='tableheader'>Total</td>
</tr>
<tr>
<td><img src='../images/add.jpg' alt='add' onclick="addRowToTable();"/><img src='../images/remove.jpg' alt='remove' onclick="removeRowFromTable();"/></td>
<td class='results'>1</td>
<td class='results'><input type='text' name='description[]' id='description[]' size='70' maxlength='180' class='inputbox2'/></td>
<td class='results'><input type='text' name='unitcost[]' id='unitcost[]' size='6' maxlength='15' class='inputbox2'/></td>
<td class='results'><input type='text' name='totalcost[]' id='totalcost[]' size='6' maxlength='15' class='inputbox2' onblur="updateTotal()"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class='tableheader'>Sub Total</td>
<td class='results'><input type='text' name='subtotalCell' id='subtotalCell' size='6' maxlength='15' class='inputbox2'></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class='tableheader'>V.A.T</td>
<td class='results'><input type='text' name='vat' id='vat' size='6' maxlength='15' class='inputbox2'></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class='tableheader'>Total Due</td>
<td class='results'><input type='text' name='total' id='total' size='6' maxlength='15' class='inputbox2'></td>
</tr>
</table>
<br/><br/>
</center>
<br/><br/>
<div class='label'>Comments</div>
<div class='input'><textarea cols=58 rows=10 name='comments' class='inputbox'></textarea></div>
<div class='pageheader' style='text-align:center'>
<a href='companyidex.php'><img src='../images/back.jpg' border='0'/></a>
<input id='submit' type='image' name='submit' src='../images/update.jpg'>
</div>
</form>
</div>
</body>
</html>
<?php } ?>
[/CODE]
<tr><td></td><td class="results">2</td><td class="results"><input class="inputbox2" size="70" name="description[]" type="text"></td><td class="results"><input class="inputbox2" size="6" name="totalcost[]" type="text"></td><td class="results"><input class="inputbox2" size="6" name="unitcost[]" type="text"></td></tr><tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td class="tableheader">Sub Total</td>
<i> </i> <td class="results"><input name="subtotalCell" id="subtotalCell" size="6" maxlength="15" class="inputbox2" type="text"></td>
<i> </i> </tr>
Not only are the fields are the wrong way round, the table is incorrectly formed.function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
lastRow = lastRow -4;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
// left cell
var cellLeft1 = row.insertCell(1);
var textNode1 = document.createTextNode(iteration);
cellLeft1.appendChild(textNode1);
cellLeft1.className = 'results';
// right cell
var cellRight = row.insertCell(2);
cellRight.className = 'results';
var el = document.createElement('input');
el.type = 'text';
el.name = 'description[]';
//el.id = 'description[]';
el.size = 70;
el.className = 'inputbox2';
cellRight.appendChild(el);
// right cell
var cellRight2 = row.insertCell(3);
cellRight2.className = 'results';
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'unitcost[]';
//el2.id = 'unitcost[]';
el2.size = 6;
el2.className = 'inputbox2';
cellRight2.appendChild(el2);
// right cell
var cellRight3 = row.insertCell(4);
cellRight3.className = 'results';
var el3 = document.createElement('input');
el3.type = 'text';
el3.name = 'totalcost[]';
//el3.id = 'totalcost[]';
el3.size = 6;
el3.className = 'inputbox2';
cellRight3.appendChild(el3);
el3.onblur = function() {updateTotal();};
//el3.onchange = function() {updateTotal();};
}
function updateTotal(){
var newTotal=0;
var allEls=document.getElementsByName("totalcost[]");
for(var i=0;i<allEls.length;i++)
{alert(allEls[i].value+":"+Number(allEls[i].value)+":"+newTotal);
if(Number(allEls[i].value))
newTotal+=Number(allEls[i].value);
}
document.getElementById('subtotalCell').value=newTotal;
var totalvat =(newTotal / 100) * 17.5;
var total = totalvat + newTotal;
document.getElementById('vat').value= "u00A3" + totalvat.toFixed(2);
document.getElementById('total').value="u00A3" + total.toFixed(2);
}
function removeRowFromTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
lastRow = lastRow - 4;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
[CODE]for(var i=0;i<allEls.length;i++)
{alert(allEls[i].value+":"+Number(allEls[i].value)+":"+newTotal);[/CODE]
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
lastRow = lastRow -4;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
// left cell
var cellLeft1 = row.insertCell(1);
var textNode1 = document.createTextNode(iteration);
cellLeft1.appendChild(textNode1);
cellLeft1.className = 'results';
// right cell
var cellRight = row.insertCell(2);
cellRight.className = 'results';
var el = createControl('description[]', 70, 'inputbox2');
cellRight.appendChild(el);
// right cell
var cellRight2 = row.insertCell(3);
cellRight2.className = 'results';
var el2 = createControl('totalcost[]', 6, 'inputbox2');
cellRight2.appendChild(el2);
// right cell
var cellRight3 = row.insertCell(4);
cellRight3.className = 'results';
var el3 = createControl('totalcost[]', 6, 'inputbox2');
cellRight3.appendChild(el3);
el3.onblur = function() {updateTotal();};
}
function createControl(n, s, c) {
var obj = null;
try { // IE
obj = document.createElement('<input type="text" name="'+n+'" size="'+s+'" class="'+c+'">');
}
catch (e) { // W3C
obj = document.createElement('input');
obj.setAttribute('type', 'text');
obj.setAttribute('name', n);
obj.setAttribute('size', s);
obj.className = c;
}
return obj;
}
function updateTotal(){
var newTotal=0;
var allEls=document.getElementsByName("totalcost[]");
for(var i=0;i<allEls.length;i++)
{
if(Number(allEls[i].value))
newTotal+=Number(allEls[i].value);
}
document.getElementById('subtotalCell').value=newTotal;
var totalvat =(newTotal / 100) * 17.5;
var total = totalvat + newTotal;
document.getElementById('vat').value= "u00A3" + totalvat.toFixed(2);
document.getElementById('total').value="u00A3" + total.toFixed(2);
}
function removeRowFromTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
lastRow = lastRow - 4;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
0.1.9 — BETA 5.27