I am using iCheck plugin for customizing checkboxes. I need to display certain text when one or more checkbox is checked and hide the text when none are checked.
The code I have currently displays the text on first click but doesn't hide it unless I click 2 more times. I have multiple checkboxes and would like to show text if one of 'em are checked else hide the text. Does anybody have any idea? The plugin has:
ifChecked ifChanged ifClicked ifUnchecked ifToggled ifDisabled ifEnabled.......
callbacks....Here is the plugin function
$('input').iCheck({ checkboxClass: 'icheckbox_square-blue', radioClass: 'iradio_square-blue', increaseArea: '20%' // optional });
Here is what I tried..
$('input').on('ifChecked', function(event){ $(".hide").toggle(); });
html
<input type="checkbox"> <div class"hide" style="display:none">Hi there</div>
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
To run iCheck, we need jQuery 1.6 or newer, and once you have included these libraries in your HTML document, add this line to initialize the plugin functionality. $('input'). iCheck(); iCheck will search for every radio and checkbox , and wrap them within a <div> element.
For those who struggle with this:
const value = $('SELECTOR').iCheck('update')[0].checked;
This directly returns true
or false
as boolean
.
you can get value of checkbox and status by
$('.i-checks').on('ifChanged', function(event) { alert('checked = ' + event.target.checked); alert('value = ' + event.target.value); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With