![]() |
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">Code:
<body>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. |
You have to wait for those DOM elements to have been loaded before you can access them with JavaScript. Either hook into the body's onload event (or DOMReady if you're feeling adventurous) or place the script below the inputs in your HTML.
|
Like said by Salathe, you need to execute when your dom tree is fully loaded (this is called "domready"). Most frameworks includes support for the domready event, so you can bind a new domready even and execute your code then.
The fact that this does not work is because the browser does not have a 2 stage parsing/execution stage like PHP (4 and newer) does. Meaning that once this part is parsed, the browser will execute it, so when it executes your javascript code it havn't got to the HTML elements yet, making them render as 0 :) |
Thanks guys,
When I moved the JS code below the HTML, I got the output I was looking for. However, it doesn't seem "clean" to do that. So I will look into how I can hook into the body "onLoad" event. This way I can keep the majority of the JS code on the top part of the page... As for Domready, the last 10 mins was the first time I ever even heard of that!!! |
Don't use onload, its the same as running it in <head>, domready or on the bottom of your HTML, just before </body> to emulate a domready event.
;) |
Ok, well I didn't use onLoad, because I don't really know how to use it just yet... :-(
But here's what I have, it DOESNT work, but it's what I have in mind. I am using "onLoad" in the INPUT tags *ONLY* as a placeholder because I don't know how to call the function within the INPUT tags. JS: Code:
<script type="text/javascript">Code:
<body>As each element loads, it calls the "countMe" function. I use the "this" to dynamically tell the function what type of element it is. The function then just takes a look at what element it is, and adds "1" to the correct counter. Is what I'm doing possible? How do you call a function within the INPUT tags? Everything I've looked up, it's always based on some kind of event like onClick, onFocus etc... Is there any "passive" way to call a function without user input? |
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:
You may also use the increment operators (both postfix and prefix in javascript): javascript Code:
As for what you are trying to do, then I think something along these lines should be correct: javascript Code:
And in your HTML: HTML Code:
<body> |
Quote:
javascript Code:
|
Quote:
Ok, so it works now, I see where you went with the code. Although the "output" is written up on top, by using the Render() function after the elements have already loaded, you can now output the correct number of elements instead of diplaying 0. gotchya...I have some questions about the CategorizeInputFields function, so I'll break it down here.... I am unsure of what is going on here. You create a variable called "retval" which I assume is short for "Retrieve Values". Is this an array or are we creating an object? Code:
var retval = 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 */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');Code:
/* No input tags in tree */You then find the element type with: elements[n].type. The [n] is just the increment for each type of element. 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)let me know if I got most of the code right? Thanks! |
Quote:
Quote:
JavaScript Code:
Quote:
Also, it returns the retval object that was just created (with all zero values, remember) rather than void. Quote:
Quote:
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 |
As for the "retval", then yes it stands for "return value" and Salathe covered all the points I see ;)
|
Thanks Salathe for explaining it to me in layman's terms.
I'll look into the object literal, that doesn't look like it's something that is available in PHP right? Sounds pretty cool though, almost like a shortcut to using objects... On your last explanation: ++retval[…] Would that be considered a unary operator? Also, when evaluating conditions like this: Code:
if(!retval.total)Like this: Code:
if(!retval.total) |
Quote:
Do you remember how long it took for you to get a good solid understanding of JS? Was it 1 month? 6 months? a full year? |
You can return nothing like there, but! That means the render function will generate an error unless modified, but in that condition it would be wrong to change, as no inputs were found. Only the "if-browser-does-not-support-getElementsByTagName" you should return void and then check for void return in the Render function.
As for the "object literal" or closure object, its one of the things I would really like with real closures now in PHP 5.3, but lets hope they can be in 6.0 if someone makes a patch for it :) |
Quote:
|
Quote:
Quote:
|
Quote:
PHP Code:
$retval->total and $retval['total']) if you wanted/needed them both.Quote:
Quote:
|
Quote:
As for the ArrayObject, any class that implements the ArrayAccess interface should be able to be accessed like arrays and objects :) |
Hey guys, quick question, this goes a little back when Salathe suggested to use onLoad in the Body tag...
Here's what I currently have and it works... shortened code: Code:
<body>Code:
<body onLoad="showCount();"> |
Quote:
So I now learned that in JS you don't have to do backward slashes if you use double quotes. var test = "today's a great day"; But in PHP you do... $test = "today\'s a great day"; Unless you use single quotes in JS, then you would have to use backslash too... Do you know if PHP is going to do something like this in the future so we don't have to use backslash if using double quotes? |
| All times are GMT. The time now is 05:29 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0