Dropdown menu with search bar. Problem with closing












0















I am trying to create a dropdown-menu with a filter/search function inside and I cant get it to keep open when i click in the " search field " at the moment it closes the whole list if i click inside.
Is there a good way to have it close when clicking outside the dropdown menu and keep it open when clicking inside the search field ?



This is what I got so far:



<ul>
<div class="navbar">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Varuhus</button>
<div class="dropdown">
<div id="myDropdown" class="dropdown-content">
<input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
<a href="https://www.google.com" target="targetframe2">E-handel</a>
<a href="https://www.google.com" target="targetframe2">Eskilstuna</a>
<a href="https://www.google.com" target="targetframe2">Borås</a>
<a href="https://www.google.com" target="targetframe2">Gävle</a>
<a href="https://www.google.com" target="targetframe2">Göteborg Bäckebol</a>
<a href="https://www.google.com" target="targetframe2">Göteborg Högsbo</a>
<a href="https://www.google.com" target="targetframe2">Göteborg Torpavallen</a>
<a href="https://www.google.com" target="targetframe2">Helsingborg</a>
<a href="https://www.google.com" target="targetframe2">Jönköping</a>
<a href="https://www.google.com" target="targetframe2">Kalmar</a>
<a href="https://www.google.com" target="targetframe2">Kristianstad</a>
<a href="https://www.google.com" target="targetframe2">Linköping</a>
<a href="https://www.google.com" target="targetframe2">Luleå</a>
<a href="https://www.google.com" target="targetframe2">Lund</a>
<a href="https://www.google.com" target="targetframe2">Malmö Svågertorp</a>
<a href="https://www.google.com" target="targetframe2">Norrköping</a>
<a href="https://www.google.com" target="targetframe2">Skövde</a>
<a href="https://www.google.com" target="targetframe2">Stockholm Barkarby</a>
<a href="https://www.google.com" target="targetframe2">Stockholm Gallerian</a>
<a href="https://www.google.com" target="targetframe2">Stockholm Heron City</a>
<a href="https://www.google.com" target="targetframe2">Stockholm Länna</a>
<a href="https://www.google.com" target="targetframe2">Stockholm Nacka</a>
<a href="https://www.google.com" target="targetframe2">Stockholm Täby</a>
<a href="https://www.google.com" target="targetframe2">Sundsvall</a>
<a href="https://www.google.com" target="targetframe2">Umeå</a>
<a href="https://www.google.com" target="targetframe2">Uppsala</a>
<a href="https://www.google.com" target="targetframe2">Västerås</a>
<a href="https://www.google.com" target="targetframe2">Växjö</a>
<a href="https://www.google.com" target="targetframe2">Örebro</a>
</div>
</div>
</div>
<a href="#Aroma1">Back up top</a>
<a href="#news">Nothing here yet</a>
</ul>


<div>
<iframe src="page1.html" name="targetframe2" width="1890" height="930"> allowTransparency="true" scrolling="no" frameborder="0" >
</iframe>
</div>


</iframe>




<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}


</script>

</body>
</html>









