PDA

View Full Version : The number of radio elements in a form


Akky
12-22-2002, 10:19 AM
Hi Everybody
I have a interesting problem. This page given below will display two radio buttons, on click event will show an alert of the number of radio buttons(ie 2 in this case).
If I remove one of the radio buttons then the alert shows the length as undefined. I am adding the radio buttons dynamically and need to know the current numbers present
Any help is highly appreciated

Regards
Akky

<html>
<head>
<script language="JavaScript">
function radioSelected() { alert(document.searchFolderContents.search.length); }
</script>
</head>

<body>
<form method="post" action="" name="searchFolderContents">
<input type="radio" name="search" value="1" onclick="radioSelected()">
<input type="radio" name="search" value="2" onclick="radioSelected()">
</form>
</body>
</html>

mordred
12-22-2002, 02:00 PM
I would just test if the .length property is defined, and if it's not, the "length" of the radio buttons must be 1. Try it like this:


function radioSelected() {
var search = document.searchFolderContents.search
var radioLength = (typeof search.length == "number") ? (search.length) : 1;
alert(radioLength);
}

Mr J
12-22-2002, 03:00 PM
Another alternative ... maybe




<html>
<head>
<script language="JavaScript">
function radioSelected() {
count=0
for(i=0;i<document.searchFolderContents.length;i++){
if(document.searchFolderContents.elements.type="radio"){
count++
}
}
alert(count);
}
</script>
</head>

<body>
<form method="post" action="" name="searchFolderContents">
<input type="radio" name="search" value="1" onclick="radioSelected()">
<input type="radio" name="search" value="1" onclick="radioSelected()">
</form>
</body>
</html>

Akky
12-24-2002, 05:11 AM
Thank You Mr J
:p

Your reply helped to solve the situation

Thank You everyone for their efforts

This is a very useful site

Akky