JS
Allow Only One Checkbox To Be Checked Using Jquery And Javascript
HTML checkboxes are used for picking multiple items from the list and for allowing a single selection HTML radio button to be used. Sometimes it is required to use a checkbox instead of a radio for a single selection.
- TAGS
- Javascript
- jQuery
- 4.5/5.0
- Last updated 10 September, 2022
- By Admin
1. Using jQuery
HTML
Create multiple checkboxes and add class="checkoption"
to all checkboxes
jQuery
Define click event on checkoption
class
Set all checkboxes checked
attribute to false
except the clicked checkbox.
Allow Only One Checkbox to be Checked using jQuery and JavaScript Option1
Option2
Option3
Option4
Option5
2. Using JavaScript
HTML
Create multiple checkboxes and add class="checkoption"
to all checkboxes. Define onclick
event on the checkboxes that calls checkedOnClick(this);
.
JavaScript
Create checkedOnClick()
function
Select all checkboxes where class="checkoption"
and assign to checkboxesList variable.
Loop on checkboxesList
and set checked
attribute to false
.
Set clicked checkbox checked
attribute to true
.
Allow Only One Checkbox to be Checked using jQuery and JavaScript Option1
Option2
Option3
Option4
Option5
I hope it can help you...