Navigation

Tuesday 21 August 2018

Multiselect - JQuery For Dropdown Box With Checkboxes


HTML part of multi-selector dropdown box




<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="js/jquery1.11.1.js"></script>
    <style>
        /*-------------------------*/
        body {
            font: normal 14px/100% "Andale Mono", AndaleMono, monospace;
            color: #fff;
            padding: 50px;
            width: 500px;
            margin: 0 auto;
            background-color: #374954;
        }
        .dropdown {
            position: relative;
            top: 50%;
            transform: translateY(50%);
        }

        a {
            color: #fff;
        }

        .dropdown dd,
        .dropdown dt {
            margin: 0px;
            padding: 0px;
        }

        .dropdown ul {
            margin: -1px 0 0 0;
        }

        .dropdown dd {
            position: relative;
            /*margin-left: 250px;*/
        }

        @media (max-width:767px) {
            .dropdown dd {
                position: relative;
                margin-left: 0px;
            }
        }

        .dropdown a,
        .dropdown a:visited {
            color: black;
            text-decoration: none;
            outline: none;
            font-size: 12px;
        }

        .dropdown dt a {
            background-color: white;
            display: block;
            padding: 7px 13px 0px 10px;
            height: 100%;
            /* line-height: 2px; */
            overflow: hidden;
            border: 0;
            width: 64%;
            border: solid 1px #e4e4e4;
            border-radius: 5px;
        }

            .dropdown dt a span,
            .multiSel span {
                cursor: pointer;
                display: inline-block;
                padding: 0 3px 2px 0;
            }

        .dropdown dd ul {
            background-color: white;
            border: solid 1px #e8e8e8;
            color: black;
            display: none;
            left: 0px;
            padding: 7px;
            position: absolute;
            top: 0px;
            width: 275px;
            list-style: none;
            height: 150px;
            overflow: auto;
            box-shadow: 0px 4px 7px rgba(0, 0, 0, 0.12);
            border-radius: 0px 0px 5px 5px;
        }

            .dropdown dd ul li {
                padding: 5px 0px;
            }

                .dropdown dd ul li input {
                    margin-right: 5px;
                }

        .dropdown span.value {
            display: none;
        }

        .dropdown dd ul li a {
            padding: 5px;
            display: block;
        }

            .dropdown dd ul li a:hover {
                background-color: white;
            }
    </style>

    <title></title>
</head>
<body>
    <div class="multiSelddl">

        <dl class="dropdown">

            <dt> 
                <a href="javascript:void(0);"> 
                    <span class="hida">Select</span> 
                    <p class="multiSel"></p> 
                </a> 
            </dt> 
            <dd> 
              <div class="mutliSelect"> 
               <ul> 
                  <li><input type="checkbox" value="Ticket Taker" onchange="fnAddValue(this)" />Ticket Taker</li>

                 <li><input type="checkbox" value="Security Guard" onchange="fnAddValue(this)"/>Security Guard</li>

                 <li><input type="checkbox" value="Event Staff" onchange="fnAddValue(this)"/>Event Staff</li>

                <li> <input type="checkbox" value="Event Manager" onchange="fnAddValue(this)"/>Event Manager</li> 
              </ul> 
             </div> 
            </dd> 
        </dl> 
    </div>

JQuery part of the multi-selector dropdown box.


    <script>
        function isNumberKey(evt) {

            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }

        $(".dropdown dt a").on('click', function () {
            $(".dropdown dd ul").slideToggle('fast');
        });

        $(".dropdown dd ul li a").on('click', function () {
            $(".dropdown dd ul").hide();
        });

        function getSelectedValue(id) {
            return $("#" + id).find("dt a span.value").html();
        }

        $(document).bind('click', function (e) {
            var $clicked = $(e.target);
            if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
        });
        //$('.mutliSelect input[type="checkbox"]').on('click', function () {
        //    debugger;
        //    var title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
        //      title = $(this).val() + ",";

        //    if ($(this).is(':checked')) {
        //        var html = '<span title="' + title + '">' + title + '</span>';
        //        $('.multiSel').append(html);
        //        $(".hida").hide();
        //    }
        //    else {
        //        $('span[title="' + title + '"]').remove();
        //        var ret = $(".hida");
        //        $('.dropdown dt a').append(ret);
        //    }
        //});

        function fnAddValue(Obj) {
            debugger;
            var title = $(Obj).closest('.mutliSelect').find('input[type="checkbox"]').val(),
              title = $(Obj).val() + ",";
            if ($(Obj).is(':checked')) {
                var html = '<span title="' + title + '">' + title + '</span>';
                $('.multiSel').append(html);
                $(".hida").hide();
            }
            else {
                $('span[title="' + title + '"]').remove();
                var ret = $(".hida");
                $('.dropdown dt a').append(ret);
                if ($('.multiSel').find("span").length == 0) {
                    $(".hida").show();
                }
                else {
                    $(".hida").hide();
                }

            }
        }
    </script>
</body>
</html>


No comments:

Post a Comment