function hd() {
document.getElementById("Y").style.display = "none";
}
body {
display:grid;
place-content: center;
height:100vh;
overflow:hidden;
}
<div id="Y">i will be hidden</div>
<br>
<button onclick="hd();">click me</button>
The important thing is to store the function so it works even user refreshes the page I tried to use cookies but it did not work by just calling the function
You can set a flag with localStorage and then check if this flag has already been set and call the function again at startup.
function hd () { document.getElementById("Y").style.display = "none"; localStorage.setItem("hidden", "yes"); } if (localStorage.getItem("hidden")) { hd(); }