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:

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 :

 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

$('#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 :

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 :

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

 

 

Recently Published

»

Is Your Business Site Fully Accessible?

Does your business site have full accessibility? Even with the ADA ...

»

Secure Web: 5 Best Practices For Application Development

There are several essential best practices for secure web ...

»

Laravel PHP Framework: Features and benefits for web development

Companies and organizations are focusing on developing custom web ...

»

Web Development Vs. Software Development: What Is the Difference?

As technology continues to transform all aspects of our lives, a lot ...

»

Web Redesign : Is Your Landing Page Begging for one?

How do you know when it’s time to web redesign your landing page? ...

»

Customer Acquisition: Few Steps To Improve This.

Customer acquisition is essential to any business’s success — to ...

»

Mobile App Development Tools: 5 Enterprise-Ready For Rapid Deployment

There are several enterprise-ready mobile app development tools for ...

»

Docker: How To Build Better Development Security With It

The top concern among developers and DevOps teams is security within ...

»

Mobile Development: How to Reduce Costs

Modern markets have become so competitive that it has become vital ...