Good morning
I am having some difficuly in looping through a radiobutton list in order to check if it was selected or not using Javascript.
With C# Asp.net the procedure is relatively easy but with the Javascript I am struggling a little.
This is the code I use with C# to check if the radion button was selected.
protected void Button1_Click(object sender, EventArgs e)
{
string radValue = RadioButtonList1.SelectedValue.ToString();
if (radValue == "")
{
lblError.Text = "Please select neigbourhood";
}
else
{
lblError.Text = "You come from " + radValue;
}
}
The code I use with javascript is a little faulty and I was hoping it could be corrected.
var radNeighbourhood;
for(var loop=0; loop < document.subscribeForm.myRadio.length; loop++)
{
if(document.subscribeForm.myRadio[loop].checked == true)
{
radNeighbourhood = document.subscribeForm.myRadio[loop].value;
break;
}
else
{
alert("Please select a neigbourhood");
return false;
}
}
return true;
Kind regards
Arian
I made a small sample of what you ‘re asking here. http://jsfiddle.net/mZhQ9/2/
EDIT: analysis
EDIT 2: As a sidenote please be careful of using
“for(var loop=0; loop < document.subscribeForm.myRadio.length; loop++)”
DOM operations within a loop. the
loop < document.subscribeForm.myRadio.length condition checks the document and grabs the radio buttons every single time, resulting in lots of unecessary overhead.