Jump to content

DOM and radio button length


Guest mac@dng.net

Recommended Posts

Guest mac@dng.net

Hi all! first post here so please be nice to me! :)I'm new to the DOM thing and I'm quite lost.How, using getElementById, can I get the number of radio buttons contained in a list?I tried :document.getElementById('radio button name').lengthdocument.getElementById('radio button name').sizedocument.getElementById('radio button name').options.sizeI'm growing out of ideas. can anyone help, please?Thanks in advance for your time.

Link to comment
Share on other sites

As far as I know you have to approach a radio set as an associative array.That means you'll need the element name.

<script type="text/javascript"> function checkPets(cfield, s){ var fieldName = document.getElementById(s).name; var p = document.forms['f1'].elements[fieldName];   if(cfield.checked) {       for(var i = 0; i < p.length; i++){         //remove the next line if you don't want them unchecked         p[i].checked = false;         p[i].disabled = true;         }     }   else {       for(var j = 0; j < p.length; j++){         p[j].disabled = false;         }     }      } </script> </head> <body> <form name="f1"> <input type="radio" id="Kind" name="Pet" value="Cat"> Cat <br> <input type="radio" id="Kind" name="Pet" value="Dog"> Dog <br> <input type="radio" id="Kind" name="Pet" value="Snake"> Snake <br> <input type="checkbox" name="c1" onclick="checkPets(this, 'Kind')"> Don't Like Any Pets <b>Especially Snakes</b> </form>

--Was that nice enough -- :) Shout back if you need more,

Link to comment
Share on other sites

Yeah, it is nice :)Only comment, ID attributes must only be unique, several same IDs are prohibited in strict markup. And this may be shortened out:

var fieldName = document.getElementById(s).name;var p = document.forms['f1'].elements[fieldName];
To:
var fieldList = document.forms.f1.Pet
(and continue using 'fieldList' instead of 'p')
Link to comment
Share on other sites

<html><body id="thing"><a href="tesdsdsdt">tesdsst</a><a href="test">test</a><h3>Explanation:</h3><p>What this will do is first put the container into a variable. Then it will check for the tag specified by the coder and alert it's length. Because there are two 'a' tags above, it will alert '2'. Hope this is what you want.</p><script type="text/javascript">////////////////////////////////////////////////////////////tagthing="a" //Put element tag here without the < and >'scontainerthing="thing" //Put the ID of the container of the elements here////////////////////////////////////////////////////////////bodthing=document.getElementById(containerthing)elements = bodthing.getElementsByTagName(tagthing)x=elements.lengthalert(x)</script></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...