Re: Radio button group

As far as I know (unless misunderstanding) the problem in your concern is  
that name and id attributes of an element are not the same thing, as they  
may differ, and the "getElementById" method should return only one element  
relying on its set id value, not its name (the behaviour you saw should be  
a non standard compatibility issue, as well as a compatibility issue is  
giving the same value to an anchor name and id to create a fragment valid  
both as an xhtml one and an html 4.01 one). Furthermore, what you aspect  
to recieve in return for your call is not an Element, but a Collection. An  
easy (and maybe elegant) way to achieve what you aim might be the  
following: group your radio buttons in a fildset with an explicit and
unique id (let's say "radioGroup"), give the enclosing form an id equal to  
its name (I mean the form name), and obtain  the fildset childNodes  
calling document.getElementById("formname").childNodes. So doing you'll  
get a NodeList you can traverse with a for statement using its length  
attribute and canonical "item(index)" method (but usually browsers should  
allow you to use it like an array; I prefer the standard form however).  
Depending on the programming language used you might have to cast each  
item too. The same behaviour is achievable with a div element, instead of  
the fildset one, but I found the fildset is more context-specific and so  
being more elegant (maybe there could be an even more elegant way using  
DOM document traversal, but not all implementation out there are fully  
compliant, as far as I've experienced - that is an html document is  
instance of HTMLDocument, Document and Node interfaces, of course, but may  
not be instance of DocumentTraversal too).

If you have to manipulate predefined document structures (i.e. an html  
document written by someone else) the only solution may be a search  
function, that is something like:

function getRadioGroup(formName, radioName){
	var radio;
	var radioGroup = new Array();
	var formInputs =  
document.getElementById(formName).getElementsByTagName("input");

	for( var i = 0; i < formInputs.length; i++){
		radio = formInputs.item(i);
		if( (radio.type == "radio") && (radio.name == radioName) )
			radioGroup.push(radio);
	}
	
	return radioGroup;
}

I don't know how to use the XPath as suggested in previous reply. I hope  
my answer will be useful to you. Best reguards.
 
 
 --
 Email.it, the professional e-mail, gratis per te: http://www.email.it/f

 
 Sponsor:
 Telefona con Email.it Phone Card, tanti minuti di conversazione con il massimo del risparmio, clicca qui
 Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=2687&d=19-3

Received on Sunday, 19 March 2006 19:51:54 UTC