/** * @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('
').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);