Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iCheck check if checkbox is checked

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> 
like image 497
user3006683 Avatar asked Dec 23 '13 02:12

user3006683


People also ask

How do you check if checkbox is checked or not?

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.

How check if checkbox is checked jQuery?

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);

Which method is used to check the status of checkbox?

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.

How do you use iCheck?

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.


2 Answers

For those who struggle with this:

const value = $('SELECTOR').iCheck('update')[0].checked; 

This directly returns true or false as boolean.

like image 96
Sercan Ozdemir Avatar answered Oct 10 '22 07:10

Sercan Ozdemir


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); }); 
like image 44
grud phunsanit Avatar answered Oct 10 '22 07:10

grud phunsanit