drop down with checkboxes in django form












1















Hi I want to have a dropdown list in my django form with a checkbox in front of every option in the drop down. I have tried using multiple choice field with selectmultiple widget but this displays every option with checkboxes on the page. They are not contained inside the drop down. is there a way to contain them inside the dropdown?










share|improve this question



























    1















    Hi I want to have a dropdown list in my django form with a checkbox in front of every option in the drop down. I have tried using multiple choice field with selectmultiple widget but this displays every option with checkboxes on the page. They are not contained inside the drop down. is there a way to contain them inside the dropdown?










    share|improve this question

























      1












      1








      1








      Hi I want to have a dropdown list in my django form with a checkbox in front of every option in the drop down. I have tried using multiple choice field with selectmultiple widget but this displays every option with checkboxes on the page. They are not contained inside the drop down. is there a way to contain them inside the dropdown?










      share|improve this question














      Hi I want to have a dropdown list in my django form with a checkbox in front of every option in the drop down. I have tried using multiple choice field with selectmultiple widget but this displays every option with checkboxes on the page. They are not contained inside the drop down. is there a way to contain them inside the dropdown?







      django






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 4 '14 at 7:27









      user3705968user3705968

      161




      161
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Dropdowns and checkboxes are HTML elements that are rendered by the browser using its built-in components. Those components don't have any support for combining them: in pure HTML, you simply can't combine a select with a check box.



          The only way to do this would be to use components rendered purely in Javascript. Google's Closure UI tools is one set of controls I've used, but only because I used to work at Google: something like jQuery UI might have a version that's easier to use.






          share|improve this answer
























          • Hi, Daniel, thax for the answer. But in jquery there is a way to combine check boxes and drop downs. But the problem here is that how do i implement it in a django form which is written in python? I can create a dropdown with check boxes in the django template but then i would not be able to "submit" my selection from the drop down. Please help...

            – user3705968
            Jun 4 '14 at 15:04





















          0














          I see that you asked this four years ago so I doubt you are still looking for an answer, but I might as well provide in case someone else finds it!



          Basically you want to make a div with an unordered list inside of it, where each item in that list contains a checkbox input.



          Then, you use jQuery so that when you click on the div, it gets assigned the 'selected' class in its html.



          Then you make your CSS so that the dropdown menu itself only shows up when it has the 'selected' class.



          The JSFiddle is here (minus the django templating, obviously):
          https://jsfiddle.net/lymanjohnson/2L71nhko/15/



          And code is below:



          HTML (django template):



              <fieldset class="item toggle-item">
          <div class="legend-container">
          <legend>Choices</legend>
          </div>
          <ul class="scrollable-dropdown-list">
          {% for choice in choices %}
          <li>
          <input type="checkbox" class="custom-control-input filter" id="choice_{{forloop.counter}}" name="choice" value="{{choice}}">
          <label for="choice_{{forloop.counter}}"class="custom-control-label">{{choice}}</label>
          </li>
          {% endfor %}
          </ul>
          </fieldset>


          JQUERY:



              <script>


          $(document).ready(function() {
          // Listener for when you click on the dropdown menu
          $('fieldset.toggle-item > .legend-container').on('click', (event) => {
          // Adds or removes the 'selected' attribute on the dropdown menu you clicked
          $(event.currentTarget).parent().toggleClass('selected')
          // If you have multiple dropdown menus you may want it so that when you open Menu B, Menu A
          // automatically closes.
          // This line does that by removing 'selected' from every dropdown menu other than the one you clicked on.
          // It's 'optional' but it definitely feels better if you have it
          $('fieldset.toggle-item').not($(event.currentTarget).parent()).removeClass('selected')
          })

          // The user is probably going to expect that any and all dropdown menus will close if they click outside of them. Here's how to make that happen:

          //This listens for whenever you let go of the mouse
          $(document).mouseup(function(e)
          {
          // make this a variable just to make the next line a little easier to read
          // a 'container' is now any
          var dropdown_menus = $("fieldset.toggle-item");

          // if the target of the click isn't a dropdown menu OR any of the elements inside one of them
          if (!dropdown_menus.is(e.target) && dropdown_menus.has(e.target).length === 0)
          {
          // then it will de-select (thereby closing) all the dropdown menus on the page
          $('fieldset.toggle-item').removeClass('selected')

          }
          });
          })

          </script>


          CSS:



          <style>
          .item {
          width: 33%;
          margin: 2px 1% 2px 1%;
          border: 0;
          }

          .item li {
          list-style: none;
          }

          .scrollable-dropdown-list{
          position: absolute;
          max-height:200px;
          width:33%;
          overflow-y:scroll;
          overflow-x:auto;
          margin: 0;
          padding-left: 1em;
          border-style: solid;
          border-width: thin;
          border-color: grey;
          background-color: white;
          }

          legend {
          margin-bottom: 0;
          font-size: 18px;
          }

          label {
          font-weight: normal;
          margin-left:20px;
          }


          .legend-container {
          cursor: pointer;
          width: 100%;
          display: flex;
          padding: 0;
          margin-bottom: 0px;
          font-size: 21px;
          line-height: inherit;
          color: #333;
          border: 0;
          border-bottom: none;
          }

          fieldset {
          border-width: thin;
          border-color: gray;
          border-style: solid;
          width:50px;
          }

          /* Note that all the browser-specific animation stuff is totally optional, but provides a nice subtle animation for the dropdown effect */

          fieldset ul.scrollable-dropdown-list {
          display: none;
          -webkit-animation: slide-down .3s ease-out;
          -moz-animation: slide-down .3s ease-out;
          }

          fieldset.selected ul.scrollable-dropdown-list {
          display: block;
          -webkit-animation: slide-down .3s ease-out;
          -moz-animation: slide-down .3s ease-out;
          }



          @-webkit-keyframes slide-down {
          0% {
          opacity: 0;
          -webkit-transform: translateY(-10%);
          }
          100% {
          opacity: 1;
          -webkit-transform: translateY(0);
          }
          }

          @-moz-keyframes slide-down {
          0% {
          opacity: 0;
          -moz-transform: translateY(-10%);
          }
          100% {
          opacity: 1;
          -moz-transform: translateY(0);
          }
          }
          </style>





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24031461%2fdrop-down-with-checkboxes-in-django-form%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Dropdowns and checkboxes are HTML elements that are rendered by the browser using its built-in components. Those components don't have any support for combining them: in pure HTML, you simply can't combine a select with a check box.



            The only way to do this would be to use components rendered purely in Javascript. Google's Closure UI tools is one set of controls I've used, but only because I used to work at Google: something like jQuery UI might have a version that's easier to use.






            share|improve this answer
























            • Hi, Daniel, thax for the answer. But in jquery there is a way to combine check boxes and drop downs. But the problem here is that how do i implement it in a django form which is written in python? I can create a dropdown with check boxes in the django template but then i would not be able to "submit" my selection from the drop down. Please help...

              – user3705968
              Jun 4 '14 at 15:04


















            0














            Dropdowns and checkboxes are HTML elements that are rendered by the browser using its built-in components. Those components don't have any support for combining them: in pure HTML, you simply can't combine a select with a check box.



            The only way to do this would be to use components rendered purely in Javascript. Google's Closure UI tools is one set of controls I've used, but only because I used to work at Google: something like jQuery UI might have a version that's easier to use.






            share|improve this answer
























            • Hi, Daniel, thax for the answer. But in jquery there is a way to combine check boxes and drop downs. But the problem here is that how do i implement it in a django form which is written in python? I can create a dropdown with check boxes in the django template but then i would not be able to "submit" my selection from the drop down. Please help...

              – user3705968
              Jun 4 '14 at 15:04
















            0












            0








            0







            Dropdowns and checkboxes are HTML elements that are rendered by the browser using its built-in components. Those components don't have any support for combining them: in pure HTML, you simply can't combine a select with a check box.



            The only way to do this would be to use components rendered purely in Javascript. Google's Closure UI tools is one set of controls I've used, but only because I used to work at Google: something like jQuery UI might have a version that's easier to use.






            share|improve this answer













            Dropdowns and checkboxes are HTML elements that are rendered by the browser using its built-in components. Those components don't have any support for combining them: in pure HTML, you simply can't combine a select with a check box.



            The only way to do this would be to use components rendered purely in Javascript. Google's Closure UI tools is one set of controls I've used, but only because I used to work at Google: something like jQuery UI might have a version that's easier to use.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 4 '14 at 8:33









            Daniel RosemanDaniel Roseman

            454k41587644




            454k41587644













            • Hi, Daniel, thax for the answer. But in jquery there is a way to combine check boxes and drop downs. But the problem here is that how do i implement it in a django form which is written in python? I can create a dropdown with check boxes in the django template but then i would not be able to "submit" my selection from the drop down. Please help...

              – user3705968
              Jun 4 '14 at 15:04





















            • Hi, Daniel, thax for the answer. But in jquery there is a way to combine check boxes and drop downs. But the problem here is that how do i implement it in a django form which is written in python? I can create a dropdown with check boxes in the django template but then i would not be able to "submit" my selection from the drop down. Please help...

              – user3705968
              Jun 4 '14 at 15:04



















            Hi, Daniel, thax for the answer. But in jquery there is a way to combine check boxes and drop downs. But the problem here is that how do i implement it in a django form which is written in python? I can create a dropdown with check boxes in the django template but then i would not be able to "submit" my selection from the drop down. Please help...

            – user3705968
            Jun 4 '14 at 15:04







            Hi, Daniel, thax for the answer. But in jquery there is a way to combine check boxes and drop downs. But the problem here is that how do i implement it in a django form which is written in python? I can create a dropdown with check boxes in the django template but then i would not be able to "submit" my selection from the drop down. Please help...

            – user3705968
            Jun 4 '14 at 15:04















            0














            I see that you asked this four years ago so I doubt you are still looking for an answer, but I might as well provide in case someone else finds it!



            Basically you want to make a div with an unordered list inside of it, where each item in that list contains a checkbox input.



            Then, you use jQuery so that when you click on the div, it gets assigned the 'selected' class in its html.



            Then you make your CSS so that the dropdown menu itself only shows up when it has the 'selected' class.



            The JSFiddle is here (minus the django templating, obviously):
            https://jsfiddle.net/lymanjohnson/2L71nhko/15/



            And code is below:



            HTML (django template):



                <fieldset class="item toggle-item">
            <div class="legend-container">
            <legend>Choices</legend>
            </div>
            <ul class="scrollable-dropdown-list">
            {% for choice in choices %}
            <li>
            <input type="checkbox" class="custom-control-input filter" id="choice_{{forloop.counter}}" name="choice" value="{{choice}}">
            <label for="choice_{{forloop.counter}}"class="custom-control-label">{{choice}}</label>
            </li>
            {% endfor %}
            </ul>
            </fieldset>


            JQUERY:



                <script>


            $(document).ready(function() {
            // Listener for when you click on the dropdown menu
            $('fieldset.toggle-item > .legend-container').on('click', (event) => {
            // Adds or removes the 'selected' attribute on the dropdown menu you clicked
            $(event.currentTarget).parent().toggleClass('selected')
            // If you have multiple dropdown menus you may want it so that when you open Menu B, Menu A
            // automatically closes.
            // This line does that by removing 'selected' from every dropdown menu other than the one you clicked on.
            // It's 'optional' but it definitely feels better if you have it
            $('fieldset.toggle-item').not($(event.currentTarget).parent()).removeClass('selected')
            })

            // The user is probably going to expect that any and all dropdown menus will close if they click outside of them. Here's how to make that happen:

            //This listens for whenever you let go of the mouse
            $(document).mouseup(function(e)
            {
            // make this a variable just to make the next line a little easier to read
            // a 'container' is now any
            var dropdown_menus = $("fieldset.toggle-item");

            // if the target of the click isn't a dropdown menu OR any of the elements inside one of them
            if (!dropdown_menus.is(e.target) && dropdown_menus.has(e.target).length === 0)
            {
            // then it will de-select (thereby closing) all the dropdown menus on the page
            $('fieldset.toggle-item').removeClass('selected')

            }
            });
            })

            </script>


            CSS:



            <style>
            .item {
            width: 33%;
            margin: 2px 1% 2px 1%;
            border: 0;
            }

            .item li {
            list-style: none;
            }

            .scrollable-dropdown-list{
            position: absolute;
            max-height:200px;
            width:33%;
            overflow-y:scroll;
            overflow-x:auto;
            margin: 0;
            padding-left: 1em;
            border-style: solid;
            border-width: thin;
            border-color: grey;
            background-color: white;
            }

            legend {
            margin-bottom: 0;
            font-size: 18px;
            }

            label {
            font-weight: normal;
            margin-left:20px;
            }


            .legend-container {
            cursor: pointer;
            width: 100%;
            display: flex;
            padding: 0;
            margin-bottom: 0px;
            font-size: 21px;
            line-height: inherit;
            color: #333;
            border: 0;
            border-bottom: none;
            }

            fieldset {
            border-width: thin;
            border-color: gray;
            border-style: solid;
            width:50px;
            }

            /* Note that all the browser-specific animation stuff is totally optional, but provides a nice subtle animation for the dropdown effect */

            fieldset ul.scrollable-dropdown-list {
            display: none;
            -webkit-animation: slide-down .3s ease-out;
            -moz-animation: slide-down .3s ease-out;
            }

            fieldset.selected ul.scrollable-dropdown-list {
            display: block;
            -webkit-animation: slide-down .3s ease-out;
            -moz-animation: slide-down .3s ease-out;
            }



            @-webkit-keyframes slide-down {
            0% {
            opacity: 0;
            -webkit-transform: translateY(-10%);
            }
            100% {
            opacity: 1;
            -webkit-transform: translateY(0);
            }
            }

            @-moz-keyframes slide-down {
            0% {
            opacity: 0;
            -moz-transform: translateY(-10%);
            }
            100% {
            opacity: 1;
            -moz-transform: translateY(0);
            }
            }
            </style>





            share|improve this answer




























              0














              I see that you asked this four years ago so I doubt you are still looking for an answer, but I might as well provide in case someone else finds it!



              Basically you want to make a div with an unordered list inside of it, where each item in that list contains a checkbox input.



              Then, you use jQuery so that when you click on the div, it gets assigned the 'selected' class in its html.



              Then you make your CSS so that the dropdown menu itself only shows up when it has the 'selected' class.



              The JSFiddle is here (minus the django templating, obviously):
              https://jsfiddle.net/lymanjohnson/2L71nhko/15/



              And code is below:



              HTML (django template):



                  <fieldset class="item toggle-item">
              <div class="legend-container">
              <legend>Choices</legend>
              </div>
              <ul class="scrollable-dropdown-list">
              {% for choice in choices %}
              <li>
              <input type="checkbox" class="custom-control-input filter" id="choice_{{forloop.counter}}" name="choice" value="{{choice}}">
              <label for="choice_{{forloop.counter}}"class="custom-control-label">{{choice}}</label>
              </li>
              {% endfor %}
              </ul>
              </fieldset>


              JQUERY:



                  <script>


              $(document).ready(function() {
              // Listener for when you click on the dropdown menu
              $('fieldset.toggle-item > .legend-container').on('click', (event) => {
              // Adds or removes the 'selected' attribute on the dropdown menu you clicked
              $(event.currentTarget).parent().toggleClass('selected')
              // If you have multiple dropdown menus you may want it so that when you open Menu B, Menu A
              // automatically closes.
              // This line does that by removing 'selected' from every dropdown menu other than the one you clicked on.
              // It's 'optional' but it definitely feels better if you have it
              $('fieldset.toggle-item').not($(event.currentTarget).parent()).removeClass('selected')
              })

              // The user is probably going to expect that any and all dropdown menus will close if they click outside of them. Here's how to make that happen:

              //This listens for whenever you let go of the mouse
              $(document).mouseup(function(e)
              {
              // make this a variable just to make the next line a little easier to read
              // a 'container' is now any
              var dropdown_menus = $("fieldset.toggle-item");

              // if the target of the click isn't a dropdown menu OR any of the elements inside one of them
              if (!dropdown_menus.is(e.target) && dropdown_menus.has(e.target).length === 0)
              {
              // then it will de-select (thereby closing) all the dropdown menus on the page
              $('fieldset.toggle-item').removeClass('selected')

              }
              });
              })

              </script>


              CSS:



              <style>
              .item {
              width: 33%;
              margin: 2px 1% 2px 1%;
              border: 0;
              }

              .item li {
              list-style: none;
              }

              .scrollable-dropdown-list{
              position: absolute;
              max-height:200px;
              width:33%;
              overflow-y:scroll;
              overflow-x:auto;
              margin: 0;
              padding-left: 1em;
              border-style: solid;
              border-width: thin;
              border-color: grey;
              background-color: white;
              }

              legend {
              margin-bottom: 0;
              font-size: 18px;
              }

              label {
              font-weight: normal;
              margin-left:20px;
              }


              .legend-container {
              cursor: pointer;
              width: 100%;
              display: flex;
              padding: 0;
              margin-bottom: 0px;
              font-size: 21px;
              line-height: inherit;
              color: #333;
              border: 0;
              border-bottom: none;
              }

              fieldset {
              border-width: thin;
              border-color: gray;
              border-style: solid;
              width:50px;
              }

              /* Note that all the browser-specific animation stuff is totally optional, but provides a nice subtle animation for the dropdown effect */

              fieldset ul.scrollable-dropdown-list {
              display: none;
              -webkit-animation: slide-down .3s ease-out;
              -moz-animation: slide-down .3s ease-out;
              }

              fieldset.selected ul.scrollable-dropdown-list {
              display: block;
              -webkit-animation: slide-down .3s ease-out;
              -moz-animation: slide-down .3s ease-out;
              }



              @-webkit-keyframes slide-down {
              0% {
              opacity: 0;
              -webkit-transform: translateY(-10%);
              }
              100% {
              opacity: 1;
              -webkit-transform: translateY(0);
              }
              }

              @-moz-keyframes slide-down {
              0% {
              opacity: 0;
              -moz-transform: translateY(-10%);
              }
              100% {
              opacity: 1;
              -moz-transform: translateY(0);
              }
              }
              </style>





              share|improve this answer


























                0












                0








                0







                I see that you asked this four years ago so I doubt you are still looking for an answer, but I might as well provide in case someone else finds it!



                Basically you want to make a div with an unordered list inside of it, where each item in that list contains a checkbox input.



                Then, you use jQuery so that when you click on the div, it gets assigned the 'selected' class in its html.



                Then you make your CSS so that the dropdown menu itself only shows up when it has the 'selected' class.



                The JSFiddle is here (minus the django templating, obviously):
                https://jsfiddle.net/lymanjohnson/2L71nhko/15/



                And code is below:



                HTML (django template):



                    <fieldset class="item toggle-item">
                <div class="legend-container">
                <legend>Choices</legend>
                </div>
                <ul class="scrollable-dropdown-list">
                {% for choice in choices %}
                <li>
                <input type="checkbox" class="custom-control-input filter" id="choice_{{forloop.counter}}" name="choice" value="{{choice}}">
                <label for="choice_{{forloop.counter}}"class="custom-control-label">{{choice}}</label>
                </li>
                {% endfor %}
                </ul>
                </fieldset>


                JQUERY:



                    <script>


                $(document).ready(function() {
                // Listener for when you click on the dropdown menu
                $('fieldset.toggle-item > .legend-container').on('click', (event) => {
                // Adds or removes the 'selected' attribute on the dropdown menu you clicked
                $(event.currentTarget).parent().toggleClass('selected')
                // If you have multiple dropdown menus you may want it so that when you open Menu B, Menu A
                // automatically closes.
                // This line does that by removing 'selected' from every dropdown menu other than the one you clicked on.
                // It's 'optional' but it definitely feels better if you have it
                $('fieldset.toggle-item').not($(event.currentTarget).parent()).removeClass('selected')
                })

                // The user is probably going to expect that any and all dropdown menus will close if they click outside of them. Here's how to make that happen:

                //This listens for whenever you let go of the mouse
                $(document).mouseup(function(e)
                {
                // make this a variable just to make the next line a little easier to read
                // a 'container' is now any
                var dropdown_menus = $("fieldset.toggle-item");

                // if the target of the click isn't a dropdown menu OR any of the elements inside one of them
                if (!dropdown_menus.is(e.target) && dropdown_menus.has(e.target).length === 0)
                {
                // then it will de-select (thereby closing) all the dropdown menus on the page
                $('fieldset.toggle-item').removeClass('selected')

                }
                });
                })

                </script>


                CSS:



                <style>
                .item {
                width: 33%;
                margin: 2px 1% 2px 1%;
                border: 0;
                }

                .item li {
                list-style: none;
                }

                .scrollable-dropdown-list{
                position: absolute;
                max-height:200px;
                width:33%;
                overflow-y:scroll;
                overflow-x:auto;
                margin: 0;
                padding-left: 1em;
                border-style: solid;
                border-width: thin;
                border-color: grey;
                background-color: white;
                }

                legend {
                margin-bottom: 0;
                font-size: 18px;
                }

                label {
                font-weight: normal;
                margin-left:20px;
                }


                .legend-container {
                cursor: pointer;
                width: 100%;
                display: flex;
                padding: 0;
                margin-bottom: 0px;
                font-size: 21px;
                line-height: inherit;
                color: #333;
                border: 0;
                border-bottom: none;
                }

                fieldset {
                border-width: thin;
                border-color: gray;
                border-style: solid;
                width:50px;
                }

                /* Note that all the browser-specific animation stuff is totally optional, but provides a nice subtle animation for the dropdown effect */

                fieldset ul.scrollable-dropdown-list {
                display: none;
                -webkit-animation: slide-down .3s ease-out;
                -moz-animation: slide-down .3s ease-out;
                }

                fieldset.selected ul.scrollable-dropdown-list {
                display: block;
                -webkit-animation: slide-down .3s ease-out;
                -moz-animation: slide-down .3s ease-out;
                }



                @-webkit-keyframes slide-down {
                0% {
                opacity: 0;
                -webkit-transform: translateY(-10%);
                }
                100% {
                opacity: 1;
                -webkit-transform: translateY(0);
                }
                }

                @-moz-keyframes slide-down {
                0% {
                opacity: 0;
                -moz-transform: translateY(-10%);
                }
                100% {
                opacity: 1;
                -moz-transform: translateY(0);
                }
                }
                </style>





                share|improve this answer













                I see that you asked this four years ago so I doubt you are still looking for an answer, but I might as well provide in case someone else finds it!



                Basically you want to make a div with an unordered list inside of it, where each item in that list contains a checkbox input.



                Then, you use jQuery so that when you click on the div, it gets assigned the 'selected' class in its html.



                Then you make your CSS so that the dropdown menu itself only shows up when it has the 'selected' class.



                The JSFiddle is here (minus the django templating, obviously):
                https://jsfiddle.net/lymanjohnson/2L71nhko/15/



                And code is below:



                HTML (django template):



                    <fieldset class="item toggle-item">
                <div class="legend-container">
                <legend>Choices</legend>
                </div>
                <ul class="scrollable-dropdown-list">
                {% for choice in choices %}
                <li>
                <input type="checkbox" class="custom-control-input filter" id="choice_{{forloop.counter}}" name="choice" value="{{choice}}">
                <label for="choice_{{forloop.counter}}"class="custom-control-label">{{choice}}</label>
                </li>
                {% endfor %}
                </ul>
                </fieldset>


                JQUERY:



                    <script>


                $(document).ready(function() {
                // Listener for when you click on the dropdown menu
                $('fieldset.toggle-item > .legend-container').on('click', (event) => {
                // Adds or removes the 'selected' attribute on the dropdown menu you clicked
                $(event.currentTarget).parent().toggleClass('selected')
                // If you have multiple dropdown menus you may want it so that when you open Menu B, Menu A
                // automatically closes.
                // This line does that by removing 'selected' from every dropdown menu other than the one you clicked on.
                // It's 'optional' but it definitely feels better if you have it
                $('fieldset.toggle-item').not($(event.currentTarget).parent()).removeClass('selected')
                })

                // The user is probably going to expect that any and all dropdown menus will close if they click outside of them. Here's how to make that happen:

                //This listens for whenever you let go of the mouse
                $(document).mouseup(function(e)
                {
                // make this a variable just to make the next line a little easier to read
                // a 'container' is now any
                var dropdown_menus = $("fieldset.toggle-item");

                // if the target of the click isn't a dropdown menu OR any of the elements inside one of them
                if (!dropdown_menus.is(e.target) && dropdown_menus.has(e.target).length === 0)
                {
                // then it will de-select (thereby closing) all the dropdown menus on the page
                $('fieldset.toggle-item').removeClass('selected')

                }
                });
                })

                </script>


                CSS:



                <style>
                .item {
                width: 33%;
                margin: 2px 1% 2px 1%;
                border: 0;
                }

                .item li {
                list-style: none;
                }

                .scrollable-dropdown-list{
                position: absolute;
                max-height:200px;
                width:33%;
                overflow-y:scroll;
                overflow-x:auto;
                margin: 0;
                padding-left: 1em;
                border-style: solid;
                border-width: thin;
                border-color: grey;
                background-color: white;
                }

                legend {
                margin-bottom: 0;
                font-size: 18px;
                }

                label {
                font-weight: normal;
                margin-left:20px;
                }


                .legend-container {
                cursor: pointer;
                width: 100%;
                display: flex;
                padding: 0;
                margin-bottom: 0px;
                font-size: 21px;
                line-height: inherit;
                color: #333;
                border: 0;
                border-bottom: none;
                }

                fieldset {
                border-width: thin;
                border-color: gray;
                border-style: solid;
                width:50px;
                }

                /* Note that all the browser-specific animation stuff is totally optional, but provides a nice subtle animation for the dropdown effect */

                fieldset ul.scrollable-dropdown-list {
                display: none;
                -webkit-animation: slide-down .3s ease-out;
                -moz-animation: slide-down .3s ease-out;
                }

                fieldset.selected ul.scrollable-dropdown-list {
                display: block;
                -webkit-animation: slide-down .3s ease-out;
                -moz-animation: slide-down .3s ease-out;
                }



                @-webkit-keyframes slide-down {
                0% {
                opacity: 0;
                -webkit-transform: translateY(-10%);
                }
                100% {
                opacity: 1;
                -webkit-transform: translateY(0);
                }
                }

                @-moz-keyframes slide-down {
                0% {
                opacity: 0;
                -moz-transform: translateY(-10%);
                }
                100% {
                opacity: 1;
                -moz-transform: translateY(0);
                }
                }
                </style>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 20:49









                Lyman JohnsonLyman Johnson

                73




                73






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24031461%2fdrop-down-with-checkboxes-in-django-form%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks