Click to See Complete Forum and Search --> : Adding elements


Matt Ellison
August 10th, 2001, 08:33 AM
I have a web page which is nicely generated through a database query. Basically it's a form that can be changed on any occasion. These forms have text boxes which need to be added up for a total box at the bottom of the page. There are two issues. First i would like to be able to add those elements on the client side (since i'm not actually storing the added total). Secondly the text boxes are named differently to relate to a key in the database for later updating and inserting. I need to be able to auto generate Javascript on the server side to add all the elements on a 'onchange' event. Below is an example of what i mean.

function TOTAL_onchange() {
var temp = 0.00;
temp = Number(document.thisForm.TOTAL.value) +
Number(document.thisForm.TOTAL.value);
document.thisForm.TOTAL.value = temp;

}
the TOTAL variable will be replaced and repeated according to how many elements exist on the page. If you need any clarifiaction please let me know.
Thanks

Matt Ellison
August 10th, 2001, 08:57 AM
I figured it out. I simple make all the names of each text box as a number or ID. Then use the following piece of code.
function TOTAL_onchange()
{
dml = document.thisForm;
len = dml.elements.length;
var i=0;
var Total = 0.00;
for( i=0 ; i<len ; i++)
{
if (isNumeric(document.thisForm.elements[i].name) )
{
Total = Total + Number(dml.elements[i].value)
}
}
document.thisForm.TOTAL.value = Total;
}

function isNumeric(text)
{
var rc;
text += ""; //convert to string
rc = text.match(/^[0-9]*$/);
if (rc == null)
{return false;}
else {return true;}
}

Thanks

HanneSThEGreaT
August 11th, 2001, 06:33 PM
Matt, if you need any JavaScript help - here's my email
cmdupreez@yebo.co.za


F. T. W.