3

I am trying to set Radio Button Checked based on its text value

This is my HTML Code

<section class="trainers_listWrap">
   <li><label class="mt-radio mt-radio-outline traine">
      <input type="radio" data-value="One" class="trainerradio" value="62" name="trainernames">One
      <span></span>
      </label>
   </li>
   <li><label class="mt-radio mt-radio-outline traine">
      <input type="radio" data-value="Two" class="trainerradio" value="9" name="trainernames">Two
      <span></span>
      </label>
   </li>
   <li><label class="mt-radio mt-radio-outline traine">
      <input type="radio" data-value="THree" class="trainerradio" value="17" name="trainernames">THree
      <span></span>
      </label>
   </li>

</section>

I have tried as (Trying to check Two Radio Button in the above HTML )

 $('input:radio[name="trainernames"][value="Two"]').prop('checked', true);

This is my fiddle

http://jsfiddle.net/cod7ceho/376/

Could you please let me know how to set Radio button based on its text value

Satpal
  • 132,252
  • 13
  • 159
  • 168
Pawan
  • 31,545
  • 102
  • 256
  • 434

4 Answers4

3

Problem is related to jQuery mobile you are using. To change state of radio (actually, to make this state change visible), you must target elements label, too, in this case, and add/remove appropriate classes:

$('input:radio[name="trainernames"][data-value="One"]').prev('label').addClass('ui-radio-on').removeClass('ui-radio-off');

Demo: http://jsfiddle.net/cod7ceho/377/

P.S. Probably more elegant solution can be found (not sure about jquery mobile possibilities, in this case)

EDIT:

http://jsfiddle.net/cod7ceho/378/

$('input:radio[name="trainernames"][data-value="One"]').prop('checked',true);


$("input[type='radio']").checkboxradio("refresh");

Based on answers here: Checking and unchecking radio buttons with Jquery Mobile

Community
  • 1
  • 1
sinisake
  • 11,240
  • 2
  • 19
  • 27
1

You have set data-value so use it

$('input:radio[name="trainernames"][data-value="Two"]').prop('checked', true);

OR use correct value i.e. [value="9"]

$('input:radio[name="trainernames"][value="9"]').prop('checked', true);
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

You can use "checked" in input like this.

<input type="radio" data-value="Two" class="trainerradio" value="9" name="trainernames" checked>
Mubariz Feyziyev
  • 412
  • 4
  • 16
0

Your code is not working as you are targeting value insetead of data-value. Additionally if radio will be checked no UI will be updated as radio is shown through css:after pesudo class.

You can update your jQuery code to:

 $('.trainers_listWrap label').eq(0).click();
Shyam
  • 782
  • 5
  • 12