How to get the selected radio button value using js












48















I am using this code to get the value of currently selected radio button, but it doesn't work.



var mailcopy = document.getElementById('mailCopy').value; 


How to get the currently selected radio button value using Javascript?










share|improve this question

























  • If this method does work already, why're you looking for another?

    – Nikita Rybak
    Oct 6 '10 at 4:46






  • 2





    I think there was a typo that left out a "not"

    – Quentin
    Oct 6 '10 at 6:28











  • the question should change to "it does NOT return the selected..."!

    – GMsoF
    Apr 22 '13 at 6:47






  • 1





    Is this still an problem, as you have not accepted an answer!

    – pattyd
    Jul 22 '13 at 20:36
















48















I am using this code to get the value of currently selected radio button, but it doesn't work.



var mailcopy = document.getElementById('mailCopy').value; 


How to get the currently selected radio button value using Javascript?










share|improve this question

























  • If this method does work already, why're you looking for another?

    – Nikita Rybak
    Oct 6 '10 at 4:46






  • 2





    I think there was a typo that left out a "not"

    – Quentin
    Oct 6 '10 at 6:28











  • the question should change to "it does NOT return the selected..."!

    – GMsoF
    Apr 22 '13 at 6:47






  • 1





    Is this still an problem, as you have not accepted an answer!

    – pattyd
    Jul 22 '13 at 20:36














48












48








48


9






I am using this code to get the value of currently selected radio button, but it doesn't work.



var mailcopy = document.getElementById('mailCopy').value; 


How to get the currently selected radio button value using Javascript?










share|improve this question
















I am using this code to get the value of currently selected radio button, but it doesn't work.



var mailcopy = document.getElementById('mailCopy').value; 


How to get the currently selected radio button value using Javascript?







javascript radio-button






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 26 '16 at 16:22









Damjan Pavlica

8,78952752




8,78952752










asked Oct 6 '10 at 4:40









MeenaMeena

50241634




50241634













  • If this method does work already, why're you looking for another?

    – Nikita Rybak
    Oct 6 '10 at 4:46






  • 2





    I think there was a typo that left out a "not"

    – Quentin
    Oct 6 '10 at 6:28











  • the question should change to "it does NOT return the selected..."!

    – GMsoF
    Apr 22 '13 at 6:47






  • 1





    Is this still an problem, as you have not accepted an answer!

    – pattyd
    Jul 22 '13 at 20:36



















  • If this method does work already, why're you looking for another?

    – Nikita Rybak
    Oct 6 '10 at 4:46






  • 2





    I think there was a typo that left out a "not"

    – Quentin
    Oct 6 '10 at 6:28











  • the question should change to "it does NOT return the selected..."!

    – GMsoF
    Apr 22 '13 at 6:47






  • 1





    Is this still an problem, as you have not accepted an answer!

    – pattyd
    Jul 22 '13 at 20:36

















If this method does work already, why're you looking for another?

– Nikita Rybak
Oct 6 '10 at 4:46





If this method does work already, why're you looking for another?

– Nikita Rybak
Oct 6 '10 at 4:46




2




2





I think there was a typo that left out a "not"

– Quentin
Oct 6 '10 at 6:28





I think there was a typo that left out a "not"

– Quentin
Oct 6 '10 at 6:28













the question should change to "it does NOT return the selected..."!

– GMsoF
Apr 22 '13 at 6:47





the question should change to "it does NOT return the selected..."!

– GMsoF
Apr 22 '13 at 6:47




1




1





Is this still an problem, as you have not accepted an answer!

– pattyd
Jul 22 '13 at 20:36





Is this still an problem, as you have not accepted an answer!

– pattyd
Jul 22 '13 at 20:36












17 Answers
17






active

oldest

votes


















82














HTML



<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male">Male<br>
<input type="radio" id="gender1" name="gender" value="Female">Female<br>


JS



var gender = document.querySelector('input[name = "gender"]:checked').value;
document.writeln("You entered " + gender + " for your gender<br>");





share|improve this answer


























  • Lovely. And no need for a parent element.

    – AlikElzin-kilaka
    Jan 7 '15 at 15:33






  • 6





    You can't have two elements with the same id.

    – mbomb007
    Apr 23 '15 at 15:32











  • @mbomb007 i don't see two elements having same id here

    – Nabeel Khan
    May 14 '16 at 7:39






  • 3





    @NabeelKhan The answer was edited after I said that. View the edit history.

    – mbomb007
    May 15 '16 at 0:48





















45














If you are using jQuery, following code will work for you.



$('input[name=radioName]:checked').val();





share|improve this answer































    34














    Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.



    function getCheckedRadio(radio_group) {
    for (var i = 0; i < radio_group.length; i++) {
    var button = radio_group[i];
    if (button.checked) {
    return button;
    }
    }
    return undefined;
    }
    var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
    if (checkedButton) {
    alert("The value is " + checkedButton.value);
    }





    share|improve this answer
























    • Good for compatibility, this method works for older browsers too, pre HTML 5.

      – j.c
      Nov 25 '16 at 5:03



















    4














    check this



    <input class="gender" type="radio" name="sex" value="male">Male
    <br>
    <input class="gender" type="radio" name="sex" value="female">Female


    <script type="text/javascript">
    $(document).ready(function () {
    $(".gender").change(function () {

    var val = $('.gender:checked').val();
    alert(val);
    });
    });

    </script>


    Example






    share|improve this answer
























    • my requirement is quite different, the alert box will come but at the click on the button

      – Akash
      Oct 30 '15 at 11:31



















    3














    Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:



    var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;


    ... which is only valid of course if have provided "value" for your radio input elements.



    <input type="radio" name="radio-group-name" value="red" checked>
    <input type="radio" name="radio-group-name" value="blue">


    The value should be either 'red' or 'blue' in the above example.






    share|improve this answer































      2














      function getCheckedValue(radioObj, name) {

      for (j = 0; j < radioObj.rows.length; ++j) {
      for (k = 0; k < radioObj.cells.length; ++k) {
      var radioChoice = document.getElementById(name + "_" + k);
      if (radioChoice.checked) {
      return radioChoice.value;
      }
      }
      }
      return "";
      }





      share|improve this answer

































        1














        A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
        I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.



        you can also do this with the id, but you usually just want the value.



        <script>
        var gRadioValue = ''; //global declared outside of function
        function myRadioFunc(){
        var radioVal = gRadioValue;
        // or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
        //do somethign with radioVal
        }
        <script>

        <input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
        <input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
        ...
        <input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99





        share|improve this answer

































          0














          you can use this



          $('input[name="field_value"]:checked').val(); 


          or, for older version of jquery



          $('input[@name="field_value"]:checked').val();





          share|improve this answer

































            0














            Since you want to get it using plain javascript, you can use the following code



            var val = '';
            if(document.getElementById('radio1').checked) {
            val = document.getElementById('radio1').value
            }else if(document.getElementById('radio2').checked) {
            val = document.getElementById('radio2').value
            }





            share|improve this answer































              0














              Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).



              I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked function. If this is set to true I can change the value of another variable.






              function createOutput() {
              var order = "in";
              if (document.getElementById('radio1').checked == true) {
              order = "pre";
              } else if (document.getElementById('radio2').checked == true) {
              order = "in";
              } else if (document.getElementById('radio3').checked == true) {
              order = "post";
              }
              document.getElementById('outputBox').innerHTML = order;
              }

              <input id="radio1" type="radio" name="order" value="preorder" />Preorder
              <input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
              <input id="radio3" type="radio" name="order" value="postorder" />Postorder
              <button onclick="createOutput();">Generate Output</button>
              <textarea id="outputBox" rows="10" cols="50"></textarea>





              Hope this is useful, in someway,



              Beanz






              share|improve this answer































                0














                You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.






                function getSelectedValue() {
                var radioBtns = document.getElementsByClassName("radioBtn");
                for(var i = 0; i < radioBtns.length; i++){
                if(radioBtns[i].checked){
                document.getElementById("output").textContent = radioBtns[i].value;
                }
                }
                }

                <input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
                <input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
                <input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
                <button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
                <textarea id="output"></textarea>








                share|improve this answer































                  0














                  all of you can test this example and easy to understand.



                          Name:   <input type="text" id="text" class ="input">
                  <input type="text" id="text1" class ="input">
                  Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
                  <input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
                  Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
                  <input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
                  <input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
                  <button type="button" id="b1">show</button>


                  // javascript



                      <script>
                  function getData(input){
                  return document.getElementById(input).value;
                  }
                  function dataByClassName(st){
                  var value=document.getElementsByClassName(st)
                  for(var i=0;i < value.length;i++){
                  if(value[i].checked){
                  return value[i].value;
                  }
                  }
                  }
                  document.getElementById("b1").onclick = function ()
                  {
                  var st={
                  name : getData("text")+getData("text1"),
                  gender : dataByClassName("Rm"),
                  course : dataByClassName("cm")
                  };
                  alert(st.name+" "+st.gender+" "+st.course);
                  };

                  </script>





                  share|improve this answer































                    0














                    Try this, I hope this one will work



                    function loadRadioButton(objectName, selectedValue) {

                    var radioButtons = document.getElementsByName(objectName);

                    if (radioButtons != null) {
                    for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
                    if (radioButtons[radioCount].value == selectedValue) {
                    radioButtons[radioCount].checked = true;
                    }
                    }
                    }
                    }





                    share|improve this answer

































                      -1














                      If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:



                      function selectedRadio() {
                      var radio = document.getElementsByName('mailCopy');
                      alert(radio[0].value);
                      }


                      Notes:




                      • In general for the inputs you want to have unique IDs (not a requirement but a good practice)

                      • All the radio inputs that are from the same group MUST have the same name attribute, for example

                      • You have to set the value attribute for each input


                      Here is an example of input radios:



                      <input type="radio" name="mailCopy" value="1" />1<br />
                      <input type="radio" name="mailCopy" value="2" />2<br />





                      share|improve this answer































                        -2














                        var mailcopy = document.getElementById('mailCopy').checked; 

                        if(mailcopy==true)
                        {
                        alert("Radio Button Checked");
                        }
                        else
                        {
                        alert("Radio Button un-Checked");
                        }





                        share|improve this answer



















                        • 3





                          The question is looking to get the currently selected button, not find out if a specific button is checked.

                          – Quentin
                          Oct 6 '10 at 6:23



















                        -2














                        Hy, you have to do it this way.



                        function checkRadio () {
                        if(document.getElementById('user1').checked) {
                        return $('#user1').val();
                        }else if(document.getElementById('user2').checked) {
                        return $('#user2').val();
                        }
                        }





                        share|improve this answer































                          -3














                          Use the element.checked property.






                          share|improve this answer


























                          • thank u for your reply . can u explain in clear

                            – Meena
                            Oct 6 '10 at 5:01











                          Your Answer






                          StackExchange.ifUsing("editor", function () {
                          StackExchange.using("externalEditor", function () {
                          StackExchange.using("snippets", function () {
                          StackExchange.snippets.init();
                          });
                          });
                          }, "code-snippets");

                          StackExchange.ready(function() {
                          var channelOptions = {
                          tags: "".split(" "),
                          id: "1"
                          };
                          initTagRenderer("".split(" "), "".split(" "), channelOptions);

                          StackExchange.using("externalEditor", function() {
                          // Have to fire editor after snippets, if snippets enabled
                          if (StackExchange.settings.snippets.snippetsEnabled) {
                          StackExchange.using("snippets", function() {
                          createEditor();
                          });
                          }
                          else {
                          createEditor();
                          }
                          });

                          function createEditor() {
                          StackExchange.prepareEditor({
                          heartbeatType: 'answer',
                          autoActivateHeartbeat: false,
                          convertImagesToLinks: true,
                          noModals: true,
                          showLowRepImageUploadWarning: true,
                          reputationToPostImages: 10,
                          bindNavPrevention: true,
                          postfix: "",
                          imageUploader: {
                          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                          allowUrls: true
                          },
                          onDemand: true,
                          discardSelector: ".discard-answer"
                          ,immediatelyShowMarkdownHelp:true
                          });


                          }
                          });














                          draft saved

                          draft discarded


















                          StackExchange.ready(
                          function () {
                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3869535%2fhow-to-get-the-selected-radio-button-value-using-js%23new-answer', 'question_page');
                          }
                          );

                          Post as a guest















                          Required, but never shown

























                          17 Answers
                          17






                          active

                          oldest

                          votes








                          17 Answers
                          17






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes









                          82














                          HTML



                          <p>Gender</p>
                          <input type="radio" id="gender0" name="gender" value="Male">Male<br>
                          <input type="radio" id="gender1" name="gender" value="Female">Female<br>


                          JS



                          var gender = document.querySelector('input[name = "gender"]:checked').value;
                          document.writeln("You entered " + gender + " for your gender<br>");





                          share|improve this answer


























                          • Lovely. And no need for a parent element.

                            – AlikElzin-kilaka
                            Jan 7 '15 at 15:33






                          • 6





                            You can't have two elements with the same id.

                            – mbomb007
                            Apr 23 '15 at 15:32











                          • @mbomb007 i don't see two elements having same id here

                            – Nabeel Khan
                            May 14 '16 at 7:39






                          • 3





                            @NabeelKhan The answer was edited after I said that. View the edit history.

                            – mbomb007
                            May 15 '16 at 0:48


















                          82














                          HTML



                          <p>Gender</p>
                          <input type="radio" id="gender0" name="gender" value="Male">Male<br>
                          <input type="radio" id="gender1" name="gender" value="Female">Female<br>


                          JS



                          var gender = document.querySelector('input[name = "gender"]:checked').value;
                          document.writeln("You entered " + gender + " for your gender<br>");





                          share|improve this answer


























                          • Lovely. And no need for a parent element.

                            – AlikElzin-kilaka
                            Jan 7 '15 at 15:33






                          • 6





                            You can't have two elements with the same id.

                            – mbomb007
                            Apr 23 '15 at 15:32











                          • @mbomb007 i don't see two elements having same id here

                            – Nabeel Khan
                            May 14 '16 at 7:39






                          • 3





                            @NabeelKhan The answer was edited after I said that. View the edit history.

                            – mbomb007
                            May 15 '16 at 0:48
















                          82












                          82








                          82







                          HTML



                          <p>Gender</p>
                          <input type="radio" id="gender0" name="gender" value="Male">Male<br>
                          <input type="radio" id="gender1" name="gender" value="Female">Female<br>


                          JS



                          var gender = document.querySelector('input[name = "gender"]:checked').value;
                          document.writeln("You entered " + gender + " for your gender<br>");





                          share|improve this answer















                          HTML



                          <p>Gender</p>
                          <input type="radio" id="gender0" name="gender" value="Male">Male<br>
                          <input type="radio" id="gender1" name="gender" value="Female">Female<br>


                          JS



                          var gender = document.querySelector('input[name = "gender"]:checked').value;
                          document.writeln("You entered " + gender + " for your gender<br>");






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 10 '17 at 8:33









                          orange01

                          450714




                          450714










                          answered Jul 22 '13 at 20:28









                          Mihai AlinMihai Alin

                          81964




                          81964













                          • Lovely. And no need for a parent element.

                            – AlikElzin-kilaka
                            Jan 7 '15 at 15:33






                          • 6





                            You can't have two elements with the same id.

                            – mbomb007
                            Apr 23 '15 at 15:32











                          • @mbomb007 i don't see two elements having same id here

                            – Nabeel Khan
                            May 14 '16 at 7:39






                          • 3





                            @NabeelKhan The answer was edited after I said that. View the edit history.

                            – mbomb007
                            May 15 '16 at 0:48





















                          • Lovely. And no need for a parent element.

                            – AlikElzin-kilaka
                            Jan 7 '15 at 15:33






                          • 6





                            You can't have two elements with the same id.

                            – mbomb007
                            Apr 23 '15 at 15:32











                          • @mbomb007 i don't see two elements having same id here

                            – Nabeel Khan
                            May 14 '16 at 7:39






                          • 3





                            @NabeelKhan The answer was edited after I said that. View the edit history.

                            – mbomb007
                            May 15 '16 at 0:48



















                          Lovely. And no need for a parent element.

                          – AlikElzin-kilaka
                          Jan 7 '15 at 15:33





                          Lovely. And no need for a parent element.

                          – AlikElzin-kilaka
                          Jan 7 '15 at 15:33




                          6




                          6





                          You can't have two elements with the same id.

                          – mbomb007
                          Apr 23 '15 at 15:32





                          You can't have two elements with the same id.

                          – mbomb007
                          Apr 23 '15 at 15:32













                          @mbomb007 i don't see two elements having same id here

                          – Nabeel Khan
                          May 14 '16 at 7:39





                          @mbomb007 i don't see two elements having same id here

                          – Nabeel Khan
                          May 14 '16 at 7:39




                          3




                          3





                          @NabeelKhan The answer was edited after I said that. View the edit history.

                          – mbomb007
                          May 15 '16 at 0:48







                          @NabeelKhan The answer was edited after I said that. View the edit history.

                          – mbomb007
                          May 15 '16 at 0:48















                          45














                          If you are using jQuery, following code will work for you.



                          $('input[name=radioName]:checked').val();





                          share|improve this answer




























                            45














                            If you are using jQuery, following code will work for you.



                            $('input[name=radioName]:checked').val();





                            share|improve this answer


























                              45












                              45








                              45







                              If you are using jQuery, following code will work for you.



                              $('input[name=radioName]:checked').val();





                              share|improve this answer













                              If you are using jQuery, following code will work for you.



                              $('input[name=radioName]:checked').val();






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 24 '12 at 10:49









                              Chamika SandamalChamika Sandamal

                              19.4k35176




                              19.4k35176























                                  34














                                  Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.



                                  function getCheckedRadio(radio_group) {
                                  for (var i = 0; i < radio_group.length; i++) {
                                  var button = radio_group[i];
                                  if (button.checked) {
                                  return button;
                                  }
                                  }
                                  return undefined;
                                  }
                                  var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
                                  if (checkedButton) {
                                  alert("The value is " + checkedButton.value);
                                  }





                                  share|improve this answer
























                                  • Good for compatibility, this method works for older browsers too, pre HTML 5.

                                    – j.c
                                    Nov 25 '16 at 5:03
















                                  34














                                  Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.



                                  function getCheckedRadio(radio_group) {
                                  for (var i = 0; i < radio_group.length; i++) {
                                  var button = radio_group[i];
                                  if (button.checked) {
                                  return button;
                                  }
                                  }
                                  return undefined;
                                  }
                                  var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
                                  if (checkedButton) {
                                  alert("The value is " + checkedButton.value);
                                  }





                                  share|improve this answer
























                                  • Good for compatibility, this method works for older browsers too, pre HTML 5.

                                    – j.c
                                    Nov 25 '16 at 5:03














                                  34












                                  34








                                  34







                                  Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.



                                  function getCheckedRadio(radio_group) {
                                  for (var i = 0; i < radio_group.length; i++) {
                                  var button = radio_group[i];
                                  if (button.checked) {
                                  return button;
                                  }
                                  }
                                  return undefined;
                                  }
                                  var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
                                  if (checkedButton) {
                                  alert("The value is " + checkedButton.value);
                                  }





                                  share|improve this answer













                                  Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.



                                  function getCheckedRadio(radio_group) {
                                  for (var i = 0; i < radio_group.length; i++) {
                                  var button = radio_group[i];
                                  if (button.checked) {
                                  return button;
                                  }
                                  }
                                  return undefined;
                                  }
                                  var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
                                  if (checkedButton) {
                                  alert("The value is " + checkedButton.value);
                                  }






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Oct 6 '10 at 6:22









                                  QuentinQuentin

                                  646k718761041




                                  646k718761041













                                  • Good for compatibility, this method works for older browsers too, pre HTML 5.

                                    – j.c
                                    Nov 25 '16 at 5:03



















                                  • Good for compatibility, this method works for older browsers too, pre HTML 5.

                                    – j.c
                                    Nov 25 '16 at 5:03

















                                  Good for compatibility, this method works for older browsers too, pre HTML 5.

                                  – j.c
                                  Nov 25 '16 at 5:03





                                  Good for compatibility, this method works for older browsers too, pre HTML 5.

                                  – j.c
                                  Nov 25 '16 at 5:03











                                  4














                                  check this



                                  <input class="gender" type="radio" name="sex" value="male">Male
                                  <br>
                                  <input class="gender" type="radio" name="sex" value="female">Female


                                  <script type="text/javascript">
                                  $(document).ready(function () {
                                  $(".gender").change(function () {

                                  var val = $('.gender:checked').val();
                                  alert(val);
                                  });
                                  });

                                  </script>


                                  Example






                                  share|improve this answer
























                                  • my requirement is quite different, the alert box will come but at the click on the button

                                    – Akash
                                    Oct 30 '15 at 11:31
















                                  4














                                  check this



                                  <input class="gender" type="radio" name="sex" value="male">Male
                                  <br>
                                  <input class="gender" type="radio" name="sex" value="female">Female


                                  <script type="text/javascript">
                                  $(document).ready(function () {
                                  $(".gender").change(function () {

                                  var val = $('.gender:checked').val();
                                  alert(val);
                                  });
                                  });

                                  </script>


                                  Example






                                  share|improve this answer
























                                  • my requirement is quite different, the alert box will come but at the click on the button

                                    – Akash
                                    Oct 30 '15 at 11:31














                                  4












                                  4








                                  4







                                  check this



                                  <input class="gender" type="radio" name="sex" value="male">Male
                                  <br>
                                  <input class="gender" type="radio" name="sex" value="female">Female


                                  <script type="text/javascript">
                                  $(document).ready(function () {
                                  $(".gender").change(function () {

                                  var val = $('.gender:checked').val();
                                  alert(val);
                                  });
                                  });

                                  </script>


                                  Example






                                  share|improve this answer













                                  check this



                                  <input class="gender" type="radio" name="sex" value="male">Male
                                  <br>
                                  <input class="gender" type="radio" name="sex" value="female">Female


                                  <script type="text/javascript">
                                  $(document).ready(function () {
                                  $(".gender").change(function () {

                                  var val = $('.gender:checked').val();
                                  alert(val);
                                  });
                                  });

                                  </script>


                                  Example







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Feb 25 '13 at 11:35









                                  SanjaySanjay

                                  6321321




                                  6321321













                                  • my requirement is quite different, the alert box will come but at the click on the button

                                    – Akash
                                    Oct 30 '15 at 11:31



















                                  • my requirement is quite different, the alert box will come but at the click on the button

                                    – Akash
                                    Oct 30 '15 at 11:31

















                                  my requirement is quite different, the alert box will come but at the click on the button

                                  – Akash
                                  Oct 30 '15 at 11:31





                                  my requirement is quite different, the alert box will come but at the click on the button

                                  – Akash
                                  Oct 30 '15 at 11:31











                                  3














                                  Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:



                                  var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;


                                  ... which is only valid of course if have provided "value" for your radio input elements.



                                  <input type="radio" name="radio-group-name" value="red" checked>
                                  <input type="radio" name="radio-group-name" value="blue">


                                  The value should be either 'red' or 'blue' in the above example.






                                  share|improve this answer




























                                    3














                                    Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:



                                    var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;


                                    ... which is only valid of course if have provided "value" for your radio input elements.



                                    <input type="radio" name="radio-group-name" value="red" checked>
                                    <input type="radio" name="radio-group-name" value="blue">


                                    The value should be either 'red' or 'blue' in the above example.






                                    share|improve this answer


























                                      3












                                      3








                                      3







                                      Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:



                                      var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;


                                      ... which is only valid of course if have provided "value" for your radio input elements.



                                      <input type="radio" name="radio-group-name" value="red" checked>
                                      <input type="radio" name="radio-group-name" value="blue">


                                      The value should be either 'red' or 'blue' in the above example.






                                      share|improve this answer













                                      Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:



                                      var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;


                                      ... which is only valid of course if have provided "value" for your radio input elements.



                                      <input type="radio" name="radio-group-name" value="red" checked>
                                      <input type="radio" name="radio-group-name" value="blue">


                                      The value should be either 'red' or 'blue' in the above example.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 17 '16 at 11:48









                                      Stan1eyStan1ey

                                      311




                                      311























                                          2














                                          function getCheckedValue(radioObj, name) {

                                          for (j = 0; j < radioObj.rows.length; ++j) {
                                          for (k = 0; k < radioObj.cells.length; ++k) {
                                          var radioChoice = document.getElementById(name + "_" + k);
                                          if (radioChoice.checked) {
                                          return radioChoice.value;
                                          }
                                          }
                                          }
                                          return "";
                                          }





                                          share|improve this answer






























                                            2














                                            function getCheckedValue(radioObj, name) {

                                            for (j = 0; j < radioObj.rows.length; ++j) {
                                            for (k = 0; k < radioObj.cells.length; ++k) {
                                            var radioChoice = document.getElementById(name + "_" + k);
                                            if (radioChoice.checked) {
                                            return radioChoice.value;
                                            }
                                            }
                                            }
                                            return "";
                                            }





                                            share|improve this answer




























                                              2












                                              2








                                              2







                                              function getCheckedValue(radioObj, name) {

                                              for (j = 0; j < radioObj.rows.length; ++j) {
                                              for (k = 0; k < radioObj.cells.length; ++k) {
                                              var radioChoice = document.getElementById(name + "_" + k);
                                              if (radioChoice.checked) {
                                              return radioChoice.value;
                                              }
                                              }
                                              }
                                              return "";
                                              }





                                              share|improve this answer















                                              function getCheckedValue(radioObj, name) {

                                              for (j = 0; j < radioObj.rows.length; ++j) {
                                              for (k = 0; k < radioObj.cells.length; ++k) {
                                              var radioChoice = document.getElementById(name + "_" + k);
                                              if (radioChoice.checked) {
                                              return radioChoice.value;
                                              }
                                              }
                                              }
                                              return "";
                                              }






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Sep 24 '12 at 11:10









                                              Littm

                                              4,71732333




                                              4,71732333










                                              answered Sep 24 '12 at 10:45









                                              Abhaysingh N. RajpurohitAbhaysingh N. Rajpurohit

                                              211




                                              211























                                                  1














                                                  A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
                                                  I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.



                                                  you can also do this with the id, but you usually just want the value.



                                                  <script>
                                                  var gRadioValue = ''; //global declared outside of function
                                                  function myRadioFunc(){
                                                  var radioVal = gRadioValue;
                                                  // or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
                                                  //do somethign with radioVal
                                                  }
                                                  <script>

                                                  <input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
                                                  <input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
                                                  ...
                                                  <input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99





                                                  share|improve this answer






























                                                    1














                                                    A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
                                                    I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.



                                                    you can also do this with the id, but you usually just want the value.



                                                    <script>
                                                    var gRadioValue = ''; //global declared outside of function
                                                    function myRadioFunc(){
                                                    var radioVal = gRadioValue;
                                                    // or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
                                                    //do somethign with radioVal
                                                    }
                                                    <script>

                                                    <input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
                                                    <input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
                                                    ...
                                                    <input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99





                                                    share|improve this answer




























                                                      1












                                                      1








                                                      1







                                                      A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
                                                      I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.



                                                      you can also do this with the id, but you usually just want the value.



                                                      <script>
                                                      var gRadioValue = ''; //global declared outside of function
                                                      function myRadioFunc(){
                                                      var radioVal = gRadioValue;
                                                      // or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
                                                      //do somethign with radioVal
                                                      }
                                                      <script>

                                                      <input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
                                                      <input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
                                                      ...
                                                      <input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99





                                                      share|improve this answer















                                                      A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
                                                      I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.



                                                      you can also do this with the id, but you usually just want the value.



                                                      <script>
                                                      var gRadioValue = ''; //global declared outside of function
                                                      function myRadioFunc(){
                                                      var radioVal = gRadioValue;
                                                      // or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
                                                      //do somethign with radioVal
                                                      }
                                                      <script>

                                                      <input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
                                                      <input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
                                                      ...
                                                      <input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99






                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Aug 30 '13 at 18:14

























                                                      answered Aug 30 '13 at 17:58









                                                      AlanAlan

                                                      24628




                                                      24628























                                                          0














                                                          you can use this



                                                          $('input[name="field_value"]:checked').val(); 


                                                          or, for older version of jquery



                                                          $('input[@name="field_value"]:checked').val();





                                                          share|improve this answer






























                                                            0














                                                            you can use this



                                                            $('input[name="field_value"]:checked').val(); 


                                                            or, for older version of jquery



                                                            $('input[@name="field_value"]:checked').val();





                                                            share|improve this answer




























                                                              0












                                                              0








                                                              0







                                                              you can use this



                                                              $('input[name="field_value"]:checked').val(); 


                                                              or, for older version of jquery



                                                              $('input[@name="field_value"]:checked').val();





                                                              share|improve this answer















                                                              you can use this



                                                              $('input[name="field_value"]:checked').val(); 


                                                              or, for older version of jquery



                                                              $('input[@name="field_value"]:checked').val();






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Jun 20 '13 at 6:24









                                                              Andrea

                                                              7,661144652




                                                              7,661144652










                                                              answered Jun 20 '13 at 6:07









                                                              Mughal SahabMughal Sahab

                                                              312




                                                              312























                                                                  0














                                                                  Since you want to get it using plain javascript, you can use the following code



                                                                  var val = '';
                                                                  if(document.getElementById('radio1').checked) {
                                                                  val = document.getElementById('radio1').value
                                                                  }else if(document.getElementById('radio2').checked) {
                                                                  val = document.getElementById('radio2').value
                                                                  }





                                                                  share|improve this answer




























                                                                    0














                                                                    Since you want to get it using plain javascript, you can use the following code



                                                                    var val = '';
                                                                    if(document.getElementById('radio1').checked) {
                                                                    val = document.getElementById('radio1').value
                                                                    }else if(document.getElementById('radio2').checked) {
                                                                    val = document.getElementById('radio2').value
                                                                    }





                                                                    share|improve this answer


























                                                                      0












                                                                      0








                                                                      0







                                                                      Since you want to get it using plain javascript, you can use the following code



                                                                      var val = '';
                                                                      if(document.getElementById('radio1').checked) {
                                                                      val = document.getElementById('radio1').value
                                                                      }else if(document.getElementById('radio2').checked) {
                                                                      val = document.getElementById('radio2').value
                                                                      }





                                                                      share|improve this answer













                                                                      Since you want to get it using plain javascript, you can use the following code



                                                                      var val = '';
                                                                      if(document.getElementById('radio1').checked) {
                                                                      val = document.getElementById('radio1').value
                                                                      }else if(document.getElementById('radio2').checked) {
                                                                      val = document.getElementById('radio2').value
                                                                      }






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Jan 21 '14 at 9:59









                                                                      NauphalNauphal

                                                                      5,53732143




                                                                      5,53732143























                                                                          0














                                                                          Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).



                                                                          I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked function. If this is set to true I can change the value of another variable.






                                                                          function createOutput() {
                                                                          var order = "in";
                                                                          if (document.getElementById('radio1').checked == true) {
                                                                          order = "pre";
                                                                          } else if (document.getElementById('radio2').checked == true) {
                                                                          order = "in";
                                                                          } else if (document.getElementById('radio3').checked == true) {
                                                                          order = "post";
                                                                          }
                                                                          document.getElementById('outputBox').innerHTML = order;
                                                                          }

                                                                          <input id="radio1" type="radio" name="order" value="preorder" />Preorder
                                                                          <input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
                                                                          <input id="radio3" type="radio" name="order" value="postorder" />Postorder
                                                                          <button onclick="createOutput();">Generate Output</button>
                                                                          <textarea id="outputBox" rows="10" cols="50"></textarea>





                                                                          Hope this is useful, in someway,



                                                                          Beanz






                                                                          share|improve this answer




























                                                                            0














                                                                            Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).



                                                                            I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked function. If this is set to true I can change the value of another variable.






                                                                            function createOutput() {
                                                                            var order = "in";
                                                                            if (document.getElementById('radio1').checked == true) {
                                                                            order = "pre";
                                                                            } else if (document.getElementById('radio2').checked == true) {
                                                                            order = "in";
                                                                            } else if (document.getElementById('radio3').checked == true) {
                                                                            order = "post";
                                                                            }
                                                                            document.getElementById('outputBox').innerHTML = order;
                                                                            }

                                                                            <input id="radio1" type="radio" name="order" value="preorder" />Preorder
                                                                            <input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
                                                                            <input id="radio3" type="radio" name="order" value="postorder" />Postorder
                                                                            <button onclick="createOutput();">Generate Output</button>
                                                                            <textarea id="outputBox" rows="10" cols="50"></textarea>





                                                                            Hope this is useful, in someway,



                                                                            Beanz






                                                                            share|improve this answer


























                                                                              0












                                                                              0








                                                                              0







                                                                              Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).



                                                                              I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked function. If this is set to true I can change the value of another variable.






                                                                              function createOutput() {
                                                                              var order = "in";
                                                                              if (document.getElementById('radio1').checked == true) {
                                                                              order = "pre";
                                                                              } else if (document.getElementById('radio2').checked == true) {
                                                                              order = "in";
                                                                              } else if (document.getElementById('radio3').checked == true) {
                                                                              order = "post";
                                                                              }
                                                                              document.getElementById('outputBox').innerHTML = order;
                                                                              }

                                                                              <input id="radio1" type="radio" name="order" value="preorder" />Preorder
                                                                              <input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
                                                                              <input id="radio3" type="radio" name="order" value="postorder" />Postorder
                                                                              <button onclick="createOutput();">Generate Output</button>
                                                                              <textarea id="outputBox" rows="10" cols="50"></textarea>





                                                                              Hope this is useful, in someway,



                                                                              Beanz






                                                                              share|improve this answer













                                                                              Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).



                                                                              I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked function. If this is set to true I can change the value of another variable.






                                                                              function createOutput() {
                                                                              var order = "in";
                                                                              if (document.getElementById('radio1').checked == true) {
                                                                              order = "pre";
                                                                              } else if (document.getElementById('radio2').checked == true) {
                                                                              order = "in";
                                                                              } else if (document.getElementById('radio3').checked == true) {
                                                                              order = "post";
                                                                              }
                                                                              document.getElementById('outputBox').innerHTML = order;
                                                                              }

                                                                              <input id="radio1" type="radio" name="order" value="preorder" />Preorder
                                                                              <input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
                                                                              <input id="radio3" type="radio" name="order" value="postorder" />Postorder
                                                                              <button onclick="createOutput();">Generate Output</button>
                                                                              <textarea id="outputBox" rows="10" cols="50"></textarea>





                                                                              Hope this is useful, in someway,



                                                                              Beanz






                                                                              function createOutput() {
                                                                              var order = "in";
                                                                              if (document.getElementById('radio1').checked == true) {
                                                                              order = "pre";
                                                                              } else if (document.getElementById('radio2').checked == true) {
                                                                              order = "in";
                                                                              } else if (document.getElementById('radio3').checked == true) {
                                                                              order = "post";
                                                                              }
                                                                              document.getElementById('outputBox').innerHTML = order;
                                                                              }

                                                                              <input id="radio1" type="radio" name="order" value="preorder" />Preorder
                                                                              <input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
                                                                              <input id="radio3" type="radio" name="order" value="postorder" />Postorder
                                                                              <button onclick="createOutput();">Generate Output</button>
                                                                              <textarea id="outputBox" rows="10" cols="50"></textarea>





                                                                              function createOutput() {
                                                                              var order = "in";
                                                                              if (document.getElementById('radio1').checked == true) {
                                                                              order = "pre";
                                                                              } else if (document.getElementById('radio2').checked == true) {
                                                                              order = "in";
                                                                              } else if (document.getElementById('radio3').checked == true) {
                                                                              order = "post";
                                                                              }
                                                                              document.getElementById('outputBox').innerHTML = order;
                                                                              }

                                                                              <input id="radio1" type="radio" name="order" value="preorder" />Preorder
                                                                              <input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
                                                                              <input id="radio3" type="radio" name="order" value="postorder" />Postorder
                                                                              <button onclick="createOutput();">Generate Output</button>
                                                                              <textarea id="outputBox" rows="10" cols="50"></textarea>






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Mar 4 '16 at 22:49









                                                                              Breandán FawcettBreandán Fawcett

                                                                              64




                                                                              64























                                                                                  0














                                                                                  You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.






                                                                                  function getSelectedValue() {
                                                                                  var radioBtns = document.getElementsByClassName("radioBtn");
                                                                                  for(var i = 0; i < radioBtns.length; i++){
                                                                                  if(radioBtns[i].checked){
                                                                                  document.getElementById("output").textContent = radioBtns[i].value;
                                                                                  }
                                                                                  }
                                                                                  }

                                                                                  <input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
                                                                                  <input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
                                                                                  <input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
                                                                                  <button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
                                                                                  <textarea id="output"></textarea>








                                                                                  share|improve this answer




























                                                                                    0














                                                                                    You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.






                                                                                    function getSelectedValue() {
                                                                                    var radioBtns = document.getElementsByClassName("radioBtn");
                                                                                    for(var i = 0; i < radioBtns.length; i++){
                                                                                    if(radioBtns[i].checked){
                                                                                    document.getElementById("output").textContent = radioBtns[i].value;
                                                                                    }
                                                                                    }
                                                                                    }

                                                                                    <input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
                                                                                    <input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
                                                                                    <input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
                                                                                    <button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
                                                                                    <textarea id="output"></textarea>








                                                                                    share|improve this answer


























                                                                                      0












                                                                                      0








                                                                                      0







                                                                                      You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.






                                                                                      function getSelectedValue() {
                                                                                      var radioBtns = document.getElementsByClassName("radioBtn");
                                                                                      for(var i = 0; i < radioBtns.length; i++){
                                                                                      if(radioBtns[i].checked){
                                                                                      document.getElementById("output").textContent = radioBtns[i].value;
                                                                                      }
                                                                                      }
                                                                                      }

                                                                                      <input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
                                                                                      <button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
                                                                                      <textarea id="output"></textarea>








                                                                                      share|improve this answer













                                                                                      You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.






                                                                                      function getSelectedValue() {
                                                                                      var radioBtns = document.getElementsByClassName("radioBtn");
                                                                                      for(var i = 0; i < radioBtns.length; i++){
                                                                                      if(radioBtns[i].checked){
                                                                                      document.getElementById("output").textContent = radioBtns[i].value;
                                                                                      }
                                                                                      }
                                                                                      }

                                                                                      <input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
                                                                                      <button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
                                                                                      <textarea id="output"></textarea>








                                                                                      function getSelectedValue() {
                                                                                      var radioBtns = document.getElementsByClassName("radioBtn");
                                                                                      for(var i = 0; i < radioBtns.length; i++){
                                                                                      if(radioBtns[i].checked){
                                                                                      document.getElementById("output").textContent = radioBtns[i].value;
                                                                                      }
                                                                                      }
                                                                                      }

                                                                                      <input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
                                                                                      <button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
                                                                                      <textarea id="output"></textarea>





                                                                                      function getSelectedValue() {
                                                                                      var radioBtns = document.getElementsByClassName("radioBtn");
                                                                                      for(var i = 0; i < radioBtns.length; i++){
                                                                                      if(radioBtns[i].checked){
                                                                                      document.getElementById("output").textContent = radioBtns[i].value;
                                                                                      }
                                                                                      }
                                                                                      }

                                                                                      <input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
                                                                                      <input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
                                                                                      <button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
                                                                                      <textarea id="output"></textarea>






                                                                                      share|improve this answer












                                                                                      share|improve this answer



                                                                                      share|improve this answer










                                                                                      answered Nov 24 '16 at 23:07









                                                                                      Emily ZhaiEmily Zhai

                                                                                      577




                                                                                      577























                                                                                          0














                                                                                          all of you can test this example and easy to understand.



                                                                                                  Name:   <input type="text" id="text" class ="input">
                                                                                          <input type="text" id="text1" class ="input">
                                                                                          Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
                                                                                          <input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
                                                                                          Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
                                                                                          <input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
                                                                                          <input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
                                                                                          <button type="button" id="b1">show</button>


                                                                                          // javascript



                                                                                              <script>
                                                                                          function getData(input){
                                                                                          return document.getElementById(input).value;
                                                                                          }
                                                                                          function dataByClassName(st){
                                                                                          var value=document.getElementsByClassName(st)
                                                                                          for(var i=0;i < value.length;i++){
                                                                                          if(value[i].checked){
                                                                                          return value[i].value;
                                                                                          }
                                                                                          }
                                                                                          }
                                                                                          document.getElementById("b1").onclick = function ()
                                                                                          {
                                                                                          var st={
                                                                                          name : getData("text")+getData("text1"),
                                                                                          gender : dataByClassName("Rm"),
                                                                                          course : dataByClassName("cm")
                                                                                          };
                                                                                          alert(st.name+" "+st.gender+" "+st.course);
                                                                                          };

                                                                                          </script>





                                                                                          share|improve this answer




























                                                                                            0














                                                                                            all of you can test this example and easy to understand.



                                                                                                    Name:   <input type="text" id="text" class ="input">
                                                                                            <input type="text" id="text1" class ="input">
                                                                                            Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
                                                                                            <input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
                                                                                            Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
                                                                                            <input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
                                                                                            <input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
                                                                                            <button type="button" id="b1">show</button>


                                                                                            // javascript



                                                                                                <script>
                                                                                            function getData(input){
                                                                                            return document.getElementById(input).value;
                                                                                            }
                                                                                            function dataByClassName(st){
                                                                                            var value=document.getElementsByClassName(st)
                                                                                            for(var i=0;i < value.length;i++){
                                                                                            if(value[i].checked){
                                                                                            return value[i].value;
                                                                                            }
                                                                                            }
                                                                                            }
                                                                                            document.getElementById("b1").onclick = function ()
                                                                                            {
                                                                                            var st={
                                                                                            name : getData("text")+getData("text1"),
                                                                                            gender : dataByClassName("Rm"),
                                                                                            course : dataByClassName("cm")
                                                                                            };
                                                                                            alert(st.name+" "+st.gender+" "+st.course);
                                                                                            };

                                                                                            </script>





                                                                                            share|improve this answer


























                                                                                              0












                                                                                              0








                                                                                              0







                                                                                              all of you can test this example and easy to understand.



                                                                                                      Name:   <input type="text" id="text" class ="input">
                                                                                              <input type="text" id="text1" class ="input">
                                                                                              Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
                                                                                              <input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
                                                                                              Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
                                                                                              <input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
                                                                                              <input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
                                                                                              <button type="button" id="b1">show</button>


                                                                                              // javascript



                                                                                                  <script>
                                                                                              function getData(input){
                                                                                              return document.getElementById(input).value;
                                                                                              }
                                                                                              function dataByClassName(st){
                                                                                              var value=document.getElementsByClassName(st)
                                                                                              for(var i=0;i < value.length;i++){
                                                                                              if(value[i].checked){
                                                                                              return value[i].value;
                                                                                              }
                                                                                              }
                                                                                              }
                                                                                              document.getElementById("b1").onclick = function ()
                                                                                              {
                                                                                              var st={
                                                                                              name : getData("text")+getData("text1"),
                                                                                              gender : dataByClassName("Rm"),
                                                                                              course : dataByClassName("cm")
                                                                                              };
                                                                                              alert(st.name+" "+st.gender+" "+st.course);
                                                                                              };

                                                                                              </script>





                                                                                              share|improve this answer













                                                                                              all of you can test this example and easy to understand.



                                                                                                      Name:   <input type="text" id="text" class ="input">
                                                                                              <input type="text" id="text1" class ="input">
                                                                                              Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
                                                                                              <input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
                                                                                              Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
                                                                                              <input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
                                                                                              <input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
                                                                                              <button type="button" id="b1">show</button>


                                                                                              // javascript



                                                                                                  <script>
                                                                                              function getData(input){
                                                                                              return document.getElementById(input).value;
                                                                                              }
                                                                                              function dataByClassName(st){
                                                                                              var value=document.getElementsByClassName(st)
                                                                                              for(var i=0;i < value.length;i++){
                                                                                              if(value[i].checked){
                                                                                              return value[i].value;
                                                                                              }
                                                                                              }
                                                                                              }
                                                                                              document.getElementById("b1").onclick = function ()
                                                                                              {
                                                                                              var st={
                                                                                              name : getData("text")+getData("text1"),
                                                                                              gender : dataByClassName("Rm"),
                                                                                              course : dataByClassName("cm")
                                                                                              };
                                                                                              alert(st.name+" "+st.gender+" "+st.course);
                                                                                              };

                                                                                              </script>






                                                                                              share|improve this answer












                                                                                              share|improve this answer



                                                                                              share|improve this answer










                                                                                              answered Oct 19 '17 at 16:02









                                                                                              Sophy HengSophy Heng

                                                                                              11




                                                                                              11























                                                                                                  0














                                                                                                  Try this, I hope this one will work



                                                                                                  function loadRadioButton(objectName, selectedValue) {

                                                                                                  var radioButtons = document.getElementsByName(objectName);

                                                                                                  if (radioButtons != null) {
                                                                                                  for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
                                                                                                  if (radioButtons[radioCount].value == selectedValue) {
                                                                                                  radioButtons[radioCount].checked = true;
                                                                                                  }
                                                                                                  }
                                                                                                  }
                                                                                                  }





                                                                                                  share|improve this answer






























                                                                                                    0














                                                                                                    Try this, I hope this one will work



                                                                                                    function loadRadioButton(objectName, selectedValue) {

                                                                                                    var radioButtons = document.getElementsByName(objectName);

                                                                                                    if (radioButtons != null) {
                                                                                                    for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
                                                                                                    if (radioButtons[radioCount].value == selectedValue) {
                                                                                                    radioButtons[radioCount].checked = true;
                                                                                                    }
                                                                                                    }
                                                                                                    }
                                                                                                    }





                                                                                                    share|improve this answer




























                                                                                                      0












                                                                                                      0








                                                                                                      0







                                                                                                      Try this, I hope this one will work



                                                                                                      function loadRadioButton(objectName, selectedValue) {

                                                                                                      var radioButtons = document.getElementsByName(objectName);

                                                                                                      if (radioButtons != null) {
                                                                                                      for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
                                                                                                      if (radioButtons[radioCount].value == selectedValue) {
                                                                                                      radioButtons[radioCount].checked = true;
                                                                                                      }
                                                                                                      }
                                                                                                      }
                                                                                                      }





                                                                                                      share|improve this answer















                                                                                                      Try this, I hope this one will work



                                                                                                      function loadRadioButton(objectName, selectedValue) {

                                                                                                      var radioButtons = document.getElementsByName(objectName);

                                                                                                      if (radioButtons != null) {
                                                                                                      for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
                                                                                                      if (radioButtons[radioCount].value == selectedValue) {
                                                                                                      radioButtons[radioCount].checked = true;
                                                                                                      }
                                                                                                      }
                                                                                                      }
                                                                                                      }






                                                                                                      share|improve this answer














                                                                                                      share|improve this answer



                                                                                                      share|improve this answer








                                                                                                      edited Nov 23 '18 at 9:42









                                                                                                      Suraj Rao

                                                                                                      23.2k85770




                                                                                                      23.2k85770










                                                                                                      answered Nov 23 '18 at 9:25









                                                                                                      user3585199user3585199

                                                                                                      32




                                                                                                      32























                                                                                                          -1














                                                                                                          If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:



                                                                                                          function selectedRadio() {
                                                                                                          var radio = document.getElementsByName('mailCopy');
                                                                                                          alert(radio[0].value);
                                                                                                          }


                                                                                                          Notes:




                                                                                                          • In general for the inputs you want to have unique IDs (not a requirement but a good practice)

                                                                                                          • All the radio inputs that are from the same group MUST have the same name attribute, for example

                                                                                                          • You have to set the value attribute for each input


                                                                                                          Here is an example of input radios:



                                                                                                          <input type="radio" name="mailCopy" value="1" />1<br />
                                                                                                          <input type="radio" name="mailCopy" value="2" />2<br />





                                                                                                          share|improve this answer




























                                                                                                            -1














                                                                                                            If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:



                                                                                                            function selectedRadio() {
                                                                                                            var radio = document.getElementsByName('mailCopy');
                                                                                                            alert(radio[0].value);
                                                                                                            }


                                                                                                            Notes:




                                                                                                            • In general for the inputs you want to have unique IDs (not a requirement but a good practice)

                                                                                                            • All the radio inputs that are from the same group MUST have the same name attribute, for example

                                                                                                            • You have to set the value attribute for each input


                                                                                                            Here is an example of input radios:



                                                                                                            <input type="radio" name="mailCopy" value="1" />1<br />
                                                                                                            <input type="radio" name="mailCopy" value="2" />2<br />





                                                                                                            share|improve this answer


























                                                                                                              -1












                                                                                                              -1








                                                                                                              -1







                                                                                                              If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:



                                                                                                              function selectedRadio() {
                                                                                                              var radio = document.getElementsByName('mailCopy');
                                                                                                              alert(radio[0].value);
                                                                                                              }


                                                                                                              Notes:




                                                                                                              • In general for the inputs you want to have unique IDs (not a requirement but a good practice)

                                                                                                              • All the radio inputs that are from the same group MUST have the same name attribute, for example

                                                                                                              • You have to set the value attribute for each input


                                                                                                              Here is an example of input radios:



                                                                                                              <input type="radio" name="mailCopy" value="1" />1<br />
                                                                                                              <input type="radio" name="mailCopy" value="2" />2<br />





                                                                                                              share|improve this answer













                                                                                                              If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:



                                                                                                              function selectedRadio() {
                                                                                                              var radio = document.getElementsByName('mailCopy');
                                                                                                              alert(radio[0].value);
                                                                                                              }


                                                                                                              Notes:




                                                                                                              • In general for the inputs you want to have unique IDs (not a requirement but a good practice)

                                                                                                              • All the radio inputs that are from the same group MUST have the same name attribute, for example

                                                                                                              • You have to set the value attribute for each input


                                                                                                              Here is an example of input radios:



                                                                                                              <input type="radio" name="mailCopy" value="1" />1<br />
                                                                                                              <input type="radio" name="mailCopy" value="2" />2<br />






                                                                                                              share|improve this answer












                                                                                                              share|improve this answer



                                                                                                              share|improve this answer










                                                                                                              answered Jun 20 '13 at 12:51









                                                                                                              Alejandro NarancioAlejandro Narancio

                                                                                                              7713




                                                                                                              7713























                                                                                                                  -2














                                                                                                                  var mailcopy = document.getElementById('mailCopy').checked; 

                                                                                                                  if(mailcopy==true)
                                                                                                                  {
                                                                                                                  alert("Radio Button Checked");
                                                                                                                  }
                                                                                                                  else
                                                                                                                  {
                                                                                                                  alert("Radio Button un-Checked");
                                                                                                                  }





                                                                                                                  share|improve this answer



















                                                                                                                  • 3





                                                                                                                    The question is looking to get the currently selected button, not find out if a specific button is checked.

                                                                                                                    – Quentin
                                                                                                                    Oct 6 '10 at 6:23
















                                                                                                                  -2














                                                                                                                  var mailcopy = document.getElementById('mailCopy').checked; 

                                                                                                                  if(mailcopy==true)
                                                                                                                  {
                                                                                                                  alert("Radio Button Checked");
                                                                                                                  }
                                                                                                                  else
                                                                                                                  {
                                                                                                                  alert("Radio Button un-Checked");
                                                                                                                  }





                                                                                                                  share|improve this answer



















                                                                                                                  • 3





                                                                                                                    The question is looking to get the currently selected button, not find out if a specific button is checked.

                                                                                                                    – Quentin
                                                                                                                    Oct 6 '10 at 6:23














                                                                                                                  -2












                                                                                                                  -2








                                                                                                                  -2







                                                                                                                  var mailcopy = document.getElementById('mailCopy').checked; 

                                                                                                                  if(mailcopy==true)
                                                                                                                  {
                                                                                                                  alert("Radio Button Checked");
                                                                                                                  }
                                                                                                                  else
                                                                                                                  {
                                                                                                                  alert("Radio Button un-Checked");
                                                                                                                  }





                                                                                                                  share|improve this answer













                                                                                                                  var mailcopy = document.getElementById('mailCopy').checked; 

                                                                                                                  if(mailcopy==true)
                                                                                                                  {
                                                                                                                  alert("Radio Button Checked");
                                                                                                                  }
                                                                                                                  else
                                                                                                                  {
                                                                                                                  alert("Radio Button un-Checked");
                                                                                                                  }






                                                                                                                  share|improve this answer












                                                                                                                  share|improve this answer



                                                                                                                  share|improve this answer










                                                                                                                  answered Oct 6 '10 at 5:07









                                                                                                                  KhanZeeshanKhanZeeshan

                                                                                                                  94241833




                                                                                                                  94241833








                                                                                                                  • 3





                                                                                                                    The question is looking to get the currently selected button, not find out if a specific button is checked.

                                                                                                                    – Quentin
                                                                                                                    Oct 6 '10 at 6:23














                                                                                                                  • 3





                                                                                                                    The question is looking to get the currently selected button, not find out if a specific button is checked.

                                                                                                                    – Quentin
                                                                                                                    Oct 6 '10 at 6:23








                                                                                                                  3




                                                                                                                  3





                                                                                                                  The question is looking to get the currently selected button, not find out if a specific button is checked.

                                                                                                                  – Quentin
                                                                                                                  Oct 6 '10 at 6:23





                                                                                                                  The question is looking to get the currently selected button, not find out if a specific button is checked.

                                                                                                                  – Quentin
                                                                                                                  Oct 6 '10 at 6:23











                                                                                                                  -2














                                                                                                                  Hy, you have to do it this way.



                                                                                                                  function checkRadio () {
                                                                                                                  if(document.getElementById('user1').checked) {
                                                                                                                  return $('#user1').val();
                                                                                                                  }else if(document.getElementById('user2').checked) {
                                                                                                                  return $('#user2').val();
                                                                                                                  }
                                                                                                                  }





                                                                                                                  share|improve this answer




























                                                                                                                    -2














                                                                                                                    Hy, you have to do it this way.



                                                                                                                    function checkRadio () {
                                                                                                                    if(document.getElementById('user1').checked) {
                                                                                                                    return $('#user1').val();
                                                                                                                    }else if(document.getElementById('user2').checked) {
                                                                                                                    return $('#user2').val();
                                                                                                                    }
                                                                                                                    }





                                                                                                                    share|improve this answer


























                                                                                                                      -2












                                                                                                                      -2








                                                                                                                      -2







                                                                                                                      Hy, you have to do it this way.



                                                                                                                      function checkRadio () {
                                                                                                                      if(document.getElementById('user1').checked) {
                                                                                                                      return $('#user1').val();
                                                                                                                      }else if(document.getElementById('user2').checked) {
                                                                                                                      return $('#user2').val();
                                                                                                                      }
                                                                                                                      }





                                                                                                                      share|improve this answer













                                                                                                                      Hy, you have to do it this way.



                                                                                                                      function checkRadio () {
                                                                                                                      if(document.getElementById('user1').checked) {
                                                                                                                      return $('#user1').val();
                                                                                                                      }else if(document.getElementById('user2').checked) {
                                                                                                                      return $('#user2').val();
                                                                                                                      }
                                                                                                                      }






                                                                                                                      share|improve this answer












                                                                                                                      share|improve this answer



                                                                                                                      share|improve this answer










                                                                                                                      answered Jul 23 '14 at 8:57









                                                                                                                      SoftwarerHelpSoftwarerHelp

                                                                                                                      455




                                                                                                                      455























                                                                                                                          -3














                                                                                                                          Use the element.checked property.






                                                                                                                          share|improve this answer


























                                                                                                                          • thank u for your reply . can u explain in clear

                                                                                                                            – Meena
                                                                                                                            Oct 6 '10 at 5:01
















                                                                                                                          -3














                                                                                                                          Use the element.checked property.






                                                                                                                          share|improve this answer


























                                                                                                                          • thank u for your reply . can u explain in clear

                                                                                                                            – Meena
                                                                                                                            Oct 6 '10 at 5:01














                                                                                                                          -3












                                                                                                                          -3








                                                                                                                          -3







                                                                                                                          Use the element.checked property.






                                                                                                                          share|improve this answer















                                                                                                                          Use the element.checked property.







                                                                                                                          share|improve this answer














                                                                                                                          share|improve this answer



                                                                                                                          share|improve this answer








                                                                                                                          edited Aug 21 '12 at 9:47









                                                                                                                          ThePower

                                                                                                                          11.1k1071116




                                                                                                                          11.1k1071116










                                                                                                                          answered Oct 6 '10 at 4:47









                                                                                                                          Francisco SotoFrancisco Soto

                                                                                                                          8,61323242




                                                                                                                          8,61323242













                                                                                                                          • thank u for your reply . can u explain in clear

                                                                                                                            – Meena
                                                                                                                            Oct 6 '10 at 5:01



















                                                                                                                          • thank u for your reply . can u explain in clear

                                                                                                                            – Meena
                                                                                                                            Oct 6 '10 at 5:01

















                                                                                                                          thank u for your reply . can u explain in clear

                                                                                                                          – Meena
                                                                                                                          Oct 6 '10 at 5:01





                                                                                                                          thank u for your reply . can u explain in clear

                                                                                                                          – Meena
                                                                                                                          Oct 6 '10 at 5:01


















                                                                                                                          draft saved

                                                                                                                          draft discarded




















































                                                                                                                          Thanks for contributing an answer to Stack Overflow!


                                                                                                                          • Please be sure to answer the question. Provide details and share your research!

                                                                                                                          But avoid



                                                                                                                          • Asking for help, clarification, or responding to other answers.

                                                                                                                          • Making statements based on opinion; back them up with references or personal experience.


                                                                                                                          To learn more, see our tips on writing great answers.




                                                                                                                          draft saved


                                                                                                                          draft discarded














                                                                                                                          StackExchange.ready(
                                                                                                                          function () {
                                                                                                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3869535%2fhow-to-get-the-selected-radio-button-value-using-js%23new-answer', 'question_page');
                                                                                                                          }
                                                                                                                          );

                                                                                                                          Post as a guest















                                                                                                                          Required, but never shown





















































                                                                                                                          Required, but never shown














                                                                                                                          Required, but never shown












                                                                                                                          Required, but never shown







                                                                                                                          Required, but never shown

































                                                                                                                          Required, but never shown














                                                                                                                          Required, but never shown












                                                                                                                          Required, but never shown







                                                                                                                          Required, but never shown







                                                                                                                          Popular posts from this blog

                                                                                                                          Wiesbaden

                                                                                                                          Marschland

                                                                                                                          Dieringhausen