/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] onblur on an input box

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= “&#163;” +totalvat;
document.getElementById(‘total’).value=”&#163;” +total;

}

[/CODE]

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?

to post a comment
JavaScript

23 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 30.2008 — el3.onblur = function() {updateTotal();};

Also you need to change the method of input creation: http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/
Copy linkTweet thisAlerts:
@cs3mwauthorSep 30.2008 — well thats stopped the alert message so I presume that blur section is working correctly however the new value of newTotal is not being calculated correctly. Is there anything obvious in the updateTotal function that you can see?
Copy linkTweet thisAlerts:
@FangSep 30.2008 — Input field values are strings. You are concatenating their values not adding them.

The loop should use [I]document.getElementsByName('totalcost[]')[/I]
Copy linkTweet thisAlerts:
@cs3mwauthorSep 30.2008 — apoligies the function is not by the input boxes that are being created. Ive just added the following alert message in the function updateTotal and it is not being called by the looks

[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]
Copy linkTweet thisAlerts:
@FangSep 30.2008 — function updateTotal(){
var newTotal=0;
var allEls=document.getElementsByName("totalcost[]");
for(var i=0;i&lt;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);
}
Copy linkTweet thisAlerts:
@cs3mwauthorSep 30.2008 — No unfortunately that doesnt do the required job. The problem is that the function updateTotal() is not being called once the generated input boxes have been changed i.e. onBlur
Copy linkTweet thisAlerts:
@FangSep 30.2008 — Can you give the full code?
Copy linkTweet thisAlerts:
@cs3mwauthorSep 30.2008 — [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"&nbsp;";
echo date("j");
echo date("S");
echo"&nbsp;";
echo date("F");
echo"&nbsp;";
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>&nbsp;</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>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</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>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</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>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</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]
Copy linkTweet thisAlerts:
@FangSep 30.2008 — Generated row: &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td class="results"&gt;2&lt;/td&gt;&lt;td class="results"&gt;&lt;input class="inputbox2" size="70" name="description[]" type="text"&gt;&lt;/td&gt;&lt;td class="results"&gt;&lt;input class="inputbox2" size="6" name="totalcost[]" type="text"&gt;&lt;/td&gt;&lt;td class="results"&gt;&lt;input class="inputbox2" size="6" name="unitcost[]" type="text"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td class="tableheader"&gt;Sub Total&lt;/td&gt;

<i> </i> &lt;td class="results"&gt;&lt;input name="subtotalCell" id="subtotalCell" size="6" maxlength="15" class="inputbox2" type="text"&gt;&lt;/td&gt;
<i> </i> &lt;/tr&gt;
Not only are the fields are the wrong way round, the table is incorrectly formed.

id's must be unique, remove the generated id's or change their values.

For this to work in IE look at the link I gave previously.
Copy linkTweet thisAlerts:
@cs3mwauthorSep 30.2008 — In what are the fields the wrong way round? I have updated the scrip i.e. changed the table IDs so that they are named on the iterations and changed the function updateTotal unfortunately it still isnt working.

As I say the function is not being called once the input box has changed.
Copy linkTweet thisAlerts:
@FangSep 30.2008 — Field order generated: description, totalcost and unitcost

See the generated content
Copy linkTweet thisAlerts:
@cs3mwauthorSep 30.2008 — the field order is generated in the same way the table is created description, unitcost,totalcost.
Copy linkTweet thisAlerts:
@FangSep 30.2008 — No it isn't. Look at the generated code using the web developer toolbar

Try this: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&lt;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 &gt; 2) tbl.deleteRow(lastRow - 1);
}

Copy linkTweet thisAlerts:
@cs3mwauthorSep 30.2008 — No unfortunately that doesnt work the alert response 30.00:30:0 if I enter that into the first box then the value of the alert response never changes regardless of any new values entered.
Copy linkTweet thisAlerts:
@FangOct 01.2008 — ? What alert response?
Copy linkTweet thisAlerts:
@cs3mwauthorOct 01.2008 — In the function updateTotal you have put in the following line of code:

[CODE]for(var i=0;i<allEls.length;i++)
{alert(allEls[i].value+":"+Number(allEls[i].value)+":"+newTotal);[/CODE]


and as I said earlier if you put in 30.00 as the first value that never changes.

Thanks for this
Copy linkTweet thisAlerts:
@FangOct 01.2008 — That was for debugging purposes.

" the first value" The first Total field? Why would that change?
Copy linkTweet thisAlerts:
@cs3mwauthorOct 01.2008 — sorry the newTotal never changes from 0
Copy linkTweet thisAlerts:
@FangOct 01.2008 — ? If a Total is changed then Sub Total, V.A.T. and Total Due changes.
Copy linkTweet thisAlerts:
@cs3mwauthorOct 01.2008 — Im afraid it doesnt. I dont think its a browser problem as well because the newTotal doesnt change hence the 0 value that is alerted out.
Copy linkTweet thisAlerts:
@cs3mwauthorOct 01.2008 — There is a problem with the variable called allEls.length as this never changes from 1 regardless of how many input boxes called totalcost[] there are.
Copy linkTweet thisAlerts:
@FangOct 02.2008 — You didn't change the way the input is generated as outlined in the link

Read the article: http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/
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('&lt;input type="text" name="'+n+'" size="'+s+'" class="'+c+'"&gt;');
}
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&lt;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 &gt; 2) tbl.deleteRow(lastRow - 1);
}
Copy linkTweet thisAlerts:
@cs3mwauthorOct 02.2008 — No it has nothing to do with the way the input is generated. The problem was that the function updateTotal() was not looping through the input boxes named totalcosts[]. I went back to the original function I had previously and it now works!! Thanks for your help

Regards
×

Success!

Help @cs3mw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...