I'm dynamically (PHP & Mysql) building a list containing names and a radio button for each name. I'm using radio buttons because I want only only one button to be selected. I want a JavaScript to determine whether a button is selected or not. That's usually no problem. Let's say the name of the radio button group is 'test'. Then I can use 'document.form.test.length' to determine the number of buttons and then make a loop to see if any of them is selected. Now to the problem. Sometimes there is only one button in the list and my method only works for two or more. If there are three buttons the length is 3, if there are two it's 2, but if there is only one it's not 1 but 'undefined' so I can't make a loop. Any suggestions?
you can make a check while getting the length for example; if(document.form.test.length != undefined) { //your loop code here } else { //no need to loop here, only one radio here, so check will be accordingly.. } hope will be helpful..