Quote:
Originally Posted by allworknoplay
I am unsure of what is going on here. You create a variable called "retval" which I assume is short for "Retrieve Values".
|
Only Kalle really knows, but I'd assume something like "return value(s)".
Quote:
Originally Posted by allworknoplay
Is this an array or are we creating an object?
Code:
var retval =
{
'checkbox': 0,
'radio': 0,
'total': 0
};
|
It's an 'object literal', a convenient way of creating a new object with some properties. Remember everything in JS is an object. The boring way of doing the same would be:
JavaScript Code:
var retval = new Object();
retval.checkbox = 0;
retval.radio = 0;
retval.total = 0
Quote:
Originally Posted by allworknoplay
Are we passing "document.getElementsByTagName" to this conditional and if it doesn't exist we return nothing??
Code:
/* Browser does not support getElementsByTagName, return void */
if(!document.getElementsByTagName)
{
return(retval);
}
|
Basically, the condition asks if "document.getElementsByTagName" evaluates to boolean false. Since we're talking about a function, the condition will be false if it does not exist.
Also, it returns the retval object that was just created (with all zero values, remember) rather than void.
Quote:
Originally Posted by allworknoplay
Here we get all the of the input tags, then count them and assign the count to retval.total
Code:
var elements = document.getElementsByTagName('input');
retval.total = elements.length;
return void, if no total from document.getElementsByTagName('input') return nothing?
Code:
/* No input tags in tree */
if(!retval.total)
{
return(retval);
}
|
Yep, you got it. (Again, not really void but the object with zero totals)
Quote:
Originally Posted by allworknoplay
I know that this "The ++retval[]" is PRE-incrementing the element value and then returning it to the function (retval), but can someone explain to my how it works exactly?
The format just looks foreign to me...
Code:
for(var n = 0; n < retval.total; ++n)
{
++retval[elements[n].type.toLowerCase()];
}
return(retval);
|
elements[n].type.toLowercase() is processed as follows. Get the
nth item in the elements array (ie, the current one that we want to work with). Then get its type property (which is a String object). Then call the toLowerCase() method on that type property (the method can be called on any string). So the result of that expression is "checkbox" or "radio".
Now, the
++retval[…] should make a little more sense. Say the current element is a radio button, we are asking:
++retval["radio"] or please increment the value of
retval["radio"] which is just another way of writing
retval.radio