Javascript is simple to achieve full selection and inverse selection of checkbox

Source: Internet
Author: User

Today, I encountered a problem when a netizen asked the Forum about all and invert selection of the input checkbox. I summarized it and wrote a simple jQuery checkbox invert selection plug-in. I hope it will be useful to everyone.
The following function can be used for simple selection and inverse selection of checkbox:

Function checkall (obj, cName)
{
Var checkboxs = document. getElementsByName (cName );
For (var I = checkboxs. length; I --;){
Checkboxs [I]. checked = obj. checked;
}
}
// Usage: add an onclick event to the selected button: onclick = "checkall (this, 'all [] ');"

The following jQuery checkbox plug-in is found online:

$. Fn. checkbox = function (){
Var t = this;
/*
* Switch all/reselect
* @ Example $ ("# checkAll"). checkbox (). toggle ($ ("input [name = 'selectall']");
*/
This. toggle = function (el ){
$ (El). click (function (){
$ (T). attr ('checked', false );
});
$ (This). click (function (){
$ (El). attr ('checked', $ (this). attr ('checked') = true? True: false );
});
};
/*
* Select all
*/
This. check = function (el ){
$ (El). attr ('checked', true );
};
/*
* Invert Selection
*/
This. uncheck = function (el ){
$ (El). attr ('checked', false );
};
Return t;
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.