Need help getting the length of :checked usin .on("click")

Need help getting the length of :checked usin .on("click")

Hey Guys. I wrote the script below to display an alert message if the user has selected more than 2 items. But it doesn't seem to be working for me...

Any help would be really appreciated. Thanks!!

http://jsfiddle.net/eldan88/qyu9N/

  1. <script>


        $(function(){

           var $checkboxes = $("input[name*='max_allowed']:checked");
           var $checkbox_len =  $checkboxes.length;


            var $max_len = 2;

            $($checkboxes).on("click", function(){
               if($max_len > $checkbox_len){
                   alert("You have selected more than 2 checkboxes");
               }
            });




        });


     </script>
  1. <input type="checkbox" name="max_allowed">
    <input type="checkbox" name="max_allowed">
    <input type="checkbox" name="max_allowed">
    <input type="checkbox" name="max_allowed">