0

I guess the problem is pretty simple but I couldn't find a solution anywhere. I want to check whether a radio button is checked or not. From here a possible solution:

For these inputs

<input type="radio" name="gender" id="gender_Male" value="Male" />
<input type="radio" name="gender" id="gender_Female" value="Female" />

you can simply

if(document.getElementById('gender_Male').checked) {
  //Male radio button is checked
}else if(document.getElementById('gender_Female').checked) {
  //Female radio button is checked
}

Yet my inputs are like this:

<label><input id="16" type="radio" value="14" name="Q3"></input>Yes</label>
<label><input id="16" type="radio" value="15" name="Q3"></input>No</label>

id is not unique by itself but only in combination with value... How do I check whether Yes or No are selected?

Community
  • 1
  • 1
CptNemo
  • 6,455
  • 16
  • 58
  • 107

6 Answers6

0

As @Markasoftware said, id should always be unique. But browser still parse your html with the same ids.

So key point is that you can select an element without id.

Try document.querySelectorAll or document.querySelector. Here is code:

document.querySelectorAll('input[value="14"]') // you can select all the `value='14'` inputs
document.querySelectorAll('input[name="Q3"]') // you can select all the `value='Q3'` inputs

You can compose these css3 selectors and reach your goal.

creeper
  • 499
  • 2
  • 17
0

You can try the next selector:

$('input#16.14').is(':checked'){ 
    //YES is checked
}
$('input#16.15').is(':checked'){ 
    //NO is checked
}

But id should be unique.

Another way to check which one is selected it's using the input name:

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

This way you will get 14 if YES is checked or 15 if No is checked

rogelio
  • 1,541
  • 15
  • 19
0

Assuming the yes/no element is next element of your gender radio, you can try something like:

$('[name="gender"]').change(function(){

  if($(this).val() == 'Male'){

    $(this).next('input[type="radio"][value="14"]').prop('checked',true);

  }else{
   //Female

  }

});
Drixson Oseña
  • 3,631
  • 3
  • 23
  • 36
0

Add a class to your radio buttons.

$('.radioGroup').change(function(){
  if($(this).val() == 'Male'){
      ///
  } else {
      //female
  }
});
Fergoso
  • 1,584
  • 3
  • 21
  • 44
0
    <html>
        <head>
            <meta charset="utf-8">

            <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
            <script src="//code.jquery.com/jquery-1.9.1.js"></script>
            <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

        </head>

        <body>
   <label><input id="16" type="radio" value="14" name="Q3"/>Yes</label>
   <label><input id="16" type="radio" value="15" name="Q3"/>No</label>

         <script type="text/javascript">
            $('input[name=Q3]').change(function () {
                if ($('input[name=Q3]:checked').length > 0) {
                    var k = $('input[name=Q3]:checked').val();
                    alert(k);
                }
            });
        </script>

        </body>
    </html>
renjith
  • 423
  • 5
  • 12
0

$(':radio').on('change',function() {
  if( this.checked && this.value === "14" ) {
    alert( "'Yes' was selected!" );
  } else if( this.checked && this.value === "15" ) {
    alert( "'No' was selected!" );
  }
});
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><input id="Q3_1" type="radio" value="14" name="Q3">Yes</label>
<label><input id="Q3_2" type="radio" value="15" name="Q3">No</label>
PeterKA
  • 24,158
  • 5
  • 26
  • 48