04-22-2009, 08:00 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
What you are doing is possible yes, but! Theres no such event called onload on input tags, also you can't determine the type of the current object by comparing it like you do so in "CountMe", so you need to check if the "type" property is what you expect.
javascript Code:
switch(el.type) { case('...'): { /* ... */ } break; /* ... */ }
You may also use the increment operators (both postfix and prefix in javascript):
javascript Code:
var $variable;
++$variable; $variable++;
--$variable; $variable--;
/* $variable = 0 */
As for what you are trying to do, then I think something along these lines should be correct:
javascript Code:
function CategorizeInputFields() { var retval = { 'checkbox': 0, 'radio': 0, 'password': 0, 'file': 0, 'button': 0, 'hidden': 0, 'image': 0, 'reset': 0, 'submit': 0, 'text': 0, 'total': 0 };
/* Browser does not support getElementsByTagName, return void */ if(!document.getElementsByTagName) { return(retval); }
var elements = document.getElementsByTagName('input'); retval.total = elements.length;
/* No input tags in tree */ if(!retval.total) { return(retval); }
for(var n = 0; n < retval.total; ++n) { /* I'm really lazy so I'll use eval here, don't do like this! */ eval('++retval.' + elements[n].type.toLowerCase() + ';'); }
return(retval); }
function Render() { var elements = CategorizeInputFields();
document.write('There are: ' + elements.checkbox + ' checkboxes <br />'); document.write('There are: ' + elements.radio + ' radio buttons <br />'); document.write('There are a total of: ' + elements.total + ' input types <br />'); }
And in your HTML:
HTML Code:
<body>
<!-- input elements -->
<script type="text/javascript">
<!--
Render();
// -->
</script>
</body>
__________________
|
|
|