Skip to content

JS Checkbox Helpers

Darryl Hein edited this page Mar 24, 2014 · 1 revision

Helps dealing with checkboxes, in checking, unchecking and toggling them.

// jQuery extensions
(function($) {
	// Checks all the elements in the jQuery object
	$.fn.check = function() {
		return this.each(function() {
			this.checked = true;
		});
	};

	// Unchecks all the elements in the jQuery object
	$.fn.uncheck = function() {
		return this.each(function() {
			this.checked = false;
		});
	};

	// Toggles the checked status of all the elements in the jQuery object
	$.fn.check_toggle = function() {
		return this.each(function() {
			this.checked = (this.checked ? false : true);
		});
	};

	// Returns if the check box is checked
	// Only works on the first element in the jQuery object
	$.fn.checked = function() {
		if (this.length > 1) {
			return this.get(0).is(':checked');
		} else if (this.length == 1) {
			return this.is(':checked');
		} else {
			return false;
		}
	};
})(jQuery);
Clone this wiki locally