2

I have several groups of radio buttons with the same name.

I want to get the id of the clicked radio button in the change event. There are several answers related to this question but they all return the id of the checked one but what i want is clicked radio id.

Updated Here is my code:

$('input[type=radio][name=SltOptionPane]').change(function () {
  debugger;
  //here i want to get the clicked id of the radio
  var selectedValue = $("input[name=SltOptionPane]");
  alert(selectedValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="26Panel">
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="27" id="27" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;طبيعي</text>
    </label>
  </div>
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="29" id="29" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;ضعيف</text>
    </label>
  </div>
</div>

<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="27Panel">
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="30" id="30" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;طبيعي</text>
    </label>
  </div>
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="31" id="31" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;ضعيف</text>
    </label>
  </div>
</div>

Please help ,Thanks in advance

Marty
  • 146
  • 1
  • 13
Shyamal
  • 161
  • 1
  • 2
  • 17

4 Answers4

2

You should use this to refer the current element.

Try $(this).attr('id');

$('input[type=radio][name=SltOptionPane]').change(function () {
  debugger;
  //here i want to get the clicked id of the radio
  var selectedValue = $(this).attr('id');
  alert(selectedValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="26Panel">
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="27" id="27" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;طبيعي</text>
    </label>
  </div>
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="29" id="29" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;ضعيف</text>
    </label>
  </div>
</div>

<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="27Panel">
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="30" id="30" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;طبيعي</text>
    </label>
  </div>
  <div class="col-md-6">
    <label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
    <span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="31" id="31" autocomplete="off"><text>&nbsp;&nbsp;&nbsp;ضعيف</text>
    </label>
  </div>
</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

You can do it with jquery on click input (on input you can also put class):

$(document).on('click', 'input', function(){
    alert(this.id);
});

Here is working fiddle: https://jsfiddle.net/m7r5z9g1/

Ingus
  • 1,026
  • 12
  • 34
0

At first give all radio buttons same class like "MyRadio" then create the following event.

$('.MyRadio').off('click').on('click', function(){
  alert(this.attr('id'));
});
-1

You can also use the prop method in order to retrieve the id of the selected item.

Here below is snippet of the Onchange function :

$('input[type=radio][name=SltOptionPane]').change(function () {
  debugger;
  var currentValue = $(this).prop("id");
  alert(currentValue);
});

Here is the link for the demonstration:

More Insights :

The .prop() method gets the property value for only the first element in the matched set. It returns undefined for the value of a property that has not been set, or if the matched set has no elements. To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method.

Reference :

redhatvicky
  • 1,912
  • 9
  • 8