Please navigate to the bottom of the page for Table of Contents

Saturday, July 16, 2011

jQuery to select/deselect all items in a CheckBoxList

In modern day sites, the user sometimes is presented with multiple options and she can either select individual options or all. The same thought also applies when deselecting the options. For example, in the screen shot below, the user can select one or more options:

image

Now wouldn’t it be nice that the user had a simple way to select/deselect all the containers in one click?

Question: Using jQuery, provide a simple way for the user to select and deselect all the checkboxes in a checkboxlist.

Let us first build our ASP.NET page. The page will have 2 basic elements: A standalone asp:CheckBox to enable select/deselect all option. An asp:CheckBoxList to hold our user options.

Select one or more containers:<br /><br />
<!-- a standalone checkbox to select/deselect all -->
<asp:CheckBox runat="server" ID="selectAllCb" Text="Select All" />

<!-- our actual list containing user options -->
<asp:checkboxlist runat="server" ID="containerCBL">
<asp:ListItem>Small</asp:ListItem>
<asp:ListItem>Large</asp:ListItem>
<asp:ListItem>Micro</asp:ListItem>
<asp:ListItem>Regular</asp:ListItem>
<asp:ListItem>Unknown</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
<asp:ListItem>Virtual</asp:ListItem>
</asp:checkboxlist>


With our ASP.NET page defined, the jQuery part is actually quite simple. All we need to do is to attach to the click() event of the standalone checkbox and copy its checked state to the checkbox list.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SelectAll.aspx.cs" 
Inherits="jQueryInterviewQuestions.SelectAll" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Select/Deselect</title>
<!-- include jQuery -->
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
   1:  
   2:  
   3:     <script type="text/javascript">
   4:         // hook to the document ready function
   5:         $(document).ready(function () 
   6:         {
   7:             // find the select all checkbox
   8:             var selectAll = $('#<%=selectAllCb.ClientID%>');
   9:  
  10:             // hook onto select all checkbox's click event
  11:             selectAll.click(
  12:                 function () 
  13:                 {
  14:                     // for all input types of checkbox
  15:                     // for the container
  16:                     // set the attribute to the value
  17:                     // of select all checkbox
  18:                     $("#<%=containerCBL.ClientID%> input[type='checkbox']").
  19:                     attr('checked', selectAll.is(':checked'));
  20:                 }
  21:             );
  22:         });
  23:     
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
Select one or more containers:<br /><br />
<!-- a standalone checkbox to select/deselect all -->
<asp:CheckBox runat="server" ID="selectAllCb" Text="Select All" />

<!-- our actual list containing user options -->
<asp:checkboxlist runat="server" ID="containerCBL">
<asp:ListItem>Small</asp:ListItem>
<asp:ListItem>Large</asp:ListItem>
<asp:ListItem>Micro</asp:ListItem>
<asp:ListItem>Regular</asp:ListItem>
<asp:ListItem>Unknown</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
<asp:ListItem>Virtual</asp:ListItem>
</asp:checkboxlist>
</div>
</form>
</body>
</html>

