How to find the Status of a Radio Button Using jQuery

When I was working on one of my projects I suddenly landed up in a situation where I wanted to find out as to which radio button has been selected or checked. So having discovered that, I am going to present you with different ways in which you can find out whether a radio button has been checked or not:

1)  Here in the below line of jQuery code you can see that it will give you the status of your radio button. This code fragment will find out whether the “Checked” property of the radio button has been tick marked or not and based on the output with return either true or false. One point here to be noted is that the below line of code will function properly with jQuery Version 1.7.2 and higher:

1
var isChecked = $('#rdSelect').prop('checked');

Nevertheless if you are not having the latest version of jQuery installed then you must use the below code fragment to select the value of a radio button :

1
 var isChecked = $('#rdSelect').attr('checked')?true: false;

 

I happened to read on different sources that the “Checked” attribute of a radio button will return either true or false if it is used with the “attr” method  however this is not so true because if you checked the checked box then the result will give you the status as checked otherwise it will give the status to be undefined. So if you want the result to be either true or false in that case make use of the conditional expression that is mentioned in the above code fragment.

 

2)  One more method to get the value of the selected radio button is to use the

1
$('#rdSelect: checked').Val()

method which will return undefined if the radio button is not checked and return “On” if it is checked.
Here is the code fragment  to be used :

1
var isChecked = $('#rdSelect: checked').Val()?true: false;

3) Another simple way is to make use of the is() selector so that it will return either true or false based on the status of the radio button.

Here is the code fragment  to be used :

1
var isChecked = $('#rdSelect').is(‘checked’);

 

 

Recently Published

»

Do You Really Need Access to Advanced Scripting Capabilities?

One of the features you’ll find most commonly advertised when ...

»

Obvious UX Trends that will Rule 2018

For web design industry, we see new UX trends in the way we build ...

»

Java Programming Language And Its Application

Programming language is one of the main IT developments of the past ...

»

Why Can’t I Add or Install Plugins in WordPress

WordPress is a great choice when it comes to Content Management ...

»

5 Website Security Myths You Should Know About

Businesses today are getting increasingly dependent on their ...

»

Capturing HD Videos with Movavi Screen Capture Studio

As you probably know High Definition (HD) videos are regarded as the ...

»

Modern Changes That You Should Consider for Your Web Site

Websites need to modernize and get updated periodically otherwise ...

»

Super Tips For A Flawless Website

A website has a specific purpose. And your job is to design a website ...

»

Wix Offers Best Free Web Apps for Artists and Photographers

The famous online website builder Wix is known for providing the ...