Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Selective HTML form text field adder (See related posts)

In a form, use on text fields like this: onblur="selectiveTotal(this.form, 1);"
where 1 is a common number across all fields to be added, and where total1 is a field to save the total to. Work in progress to solve this common problem.
totalTheTotals(a,b,c) adds the value of a and b and puts it in c.

<script language="javascript" type="text/javascript">

function selectiveTotal(form, which)
{
	var total = 0;
	for(var j = 0; j < form.elements.length; j++){
		var cid = form.elements[j].id.toString();
		if ((form.elements[j].type == "text") && 
		    (form.elements[j].value.length > 0) && 
		    (cid.charAt(cid.length-1) == which) && 
		    (cid != "total" + which))
		{
			total += parseInt(form.elements[j].value);
		}
	}
	document.getElementById("total" + which).value = total;
	totalTheTotals("1","2","3");
}

function totalTheTotals(a, b, c)
{
	var total = parseInt(document.getElementById("total" + a).value) + parseInt(document.getElementById("total" + b).value);
	document.getElementById("total" + c).value = total;
}

</script>


You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts