/    Sign up×
Community /Pin to ProfileBookmark

hi:

How I can get the number of checkboxes that are in the same form and have the same name?

Thanks!!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@pj59Jan 05.2005 — Hello!

I would not use checkboxes with the same name. You seem to. Example:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script language="JavaScript" type="text/javascript">
<!--
function count(who,frm){
howMany=0;
cb=frm.getElementsByTagName('input');
for (var i=0;i<cb.length;i++){
if(cb[i].type=='checkbox'&&cb[i].name==who)howMany++;
}
alert(howMany);
}
//-->
</script>
</head>
<body><form name="myForm">
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blah"><br>
<input type="button" value="count blah" onclick="count('blah',this.form)">
<br>
<input type="button" value="count blahh" onclick="count('blahh',this.form)">
</form>
<form name="myForm2">
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blahh"><br>
<input type="checkbox" name="blah"><br>
<input type="checkbox" name="blah"><br>
<input type="button" value="count blah" onclick="count('blah',this.form)">
<br>
<input type="button" value="count blahh" onclick="count('blahh',this.form)">
</form>
</body>
</html>[/code]
Regards PJ
Copy linkTweet thisAlerts:
@ScriptNoobie86Jan 05.2005 — Perhaps it could be simpler to understand:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
function count(formId, elementName)
{
code = document.getElementById(formId);
code = code.innerHTML
var count = 0
while(code.indexOf("name="+elementName+"&gt;") &gt; 0 )
{
code = code.replace("name="+elementName+"&gt;", "")
count++ <br/>
}
return(count)
}

&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;


&lt;form id="form1"&gt;
Checkbox 1:
&lt;input type=checkbox name="check" id="ch1"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 2:
&lt;input type=checkbox name="check" id="ch2"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 3:
&lt;input type=checkbox name="check" id="ch3"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 4:
&lt;input type=checkbox name="check" id="ch3"&gt;
&lt;/form&gt;
&lt;input type=button value='Test Function' onclick="alert(count('form1', 'check'))"&gt;
&lt;/body&gt;
&lt;/html&gt;


Enjoy, that should work cross browser also :-D
Copy linkTweet thisAlerts:
@pj59Jan 05.2005 — Sorry, but that code will alert a wrong result if, e.g., on of the boxes' name was checkxx.

PJ
Copy linkTweet thisAlerts:
@ScriptNoobie86Jan 05.2005 — If what?

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
function count(formId, elementName)
{
code = document.getElementById(formId);
code = code.innerHTML
alert(code)
var count = 0
while(code.indexOf("name="+elementName+"&gt;") &gt; 0 )
{
code = code.replace("name="+elementName+"&gt;", "")
count++ <br/>
}
return(count)
}

&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;


&lt;form id="form1"&gt;
Checkbox 1:
&lt;input type=checkbox name="checkxx" id="ch1"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 2:
&lt;input type=checkbox name="check" id="ch2"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 3:
&lt;input type=checkbox name="check" id="ch3"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 4:
&lt;input type=checkbox name="check" id="ch3"&gt;
&lt;/form&gt;
&lt;input type=button value='Test Function' onclick="alert(count('form1', 'check'))"&gt;
&lt;/body&gt;
&lt;/html&gt;


seems to work fine.....
Copy linkTweet thisAlerts:
@pj59Jan 05.2005 — [i]Originally posted by ScriptNoobie86 [/i]

[B]If what?[/B][/QUOTE]
If this:&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
function count(formId, elementName)
{
code = document.getElementById(formId);
code = code.innerHTML
var count = 0
while(code.indexOf("name="+elementName) &gt; 0 )
{
code = code.replace("name="+elementName, "")
count++ <br/>
}
return(count)
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="form1"&gt;
Checkbox 1:
&lt;input type=checkbox name="check" id="ch1"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 2:
&lt;input type=checkbox name="check[COLOR=red]xx[/COLOR]" id="ch2"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 3:
&lt;input type=checkbox name="check" id="ch3"&gt;
&lt;br&gt;&lt;br&gt;
Checkbox 4:
&lt;input type=checkbox name="check" id="ch3"&gt;
&lt;/form&gt;
&lt;input type=button value='Test Function' onclick="alert(count('form1', 'check'))"&gt;
&lt;/body&gt;
&lt;/html&gt;
PJ
Copy linkTweet thisAlerts:
@ScriptNoobie86Jan 05.2005 — yeah i just realized that, fixed it tho ;-D

thx 4 teh hlp
Copy linkTweet thisAlerts:
@pj59Jan 05.2005 — So let us be satisfied then upto an extend, the others permit. ?

Regards PJ
×

Success!

Help @gabrielgv 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.28,
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,
)...