0

i am trying to select a radio button by JQUERY, but i am not able to do so. i tried using these but its not working.

$("#agencyBill").prop("checked", true);
$radios.filter('[value=Agency Bill]').prop('checked', true);
$("[name=customRadio]").filter("[value='Agency Bill]']").attr("checked","checked");

here is fiddle link of the same

i need to select it either by its value or by its id anyone will do.

rajesh
  • 239
  • 2
  • 6
  • 21
  • `value="Agency Bill"` you need to wrap the value in quotes. – Jai Jul 27 '18 at 07:30
  • @Jai thanks for responding i tried using $radios.filter("value=Agency Bill").prop('checked', true); $("[name=customRadio]").filter("[value=Agency Bill").attr("checked","checked"); its still not working – rajesh Jul 27 '18 at 07:32
  • see i have posted one answer. – Jai Jul 27 '18 at 07:34
  • Possible duplicate of [How to check a radio button with jQuery?](https://stackoverflow.com/questions/5665915/how-to-check-a-radio-button-with-jquery) – yash Jul 27 '18 at 07:37
  • Please read this https://stackoverflow.com/help/how-to-ask – UtkarshPramodGupta Jul 27 '18 at 07:45

4 Answers4

1

Try this : You can put both name and value attribute selector in one and use prop instead of attr to set radio checked

$(function() {
  $("[name=customRadio][value='Agency Bill']").prop("checked", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom-control custom-control-inline custom-radio">
  <input type="radio" name="customRadio" class="custom-control-input" id="dirBill" value="Direct Bill" checked>
  <label class="custom-control-label" for="dirBill">Direct Bill</label>
</div>
<div class="custom-control custom-control-inline custom-radio">
  <input type="radio" name="customRadio" class="custom-control-input" value="Agency Bill" id="agencyBill">
  <label class="custom-control-label" for="agencyBill">Agency Bill</label>
</div>

<div id="test">

</div>
31piy
  • 23,323
  • 6
  • 47
  • 67
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
0
$("#agencyBill").prop("checked", true);
$radios.filter('[value="Agency Bill"]').prop('checked', true);
$('[name="customRadio"]').filter('[value="Agency Bill]"]').attr("checked","checked");

Using proper quotes "" for selectors would solve the problem.

In your jsfiddle, include JQuery CDN for it to work. https://jsfiddle.net/974duyzb/25/

Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25
0

Because it contains a space character in the value and value is not wrapped in quotes, it won't do what you are intended to. So, you must wrap the values in quotes:

$radios.filter('[value="Agency Bill"]').prop('checked', true);
//$("[name=customRadio]").filter("[value='Agency Bill']").attr("checked","checked");

and instead of attr() use .prop() to mark it checked.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • i tried using $radios.filter('[value="Agency Bill"]').prop('checked', true); still the same reult – rajesh Jul 27 '18 at 07:38
  • @rajesh bro you have not added the jquery library to your fiddle. [updated fiddle](https://jsfiddle.net/974duyzb/50/) check this. – Jai Jul 27 '18 at 07:43
0

Your source is actually work OK, what you miss is Jquery library: Using this will OK:

$("#agencyBill").prop("checked", true);

but need to add Jquery library first:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
An Le
  • 128
  • 1
  • 8
  • thanks this resolved my problem. actually i was importing jquery.min.js from wrong source in my project too... thanks – rajesh Jul 27 '18 at 07:43