Welcome to our comprehensive guide on the jQuery Checkboxradio Widget! In this lesson, we'll learn how to create and style checkboxes and radio buttons using jQuery, making your web projects more interactive and user-friendly. Let's get started!
Before we dive into the jQuery Checkboxradio Widget, let's first understand what checkboxes and radio buttons are and when to use them.
Checkboxes are used when multiple selections are allowed from a group of options. Each checkbox is independent and can be selected or deselected at will.
Radio buttons, on the other hand, are used when only one selection is allowed from a group. They are often used in forms for selecting an option from a predefined set.

The jQuery Checkboxradio Widget simplifies the process of creating and styling checkboxes and radio buttons. It provides a more flexible and efficient way of handling user interactions, compared to the native HTML elements.
Before we proceed, ensure you have jQuery included in your HTML file. If not, you can add it using the following script tag:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>Let's create a simple checkboxradio widget using the $.fn.checkboxradio() function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="checkbox-radio">
<input type="checkbox" id="check1" name="checkbox1" value="option1">
<label for="check1">Option 1</label>
<input type="checkbox" id="check2" name="checkbox2" value="option2">
<label for="check2">Option 2</label>
</div>
<script>
$(function() {
$('.checkbox-radio input[type="checkbox"]').checkboxradio();
});
</script>
</body>
</html>In the above example, we have created a simple checkboxradio widget with two checkboxes. The $.fn.checkboxradio() function is applied to the checkbox elements wrapped in a div with the class checkbox-radio.
By default, the jQuery Checkboxradio Widget styles the checkboxes and radio buttons to have a more modern appearance. However, you can customize the widget to match your project's design.
<style>
.checkbox-radio label {
display: inline-flex;
align-items: center;
margin-right: 10px;
cursor: pointer;
}
.checkbox-radio input[type="checkbox"] {
display: none;
}
.checkbox-radio .checkbox {
width: 24px;
height: 24px;
border: 1px solid #ccc;
border-radius: 3px;
background-color: #fff;
display: inline-block;
margin-right: 5px;
position: relative;
}
.checkbox-radio .checkbox::before {
content: "";
display: block;
width: 16px;
height: 16px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
position: absolute;
top: 3px;
left: 3px;
transition: all 0.2s ease-in-out;
}
.checkbox-radio input[type="checkbox"]:checked + label .checkbox {
border-color: #3498db;
}
.checkbox-radio input[type="checkbox"]:checked + label .checkbox::after {
content: "✓";
font-size: 14px;
text-align: center;
line-height: 16px;
position: absolute;
top: 3px;
left: 3px;
}
</style>In the above CSS, we've created a custom style for the checkboxradio widget, making it more visually appealing.
What does the `$.fn.checkboxradio()` function do in jQuery?
That's it for today's lesson! In the next part, we'll delve deeper into advanced features and customizations of the jQuery Checkboxradio Widget. Stay tuned! 🎓🚀