Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check radio button is checked using JQuery?

I have two radio buttons in one group, I want to check the radio button is checked or not using JQuery, How ?

like image 751
Bader Avatar asked Jan 04 '12 10:01

Bader


People also ask

How do you check whether a radio button is checked or not in jQuery?

We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.

How do you check the radio button is checked?

To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.

How do you check radio button is checked or not in JS?

Use document. getElementById('id'). checked method to check whether the element with selected id is check or not. If it is checked then display its corresponding result otherwise check the next statement.


2 Answers

Given a group of radio buttons:

<input type="radio" id="radio1" name="radioGroup" value="1"> <input type="radio" id="radio2" name="radioGroup" value="2"> 

You can test whether a specific one is checked using jQuery as follows:

if ($("#radio1").prop("checked")) {    // do something }  // OR if ($("#radio1").is(":checked")) {    // do something }  // OR if you don't have ids set you can go by group name and value // (basically you need a selector that lets you specify the particular input) if ($("input[name='radioGroup'][value='1']").prop("checked")) 

You can get the value of the currently checked one in the group as follows:

$("input[name='radioGroup']:checked").val() 
like image 189
nnnnnn Avatar answered Sep 22 '22 09:09

nnnnnn


  //the following code checks if your radio button having name like 'yourRadioName'  //is checked or not $(document).ready(function() {   if($("input:radio[name='yourRadioName']").is(":checked")) {       //its checked   } });  
like image 40
Sudhir Bastakoti Avatar answered Sep 20 '22 09:09

Sudhir Bastakoti