1

I'm making a prank .html page for a friend. The prank was, you would push a button and a phrase would pop up under the button. I figured out a way to hide the phrase and unhide it. The problem is that, when you load into the page, the phrase would already be on the screen. How do I keep it hidden on page load?

function Function() {
  var x = document.getElementById("urmom");
  
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="Function()">Try it</button>

<div id="urmom">
  ur mom
</div>

<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>
  • 4
    Why not just use CSS? `#urmom {display:none;}` – j08691 Dec 09 '21 at 19:47
  • Same as you did with `onclick`, really, though you should be using [event handlers](https://developer.mozilla.org/en-US/docs/Web/Events/Event_handlers) rather than HTML attributes. Or use CSS, which would certainly be easier. – isherwood Dec 09 '21 at 19:49

1 Answers1

3

Set with CSS first as NONE.

function Function() {
  var x = document.getElementById("urmom");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="Function()">Try it</button>

<div id="urmom" style="display:none">
ur mom
</div>

<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>
 

</body>
</html>
flakerimi
  • 2,580
  • 3
  • 29
  • 49