If I have several <DIV> tags in a script that each contains several <INPUT> tags,
how do I count the number of occurances for each <DIV>?
My thoughts run along this where I trying to determine a ‘Cnt’ value in the ‘UpdateCost()’ function:
[code=php]
<html>
<head>
<title>Multiple CBox Prices</title>
<script type=”text/javascript”>
function UpdateGroups(Group) {
var elem = document.getElementById(Group);
var GN = elem.selectedIndex; // alert(elem.id+’n’+GN);
var ID = elem.options[GN].value; // alert(ID);
for (i=0; i<elem.options.length; i++) {
document.getElementById(elem.options[i].value).style.display = ‘none’;
}
document.getElementById(ID).style.display = ‘block’; // alert(elem.id+’n’+ID);
UpdateCost(ID);
}
function UpdateCost(Category) {
var sum = 0;
var gn, elem;
// following gives an error.
// alert(‘Inputs: ‘+document.getElementById(Category).document.getElementsByTagName(‘input’).length);
// I want to set above .length value into ‘Cnt’ below.
// If I leave out the document.getElementById(Category). above, I can get a TOTAL count of the <INPUT> tags (16), but I want a sub-group count.
Cnt = 5;
for (i=0; i<Cnt; i++) {
gn = Category+i;
elem = document.getElementById(gn); // alert(elem.id+’n’+gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById(‘totalcost’).value = sum.toFixed(2);
}
</script>
</head>
<body>
<select id=”Groups” onchange=”UpdateGroups(this.id)”>
<option value=”Blank”>Select</option>
<option value=”Game”>Games</option>
<option value=”Food”>Foods</option>
<option value=”Car”>Cars</option>
</select>
<div id=”Blank” style=”display:block”></div>
<div id=”Game” style=”display:none”>
<input type=”checkbox” id=’Game0′ value=”9.99″ onclick=”UpdateCost(‘Game’)”>Game 1 ( 9.99)<br>
<input type=”checkbox” id=’Game1′ value=”19.99″ onclick=”UpdateCost(‘Game’)”>Game 2 (19.99)<br>
<input type=”checkbox” id=’Game2′ value=”27.50″ onclick=”UpdateCost(‘Game’)”>Game 3 (27.50)<br>
<input type=”checkbox” id=’Game3′ value=”45.65″ onclick=”UpdateCost(‘Game’)”>Game 4 (45.65)<br>
<input type=”checkbox” id=’Game4′ value=”87.20″ onclick=”UpdateCost(‘Game’)”>Game 5 (87.20)<br>
</div>
<div id=”Food” style=”display:none”>
<input type=”checkbox” id=’Food0′ value=”0.99″ onclick=”UpdateCost(‘Food’)”>Food 1 (0.99)<br>
<input type=”checkbox” id=’Food1′ value=”1.99″ onclick=”UpdateCost(‘Food’)”>Food 2 (1.99)<br>
<input type=”checkbox” id=’Food2′ value=”2.99″ onclick=”UpdateCost(‘Food’)”>Food 3 (2.99)<br>
<input type=”checkbox” id=’Food3′ value=”3.99″ onclick=”UpdateCost(‘Food’)”>Food 4 (3.99)<br>
<input type=”checkbox” id=’Food4′ value=”4.99″ onclick=”UpdateCost(‘Food’)”>Food 5 (4.99)<br>
</div>
<div id=”Car” style=”display:none”>
<input type=”checkbox” id=’Car0′ value=”12999″ onclick=”UpdateCost(‘Car’)”>Car 1 (12999)<br>
<input type=”checkbox” id=’Car1′ value=”15999″ onclick=”UpdateCost(‘Car’)”>Car 2 (15999)<br>
<input type=”checkbox” id=’Car2′ value=”19999″ onclick=”UpdateCost(‘Car’)”>Car 3 (19999)<br>
<input type=”checkbox” id=’Car3′ value=”25000″ onclick=”UpdateCost(‘Car’)”>Car 4 (25000)<br>
<input type=”checkbox” id=’Car4′ value=”30000″ onclick=”UpdateCost(‘Car’)”>Car 5 (30000)<br>
</div>
<input type=”text” id=”totalcost” value=””>
</body>
</html>
It works except I must hardcode the count for the number of options that I want to make variable.