'Dropdown menu remain open after click and refresh

I have a problem on how to make my dropdown remain open after clicking and avoid closing after refresh.

Here's what I've done.

<button class="dropdown_btn">
   <span>Sample</span>
</button>
<div class="dropdown-container">
   <a href="sample.php">Sample 1</a>
   <a href="sample.php">Sample 2</a>
</div>
<br>
<button class="dropdown_btn">
  <span>Sample</span>
</button>
<div class="dropdown-container">
   <a href="sample.php">Sample 1</a>
   <a href="sample.php">Sample 2</a>
</div>

Jsfiddle Link: https://jsfiddle.net/Mandy201/7w8t2muc/



Solution 1:[1]

You can use the localStorage with a simply flag. On open the flag become true, on close the flag become false. For the refresh you need to test your var contains in localStorage. If is true, display the menu, if is false do not display the menu

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P