getElementById is going to return a reference to an individual element, not to the array that the browser creates when you give a group of elements the same name. Also id's must be unique unlike names. You don't need to test for
document.all to access the forms collections, so I don't think you'll have a problem retrieving a reference to the array like this:
srcf = document.forms[0].elements["opt_tye"];
// edit
I was thinking about this a bit more and I'm not sure you would need access to the array to do what you want if you passed
this as an argument to the function instead of an integer:
<input type="radio" name="opt_tye" onclick="seton('basic', this);">
function seton(row, opt){
document.getElementById(row).bgColor = '#EEEEEE';
opt.checked = true;
}
- function seton(row, opt){
- document.getElementById(row).bgColor = '#EEEEEE';
- opt.checked = true;
- }
When I tested this and set the value of one radio button programatically the browsers (IE 6, FF1.0.4 and Opera 8 ) automatically set the other buttons in the group to false.
I was trying to think of a way to get the radio group array with DOM methods if that's what you really wanted to do. I don't think that array is part of the DOM (I'd be happy to be corrected if I'm wrong), so the best I could come up with was to make a function that would get a NodeList of all the input elements and then check them for their
name attributes to build and return an array of the group in question:
function getRadioArray(groupName) {
var retval = new Array();
var nodeList = document.getElementsByTagName("input");
for(var i = 0; i < nodeList.length; i++) {
if(nodeList.item(i).name == groupName) retval[i] = nodeList.item(i);
}
return retval;
}
- function getRadioArray(groupName) {
- var retval = new Array();
- var nodeList = document.getElementsByTagName("input");
- for(var i = 0; i < nodeList.length; i++) {
- if(nodeList.item(i).name == groupName) retval[i] = nodeList.item(i);
- }
- return retval;
- }
then you could call it with:
var srcf = getRadioArray("opt_tye");
or
var srcf = getRadioArray(opt.name);
if you were passing
this into the
opt parameter
I'm sure it's just a typo, but don't forget to use camelCase for .bgColor
Free Programming Resources