LP13
LP13

Reputation: 34159

How to find individual radio from radio group and set check property?

i have radio group

      <input class="form-check-input" id="hasHeader0" name="hasHeader" type="radio" value="Yes">
      <input class="form-check-input" id="hasHeader1" name="hasHeader" type="radio" value="No">

then i have a jquery instance of radio group

         var radioGroup =   $("input:radio[name ='hasHeader']")

how do i find a individual radio based on value and then set its Check property to true.

i tried

   radioGroup.find("[value='Yes']").prop('checked', true);

and also tried

   radioGroup.find("input[value='Yes']").prop('checked', true);

Note that i already have the instance of radio group. So i am not looking for answer

   $("input[name='hasHeader'][value='Yes']").prop('checked', 'checked');

Upvotes: 1

Views: 37

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115272

The find method will search for child elements but in your case use filter method to filter from a collection.

radioGroup.filter("[value='Yes']").prop('checked', true);  

var radioGroup =   $("input:radio[name ='hasHeader']")
radioGroup.filter("[value='Yes']").prop('checked', true);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
 Yes<input class="form-check-input" id="hasHeader0" name="hasHeader" type="radio" value="Yes">
 No<input class="form-check-input" id="hasHeader1" name="hasHeader" type="radio" value="No">

Upvotes: 1

Related Questions