Skip to content
Fix Code Error

How to get the selected radio button’s value?

March 13, 2021 by Code Error
Posted By: Anonymous

I’m having some strange problem with my JS program. I had this working properly but for some reason it’s no longer working. I just want to find the value of the radio button (which one is selected) and return it to a variable. For some reason it keeps returning undefined.

Here is my code:

function findSelection(field) {
    var test = 'document.theForm.' + field;
    var sizes = test;

    alert(sizes);
        for (i=0; i < sizes.length; i++) {
            if (sizes[i].checked==true) {
            alert(sizes[i].value + ' you got a value');     
            return sizes[i].value;
        }
    }
}

submitForm:

function submitForm() {

    var genderS =  findSelection("genderS");
    alert(genderS);
}

HTML:

<form action="#n" name="theForm">

    <label for="gender">Gender: </label>
    <input type="radio" name="genderS" value="1" checked> Male
    <input type="radio" name="genderS" value="0" > Female<br><br>
    <a href="javascript: submitForm()">Search</A>
</form>

Solution

You can do something like this:

var radios = document.getElementsByName('genderS');

for (var i = 0, length = radios.length; i < length; i++) {
  if (radios[i].checked) {
    // do whatever you want with the checked radio
    alert(radios[i].value);

    // only one radio can be logically checked, don't check the rest
    break;
  }
}
<label for="gender">Gender: </label>
<input type="radio" name="genderS" value="1" checked="checked">Male</input>
<input type="radio" name="genderS" value="0">Female</input>

jsfiddle

Edit: Thanks HATCHA and jpsetung for your edit suggestions.

Answered By: Anonymous

Related Articles

  • Active tab issue on page load HTML
  • How To Check A Radio Button in Vue.JS 2 WITHOUT…
  • How do I limit the number of digits from 6 to 4 in…
  • Customize Bootstrap checkboxes
  • How to enforce required paper-radio-group in Polymer?
  • How does one validate paper-radio-group element with…
  • Js Calculation based on radio button, and some problems
  • Jquery If radio button is checked
  • How can i display data from xml api in flutter?
  • Vue - Multiple radio inputs, checked value?
  • Polymer 3: How to implement two way data binding for…
  • Polymer Paper-Radio-Group - how to have multiple…
  • Polymer Paper radio group post value
  • Polymer 1.x: Observers
  • How to make a radio button look like a toggle button
  • Javascript validate all checkboxes are selected
  • Get selected button's value from Polymer radio group…
  • Vuetify radio button not appearing as checked
  • Radio Buttons ng-checked with ng-model
  • backbone.js event binding is breaking radio buttons…
  • The radio button is not checked (turning blue)
  • Onclick, the button moves down, why can that due to?
  • Fastest way to iterate over all the chars in a String
  • Sort table rows In Bootstrap
  • paper-radio button does not update properly
  • Vue.js v-model with dynamic list of radio buttons
  • setTimeout function not working : javascript
  • How do i update a javascript variable as its value changes?
  • Vue v-model not reactive with BS4 radio button group
  • Not submit any value if checkbox is empty
  • Polymer 2.x paper-radio-group selectedValues not working
  • paper-radio-group-changed not firing when a radio…
  • paper-radio-button ripple persist after selection change
  • changing the style of radio button in vue js
  • Vue-strap Radio Buttons not working with vue.js
  • CSS - How to Style a Selected Radio Buttons Label?
  • Use images instead of radio buttons
  • How do I style (css) radio buttons and labels?
  • How to dynamically add and remove views with Ember.js
  • laravel vuejs/axios put request Formdata is empty
  • change img src based on conditional statement vuejs
  • how to use canvas in JavaScript flappy bird code
  • Browser page keeps refreshing when testing Backbone…
  • Getting the value of a paper-radio-button inside a template
  • In jQuery, how do I get the value of a radio button…
  • VueJS Master Checkbox Toggle with array values
  • Smart way to truncate long strings
  • Calling Javascript from a html form
  • .attr('checked','checked') does not work
  • Changing Icon Color behind ListTile in an…
  • Bootstrap radio button "checked" flag
  • Remove object key by spread operated returns undefined
  • Test a Form submission with jasmine and backbone.
  • ASP.NET postback with JavaScript
  • Vuetify/Vuejs radio group and checkbox group set to…
  • How do I create a select all checkbox when I have an…
  • easiest way to extract Oracle form xml format data
  • CSS selector for a checked radio button's label
  • Uncheck radio button
  • Input radio options to be displayed on click
  • OnChange event handler for radio button (INPUT…
  • How to make onClick card to move to another page in react
  • Dynamic label "for" attribute in Polymer
  • Ember yield multiple actions into component
  • I have these 2 tonnage calculators. The first one is…
  • Keep button focused when using mulitple button groups
  • How to change the color of vaadin-select-text-field…
  • jQuery Mobile: document ready vs. page events
  • How to set checked attribute to radio in litelement
  • How to select radio button by clicking its div?
  • Polymer multiple iron-form submissions
  • Can I set variables to undefined or pass undefined…
  • Refresh Page and Keep Scroll Position
  • How to store the last checked item as default, when…
  • Polymer 1.x: Databinding Arrays
  • How to end C++ code
  • Javascript change color of text and background to…
  • Dynamically update values of a chartjs chart
  • Dynamically link radio button tabs to content div
  • How to reset radiobuttons in jQuery so that none is checked
  • How do I keep only the first map and when the game…
  • Why doesnt my table sort my div variable in numerical order?
  • How does PHP 'foreach' actually work?
  • DOM is not updating when props value changes in Vuejs
  • Get multiple values of selected paper-radio-button
  • Vuejs radio buttons checked if v-model is an array
  • paper-tabs event listener to change selected?
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Add attribute 'checked' on click jquery
  • form action with javascript
  • Vue + VUEX + Typescript + Vue Router. component not…
  • Why jQuery returning value of radio buttons as "ON"?…
  • How to create an alert the fade after a duration in Vuetify?
  • jQuery AJAX submit form
  • Uncheck vue radio button from another button
  • Setting my model with Backbone.js
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How to combine the data from two different…
  • VueJs Checked Radio Button group
  • How can I pass a wct test while rearranging children…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Could not load file or assembly or one of its dependencies

Next Post:

Deserialize JSON into C# dynamic object?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error