Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


How to make checkboxes to act like radio button using jQuery

Share with WhatsApp

This post covers information about how you can make checkboxes to act like radio buttons i.e. at any time only one checkbox is selected using jQuery.

In one of mine project there was this small requirement where I have to provide checkboxes which will behave like radio button. That time to achive this functionality I had added one class to all checkboxes and then have handled changed event in which I unchecked all checkboxes having that class and then again selected current checkbox. But you can achieve the same functionality in single line of code.

Suppose you have defined a checkboxes like below to select user's sport preference.

Select your sport preference :
<input type="checkbox" value="Male"> I Like Outdoor Games <br/>
<input type="checkbox" value="Female"> I Like Indoor Games <br/>
<input type="checkbox" value="All"> I like both. <br/>

Now to make above checkboxes to act like radio button, we can write a simple jQuery code like below.

    $("input:checkbox").not(this).attr('checked', false);

If you notive above code, I have used not function which deselects current checkbox from the list of checkboxes and then removed its check.

To know more about not() function you can visit here.

Checkout the live working fiddle of the above code.

Hope you have liked this simple but useful post. Do share your thoughts in comment section below.

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!