I create a website that is not using jquery library just plain javascript.
Most of the users have low internet bandwidth and I try to keep it as clean as possible.
I'm using just few functions and that's why I decided not to use jquery.
I have created this collapsing menu that opens and close the container when user clicks on it. I want also to close it when the user clicks outside of it. I am not so experienced with javascript and if someone can give some idea I would really appreciate it.
Thank you
function myFunction() {
var x = document.getElementById("Demo");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<div class="w3-container">
<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>
<div class="w3-dropdown-click">
<button onclick="myFunction()" class="w3-button w3-black">Click Me!</button>
<div id="Demo" class="w3-dropdown-content w3-bar-block w3-border">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#" class="w3-bar-item w3-button">Link 3</a>
</div>
</div>
</div>