Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6125809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:17:27+00:00 2026-05-23T16:17:27+00:00

Good morning I am having some difficuly in looping through a radiobutton list in

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:17:28+00:00Added an answer on May 23, 2026 at 4:17 pm

    I made a small sample of what you ‘re asking here. http://jsfiddle.net/mZhQ9/2/

    EDIT: analysis

    var radioButtons = document.subscribeForm.myRadio; //it is crucial to store the DOM information in a variable instead of grabbing it, each single time. (DOM operations are EXTREMELY slow)
    var len = radioButtons.length; //same as before
    var found = false; //our flag - whether something was found or not
    
    while( len-- > 0 ) { //decreasing the counter (length of radio buttons)
        if( radioButtons[len].checked === true ) { //if we find one that is checked
            found = true; //set the flag to true
            break; //escape the loop
        }
    } 
    
    if( found ) { //if our flag is set to true
        alert( radioButtons[len].value );
        return radioButtons[len].value; //return the value of the checked radiobutton (remember, when we broke from the While-loop, the len value remained at the 'checked' radio button position)
    }
    else { 
        alert( "Please select a neigbourhood" );
        return false; //else return false
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning! I'm programming a managment aplication that shows a task list using WPF
Hello good morning everyone, i am having a weird issue when using imagePickerController. Actually
Good morning. As the title indicates, I've got some questions about using python for
Good morning all, I'm having a few problems with a method in my C#
Good morning, I am working on a C# winform application that is using validation
Good Morning, i have some Trouble with the Zend Framework and Zend_DB_Select, i want
Good morning fellows, I am having an issue with facebook api: is it possible
Good morning, I'm having a really annoying issue with Highcharts, such that the x-axis
Good morning! I'm having great success so far with CodeIgniter. I'm new to PHP
Good morning, I a linking to an Oracle Data (table) through ODBC. I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.