View Single Post
Old 04-22-2009, 06:47 PM   #1 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default count checkbox, radio and input....

Ok so I gave myself a simple project...I have 6 input types.
3 are of type radio, and the other 3 are checkboxes.

JS:
Code:
<script type="text/javascript">
var getCheck = document.getElementsByName('checkbox');
var getRadio = document.getElementsByName('radio');
var getInput = document.getElementsByTagName('input');
	
  
document.write('There are: ' +getCheck.length+ ' checkboxes');
document.write('There are: ' +getRadio.length+ ' radio buttons');
document.write('There are a total of: ' +getInput.length+ ' input types');
	
</script>
HTML:
Code:
<body>
<div><input type="checkbox" name="checkbox" value="checkbox" /></div>
<div><input type="checkbox" name="checkbox" value="checkbox" /></div>
<div><input type="checkbox" name="checkbox" value="checkbox" /></div>
<div><input type="radio" name="radio" /></div>
<div><input type="radio" name="radio" /></div>
<div><input type="radio" name="radio" /></div>
</body>
My output is:

There are: 0 checkboxes
There are: 0 radio buttons
There are a total of: 0 input types

I thought that the "getElementsByName and getElementsByTagName" were arrays? So I thought I could just use the ".length" to get the count?

So anyways, the output is suppose to say:

There are 3 checkboxes.
There are 3 radio buttons.
There are a total of: 6 input types.
allworknoplay is offline  
Reply With Quote