Files
MaszToZadanieDomowe.com/dziennik/jquery.checkbox.js
2026-02-10 21:35:37 +01:00

69 lines
2.4 KiB
JavaScript

/**
* @Author: Dawid Pośliński
* @Website: http://poslinski.net
* @Modified: Bartek Medoń, www.bmedon.net
*/
(function($) {
$.fn.checkbox = function(method) {
if(typeof method === 'boolean'){
var div = $(this).parent().children('div.checkbox');
if(div){
if(method === true) div.removeClass("checked");
else div.addClass("checked");
onClick(3, $(this).parent().children('div.checkbox'));
}
return;
}
$('body').off('click.checkbox', 'div.checkbox:not(.disabled)').on('click.checkbox', 'div.checkbox:not(.disabled)', onClick);
$('body').off('keypress.checkbox', 'div.checkbox:not(.disabled)').on('keypress.checkbox', 'div.checkbox:not(.disabled)', onKeypress);
$('body').off('click.checkbox-label', 'label').on('click.checkbox-label', 'label', onClickLabel);
return this.each(function(){
if(!$(this).filter('.checkboxed').length){
var checked = "";
var disabled = '';
if( $(this).attr('disabled') != undefined) disabled = ' disabled';
if ( $(this).attr("checked") != undefined) checked = " checked";
$(this).addClass('checkboxed').after('<div data-val="'+$(this).attr('value')+'" class="checkbox'+checked+disabled+'" tabindex="0"></div>').hide();
}
// $(this).next().unbind('click').click(onClick);
// $(this).next().unbind('keypress').keypress(onKeypress);
});
function onClickLabel(e){
if($(this).children('input.checkbox[type="checkbox"]').length === 1){
e.preventDefault();
if(!$(e.target).is('div.checkbox')){
onClick(2, $(this).children('div.checkbox'));
}
}
}
function onClick(type, ths) {
if(!ths){
ths = $(this);
}
if ( ths.hasClass("checked") ) {
ths.removeClass("checked");
ths.parent().children(":checkbox[value="+ths.attr("data-val")+"]").removeAttr("checked").prop('checked', false).trigger('change');
}
else {
ths.addClass("checked");
ths.parent().children(":checkbox[value="+ths.attr("data-val")+"]").attr("checked","checked").prop('checked', true).trigger('change');
}
}
function onKeypress(event) {
if (event.keyCode === 32) {
onClick(2, $(this));
}
return false;
}
};
})(jQuery);