Skip to content Skip to sidebar Skip to footer

How To Get Dropdown Menus Behave Like Checkboxes?

I'm a bit of a noob, but managed to make something still with the help of one JavaScript snippet I found here (need to dig it up soon, but for now, my problem:) I'm making this web

Solution 1:

You expect menu works same as checkbox so you don't need to <option value="showAll">show all</option>

simply you can get all selected option and doing like filterFunc

Here is working sample:

 var $filterCheckboxes = $('input[type="checkbox"]');
        var $filtermenues = $('.grid1');

        filterfuncAnother = function () {
            var selectedFilters = [];

            $filtermenues.find(":selected").each(function () {
                debugger
                var v = this.value;
                if (selectedFilters.indexOf(v) === -1 && v)
                    selectedFilters.push(v);
            });

            $('.animal' && '.filterDiv')
                .hide()
                .filter(
                    function (_, a) {
                        var itemCat = $(a).data('category').split(' ');
                        if (itemCat.indexOf("showAll") > -1)
                            return;
                        return selectedFilters.every(
                            function (c) {
                                return itemCat.indexOf(c) > -1;
                            })
                    })
                .show();
        }

        var filterFunc = function () {

            var selectedFilters = [];
            debugger
            $filterCheckboxes.filter(':checked').each(function () {
                var v = this.value;
                if (selectedFilters.indexOf(v) === -1)
                    selectedFilters.push(v);
            });

            $('.animal' && '.filterDiv')
                .hide()
                .filter(
                    function (_, a) {
                        var itemCat = $(a).data('category').split(' ');
                        return selectedFilters.every(
                            function (c) {
                                return itemCat.indexOf(c) > -1;
                            })
                    })
                .show();

        }

        $filterCheckboxes.on('change', filterFunc);

        $('select').on('change', filterfuncAnother);
body {
width: 100%;
text-align: center;
background-color: black;
color: white;
}
.grid {
width: 300px;
margin: 50px auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
.filterDiv {
width: 100px;
height: 100px;
}
<!-- Help needed in this URL: https://stackoverflow.com/q/68315184/4383420 -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <div class=grid1>
        <select>
            <option value="">--</option>
            <!--<option value="showAll">show all</option>-->
            <option value="violet">violet</option>
        </select>
        <select>
            <option value="">--</option>
            <!--<option value="showAll">show all</option>-->
            <option value="blue">blue</option>
        </select>
        <select>
            <option value="">--</option>
            <!--<option value="showAll">show all</option>-->
            <option value="yellow">yellow</option>
        </select>
    </div>

<div class=grid>
    <label>violet
    <input type="checkbox" value="violet" />
    <span class="checkmark"></span>
    </label>
    <label>blue
    <input type="checkbox" value="blue" />
    <span class="checkmark"></span>
    </label>
    <label>yellow
    <input type="checkbox" value="yellow" />
    <span class="checkmark"></span>
    </label>
</div>

<div class=grid>
    <div class="filterDiv" data-category="violet blue" style="background-color: blue"></div>
    <div class="filterDiv" data-category="violet red" style="background-color: red"></div>
    <div class="filterDiv" data-category="yellow" style="background-color: yellow"></div>
</div>

Post a Comment for "How To Get Dropdown Menus Behave Like Checkboxes?"