/    Sign up×
Community /Pin to ProfileBookmark

Counting tags inside a div…how?

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>
[/code]

It works except I must hardcode the count for the number of options that I want to make variable.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@weblizardOct 25.2008 — Try this instead:
<i>
</i>Cnt = document.getElementById(Category).getElementsByTagName('input').length;
Copy linkTweet thisAlerts:
@JMRKERauthorOct 25.2008 — Thank you 'weblizard' ... works perfectly. ?
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...