26 comments:

  1. very good post. Thank you

    ReplyDelete
  2. Really Useful. Thank you! :)

    ReplyDelete
  3. Crypto-currency as a modern form of the digital asset has received a worldwide acclaim for easy and faster financial transactions and its awareness among people have allowed them to take more interest in the field thus opening up new and advanced ways of making payments. Crypto.com Referral Code with the growing demand of this global phenomenon more,new traders and business owners are now willing to invest in this currency platform despite its fluctuating prices however it is quite difficult to choose the best one when the market is full. In the list of crypto-currencies bit-coins is one of the oldest and more popular Crypto.com Referral Code for the last few years. It is basically used for trading goods and services and has become the part of the so-called computerized block-chain system allowing anyone to use it thus increasing the craze among the public, Crypto.com Referral Code.

    Common people who are willing to purchase BTC can use an online wallet system for buying them safely in exchange of cash or credit cards and in a comfortable way from the thousands of BTC foundations around the world and keep them as assets for the future. Due to its popularity, many corporate investors are now accepting them as cross-border payments and the rise is unstoppable. With the advent of the internet and mobile devices,information gathering has become quite easy as a result the BTC financial transactions are accessible and its price is set in accordance with people’s choice and preferences thus leading to a profitable investment with Crypto.com Referral Code. Recent surveys have also proved that instability is good for BTC exchange as if there is instability and political unrest in the country due to which banks suffer then investing in BTC can surely be a better option. Again bit-coin transaction fees are pretty cheaper and a more convenient technology for making contracts thus attracting the crowd. The BTC can also be converted into different fiat currencies and is used for trading of securities, for land titles, document stamping, public rewards and vice versa.

    Another advanced block-chain project is Ethereumor the ETH which has served much more than just a digital form of crypto-currency Crypto.com Referral Code and its popularity in the last few decades have allowed billions of people to hold wallets for them. With the ease of the online world,the ETH have allowed the retailers and business organizations to accept them for trading purposes, therefore, can serve as the future of the financial system.

    ReplyDelete
  4. Internet Download Manager Crack ed Download
    If you cannot afford to purchase the Internet Download Manager sequential number in the (moment), then it is possible to readily download the online download manager crack version. Don't stress IDM Crack can also be full edition, you may down load the deciphered IDM from the URL given below or you need to utilize IDM serial number to register the online downloadmanager.
    Once we know, there are many IDM busted models are available and also the Internet download manager limitation is also available. Still, if you are a person who can readily afford the applications, then you must purchase the applications as it ailing help the programmers to pay for their bills also to spend the money generated by sales of the Internet download manager or even IDM crack full variant in making the software easier and much more useful because new features also cost the company in execution and research. You can purchase the original IDM full variant through their official site.

    ReplyDelete
  5. Vacation rentals in Dubai is a DTCM licensed holiday home Operator managing vacation rental villas and apartments for short and mid term stay in Dubai.

    ReplyDelete
  6. Friendly Finance is South Africa's number 1 choice for finance - providing customers with all the information they need to make better financial decisions. friendly finance We provide hundreds of comparisons of common consumer finance products, such as personal loans, car loans, short-term loans and credit cards. Think of us as a helping hand in selecting consumer finance products.

    ReplyDelete
  7. Testimonial Review of Taskade, Taskade Ratings the all-in-one collaboration platform for remote teams. Unleash your team productivity with task lists, mindmaps, and video chat.

    ReplyDelete
  8. Recently, the keto diet has become extremely popular for its health benefits such as weight loss and preventing disease. A custom Keto plan for life! Visit here: keto custom plan

    ReplyDelete
  9. Find the perfect handmade gift, sana sana rana vintage & on-trend clothes, unique jewelry, and more… lots more.

    ReplyDelete
  10. ac repair and services in bachupally Extend the Lifespan of Your System with Proper Maintenance Like all machines, air conditioners benefit from regular maintenance; we know that for a fact.

    ReplyDelete
  11. Impresión online en gran formato, Photocalls personalizados imprimimos roll ups, photocalls, vinilos, lonas. Envío gratis

    ReplyDelete
  12. Crystal online pharmacy is a trusted online drug store with a wide range of products to suit the needs of our clients. Misoprostol for sales Crystal Pharmacy do strive to offer the best service and ship products world wide. All the products listed on our website are Ava in stock. Expect your order to be processed Immediately when you send us your request. We deal with varieties of drugs for our customers satisfaction. We cross barriers with our products and struggle hard to meet human satisfaction. When shopping with us, Be safe and secured and you will realize how swift we are with our services.

    ReplyDelete
  13. Universal Gun sales is a trusted Firearm company base in Los Angeles California USA. It is one of the top Firearms Company in this part of the country. Buy CCI suppressor 22LR 45 GR HP 50RD online They do offer the best firearms deal in the USA and to over 25 countries in the world. You can always visit their shop in Los Angeles using the address on their website. Place your order and get served by the best Firearm Company in the planet. Have fun.

    ReplyDelete
  14. massage mundaring is run by a talented female massage therapist and is based from her home studio in Mundaring. Trained in an introductory Traditional Thai Style Yoga massage course, in Northern Thailand mid 2015, Her eyes opened to the different cultural style of massage, which is offered fully clothed with pressing, stretching, extending and rotating techniques focusing on meridian and acupressure points. Her chosen style of massage is Deep Tissue and Trigger Point which she believes to be powerful at unlocking and manipulating the muscle fibers, allowing new oxygen and blood to restore the tissue. Her technique is described by her clients as fluid, deep, rhythmic, releasing and restorative.

    ReplyDelete

  15. Great set of tips from the master himself. Excellent ideas. Thanks for Awesome tips Keep it up
    nuance-paperport-professional-crack
    da-formmaker-crack

    ReplyDelete
  16. I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
    Very interesting blog.
    Speedify Crack

    ReplyDelete
  17. I'm really impressed with your writing skills, as smart as the structure of your


    Latest Software Free Download



    weblog. Is this a paid topic



    Diskdigger crack



    do you change it yourself? However, stopping by with great quality writing, it's hard to see any good blog today.



    Program4pc audio converter-crack





    Diskdigger-crack





    Klevgrand complete bundal -crack




    Coolutils total mail converter pro crack

    ReplyDelete
  18. yeah, This is great place where we collected such a nice informations. Thank you!

    movavi-slideshow-maker-crack
    easeus-partition-crack
    zemana-antilogger-crack

    ReplyDelete
  19. If you really desire to get such type of information, visit this blog quickly.
    user interface design companies

    ReplyDelete
  20. We're more than just a san diego web design Company. With our full-service digital marketing packages, you can get the most out of your online marketing

    ReplyDelete
  21. Freelancers Incubator is a European educational institution specializing in the field of freelancing and fersonal growth. Become a freelancer

    ReplyDelete
  22. Just 99 Web Design offers affordable web design packages for every business, from basic one-page websites to full eCommerce website design services. inexpensive web design companies

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete