- February 8, 2017
- Posted by: Bharat Patel
- Category: Uncategorized
A jQuery plugin is simply a way to put your code into a package. It makes it easier to maintain and use in different projects.
I have created sample jquery plugin for checking all checkboxes at one click. Also you can select odd or even row using this plugin. You need to give class of child checkboxes. Remember, Class name must be same for all.
This plugins have three attributes.
1. child – It contains name of class which is assinged to child checkboxes.
2. odd – It contains boolean value either true or false. default value is false;
3. even – It also contains boolean value either true or false. defaule value is false;
[code:js]
jQuery(function(){
jQuery(‘#one’).checkall({
child : ‘child’,
odd : true
});
});[/code]
jQuery.checkAll.js contains
[code:js](function($){
$.fn.extend({
// Check and uncheck all check boxes
checkall: function(options){
var defaults = {
child : ”,
odd : false,
even : false
};
var opts = $.extend({},defaults, options);
return this.each(function(){
obj = $(this);
obj.click(function(){
var checkStatus = $(obj).attr(‘checked’);
if(opts.odd == true){
$(‘.’+opts.child+”).attr(‘checked’,false);
$(‘.’+opts.child+’:even’).attr(‘checked’,checkStatus);
}else if(opts.even == true){
$(‘.’+opts.child+”).attr(‘checked’,false);
$(‘.’+opts.child+’:odd’).attr(‘checked’,checkStatus);
}else{
$(‘.’+opts.child+”).attr(‘checked’,checkStatus);
}
});
});
}
});
})(jQuery);[/code]