share|improve this question





























    0















    I am trying to create a dropdown-menu with a filter/search function inside and I cant get it to keep open when i click in the " search field " at the moment it closes the whole list if i click inside.
    Is there a good way to have it close when clicking outside the dropdown menu and keep it open when clicking inside the search field ?



    This is what I got so far:



    <ul>
    <div class="navbar">
    <div class="dropdown">
    <button onclick="myFunction()" class="dropbtn">Varuhus</button>
    <div class="dropdown">
    <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="https://www.google.com" target="targetframe2">E-handel</a>
    <a href="https://www.google.com" target="targetframe2">Eskilstuna</a>
    <a href="https://www.google.com" target="targetframe2">Borås</a>
    <a href="https://www.google.com" target="targetframe2">Gävle</a>
    <a href="https://www.google.com" target="targetframe2">Göteborg Bäckebol</a>
    <a href="https://www.google.com" target="targetframe2">Göteborg Högsbo</a>
    <a href="https://www.google.com" target="targetframe2">Göteborg Torpavallen</a>
    <a href="https://www.google.com" target="targetframe2">Helsingborg</a>
    <a href="https://www.google.com" target="targetframe2">Jönköping</a>
    <a href="https://www.google.com" target="targetframe2">Kalmar</a>
    <a href="https://www.google.com" target="targetframe2">Kristianstad</a>
    <a href="https://www.google.com" target="targetframe2">Linköping</a>
    <a href="https://www.google.com" target="targetframe2">Luleå</a>
    <a href="https://www.google.com" target="targetframe2">Lund</a>
    <a href="https://www.google.com" target="targetframe2">Malmö Svågertorp</a>
    <a href="https://www.google.com" target="targetframe2">Norrköping</a>
    <a href="https://www.google.com" target="targetframe2">Skövde</a>
    <a href="https://www.google.com" target="targetframe2">Stockholm Barkarby</a>
    <a href="https://www.google.com" target="targetframe2">Stockholm Gallerian</a>
    <a href="https://www.google.com" target="targetframe2">Stockholm Heron City</a>
    <a href="https://www.google.com" target="targetframe2">Stockholm Länna</a>
    <a href="https://www.google.com" target="targetframe2">Stockholm Nacka</a>
    <a href="https://www.google.com" target="targetframe2">Stockholm Täby</a>
    <a href="https://www.google.com" target="targetframe2">Sundsvall</a>
    <a href="https://www.google.com" target="targetframe2">Umeå</a>
    <a href="https://www.google.com" target="targetframe2">Uppsala</a>
    <a href="https://www.google.com" target="targetframe2">Västerås</a>
    <a href="https://www.google.com" target="targetframe2">Växjö</a>
    <a href="https://www.google.com" target="targetframe2">Örebro</a>
    </div>
    </div>
    </div>
    <a href="#Aroma1">Back up top</a>
    <a href="#news">Nothing here yet</a>
    </ul>


    <div>
    <iframe src="page1.html" name="targetframe2" width="1890" height="930"> allowTransparency="true" scrolling="no" frameborder="0" >
    </iframe>
    </div>


    </iframe>




    <script>
    /* When the user clicks on the button,
    toggle between hiding and showing the dropdown content */
    function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
    }

    function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myDropdown");
    a = div.getElementsByTagName("a");
    for (i = 0; i < a.length; i++) {
    if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
    a[i].style.display = "";
    } else {
    a[i].style.display = "none";
    }
    }
    }
    // Close the dropdown if the user clicks outside of it
    window.onclick = function(event) {
    if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    if (openDropdown.classList.contains('show')) {
    openDropdown.classList.remove('show');
    }
    }
    }
    }


    </script>

    </body>
    </html>









    share|improve this question



























      0












      0








      0








      I am trying to create a dropdown-menu with a filter/search function inside and I cant get it to keep open when i click in the " search field " at the moment it closes the whole list if i click inside.
      Is there a good way to have it close when clicking outside the dropdown menu and keep it open when clicking inside the search field ?



      This is what I got so far:



      <ul>
      <div class="navbar">
      <div class="dropdown">
      <button onclick="myFunction()" class="dropbtn">Varuhus</button>
      <div class="dropdown">
      <div id="myDropdown" class="dropdown-content">
      <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
      <a href="https://www.google.com" target="targetframe2">E-handel</a>
      <a href="https://www.google.com" target="targetframe2">Eskilstuna</a>
      <a href="https://www.google.com" target="targetframe2">Borås</a>
      <a href="https://www.google.com" target="targetframe2">Gävle</a>
      <a href="https://www.google.com" target="targetframe2">Göteborg Bäckebol</a>
      <a href="https://www.google.com" target="targetframe2">Göteborg Högsbo</a>
      <a href="https://www.google.com" target="targetframe2">Göteborg Torpavallen</a>
      <a href="https://www.google.com" target="targetframe2">Helsingborg</a>
      <a href="https://www.google.com" target="targetframe2">Jönköping</a>
      <a href="https://www.google.com" target="targetframe2">Kalmar</a>
      <a href="https://www.google.com" target="targetframe2">Kristianstad</a>
      <a href="https://www.google.com" target="targetframe2">Linköping</a>
      <a href="https://www.google.com" target="targetframe2">Luleå</a>
      <a href="https://www.google.com" target="targetframe2">Lund</a>
      <a href="https://www.google.com" target="targetframe2">Malmö Svågertorp</a>
      <a href="https://www.google.com" target="targetframe2">Norrköping</a>
      <a href="https://www.google.com" target="targetframe2">Skövde</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Barkarby</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Gallerian</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Heron City</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Länna</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Nacka</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Täby</a>
      <a href="https://www.google.com" target="targetframe2">Sundsvall</a>
      <a href="https://www.google.com" target="targetframe2">Umeå</a>
      <a href="https://www.google.com" target="targetframe2">Uppsala</a>
      <a href="https://www.google.com" target="targetframe2">Västerås</a>
      <a href="https://www.google.com" target="targetframe2">Växjö</a>
      <a href="https://www.google.com" target="targetframe2">Örebro</a>
      </div>
      </div>
      </div>
      <a href="#Aroma1">Back up top</a>
      <a href="#news">Nothing here yet</a>
      </ul>


      <div>
      <iframe src="page1.html" name="targetframe2" width="1890" height="930"> allowTransparency="true" scrolling="no" frameborder="0" >
      </iframe>
      </div>


      </iframe>




      <script>
      /* When the user clicks on the button,
      toggle between hiding and showing the dropdown content */
      function myFunction() {
      document.getElementById("myDropdown").classList.toggle("show");
      }

      function filterFunction() {
      var input, filter, ul, li, a, i;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      div = document.getElementById("myDropdown");
      a = div.getElementsByTagName("a");
      for (i = 0; i < a.length; i++) {
      if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
      } else {
      a[i].style.display = "none";
      }
      }
      }
      // Close the dropdown if the user clicks outside of it
      window.onclick = function(event) {
      if (!event.target.matches('.dropbtn')) {

      var dropdowns = document.getElementsByClassName("dropdown-content");
      var i;
      for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
      openDropdown.classList.remove('show');
      }
      }
      }
      }


      </script>

      </body>
      </html>









      share|improve this question
















      I am trying to create a dropdown-menu with a filter/search function inside and I cant get it to keep open when i click in the " search field " at the moment it closes the whole list if i click inside.
      Is there a good way to have it close when clicking outside the dropdown menu and keep it open when clicking inside the search field ?



      This is what I got so far:



      <ul>
      <div class="navbar">
      <div class="dropdown">
      <button onclick="myFunction()" class="dropbtn">Varuhus</button>
      <div class="dropdown">
      <div id="myDropdown" class="dropdown-content">
      <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
      <a href="https://www.google.com" target="targetframe2">E-handel</a>
      <a href="https://www.google.com" target="targetframe2">Eskilstuna</a>
      <a href="https://www.google.com" target="targetframe2">Borås</a>
      <a href="https://www.google.com" target="targetframe2">Gävle</a>
      <a href="https://www.google.com" target="targetframe2">Göteborg Bäckebol</a>
      <a href="https://www.google.com" target="targetframe2">Göteborg Högsbo</a>
      <a href="https://www.google.com" target="targetframe2">Göteborg Torpavallen</a>
      <a href="https://www.google.com" target="targetframe2">Helsingborg</a>
      <a href="https://www.google.com" target="targetframe2">Jönköping</a>
      <a href="https://www.google.com" target="targetframe2">Kalmar</a>
      <a href="https://www.google.com" target="targetframe2">Kristianstad</a>
      <a href="https://www.google.com" target="targetframe2">Linköping</a>
      <a href="https://www.google.com" target="targetframe2">Luleå</a>
      <a href="https://www.google.com" target="targetframe2">Lund</a>
      <a href="https://www.google.com" target="targetframe2">Malmö Svågertorp</a>
      <a href="https://www.google.com" target="targetframe2">Norrköping</a>
      <a href="https://www.google.com" target="targetframe2">Skövde</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Barkarby</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Gallerian</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Heron City</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Länna</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Nacka</a>
      <a href="https://www.google.com" target="targetframe2">Stockholm Täby</a>
      <a href="https://www.google.com" target="targetframe2">Sundsvall</a>
      <a href="https://www.google.com" target="targetframe2">Umeå</a>
      <a href="https://www.google.com" target="targetframe2">Uppsala</a>
      <a href="https://www.google.com" target="targetframe2">Västerås</a>
      <a href="https://www.google.com" target="targetframe2">Växjö</a>
      <a href="https://www.google.com" target="targetframe2">Örebro</a>
      </div>
      </div>
      </div>
      <a href="#Aroma1">Back up top</a>
      <a href="#news">Nothing here yet</a>
      </ul>


      <div>
      <iframe src="page1.html" name="targetframe2" width="1890" height="930"> allowTransparency="true" scrolling="no" frameborder="0" >
      </iframe>
      </div>


      </iframe>




      <script>
      /* When the user clicks on the button,
      toggle between hiding and showing the dropdown content */
      function myFunction() {
      document.getElementById("myDropdown").classList.toggle("show");
      }

      function filterFunction() {
      var input, filter, ul, li, a, i;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      div = document.getElementById("myDropdown");
      a = div.getElementsByTagName("a");
      for (i = 0; i < a.length; i++) {
      if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
      } else {
      a[i].style.display = "none";
      }
      }
      }
      // Close the dropdown if the user clicks outside of it
      window.onclick = function(event) {
      if (!event.target.matches('.dropbtn')) {

      var dropdowns = document.getElementsByClassName("dropdown-content");
      var i;
      for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
      openDropdown.classList.remove('show');
      }
      }
      }
      }


      </script>

      </body>
      </html>






      drop-down-menu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 7:58









      Andrei Suvorkov

      4,1674929




      4,1674929










      asked Nov 25 '18 at 18:39









      SimonSimon

      62




      62
























          0






          active

          oldest

          votes











          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%2f53470673%2fdropdown-menu-with-search-bar-problem-with-closing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f53470673%2fdropdown-menu-with-search-bar-problem-with-closing%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

          Futebolista

          Lallio

          Jornalista