- Posted On 18 December 2014
- By
- In Programming
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 : <hr/> <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").click(function(){ $("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.
- Tags :
- ASP.NET
- MVC
- jQueryJavascript
Top 10 Visual Studio things which can boost developers coding speed
Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.
Visual Studio 2008 Shell and TFS integration
Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
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.
How to select all checkboxes as per range of numbers in jQuery MVC
JQuery code to select all checkboxes as per range provided with select all and deselect all functionality which can be embedded in ASP.NET MVC too. With validations such as invalid range.