admin管理员组

文章数量:1024615

I want that the text ist hidden in the beginning and after clicking the button it is displayed. I would be really greatfull if someone would find the mistake in my code.

function F1()
{
  var x = document.getElementById("step1DIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
<!DOCTYPE html>
<html>

<body>

  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV">
    <p> text </p>
  </div>

</body>

</html>

I want that the text ist hidden in the beginning and after clicking the button it is displayed. I would be really greatfull if someone would find the mistake in my code.

function F1()
{
  var x = document.getElementById("step1DIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
<!DOCTYPE html>
<html>

<body>

  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV">
    <p> text </p>
  </div>

</body>

</html>

Share Improve this question edited Mar 11, 2020 at 23:18 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Mar 11, 2020 at 23:15 Save PainSave Pain 2572 silver badges11 bronze badges 1
  • style.display is not available until it's set. I remend having .hide{ display:none; } in CSS then have Element.classList.add('hide') and Element.classList.remove('hide') or Element.classList.toggle('hide'). Unless you want to do a lot of typing, I generally remend using CSS classes to style. – StackSlave Commented Mar 11, 2020 at 23:18
Add a ment  | 

4 Answers 4

Reset to default 2

You need to give it an initial style that hides it in the HTML.

function F1()
{
  var x = document.getElementById("step1DIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV" style="display: none;">
    <p> text </p>
  </div>

But inline styles are poor design, it's better to use a class with CSS.

function F1()
{
  var x = document.getElementById("step1DIV");
  x.classList.toggle("hidden");
}
.hidden {
  display: none;
}
<button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV" class="hidden">
    <p> text </p>
  </div>

I just defined it as 'none' to begin with as such:

<div id="step1DIV" style="display: none"> 

Try to initially set the display of your step1 DIV to none. Either using inline styling or CSS.

You can also try to run your function on page load.

You want to toggle the hidden attribute defined in the HTML Standard.

function F1 () {
  document.getElementById("a").toggleAttribute("hidden");
}
<!DOCTYPE html>
<html>

<body>

  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id=a>
    <p> text </p>
  </div>

</body>

</html>

I want that the text ist hidden in the beginning and after clicking the button it is displayed. I would be really greatfull if someone would find the mistake in my code.

function F1()
{
  var x = document.getElementById("step1DIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
<!DOCTYPE html>
<html>

<body>

  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV">
    <p> text </p>
  </div>

</body>

</html>

I want that the text ist hidden in the beginning and after clicking the button it is displayed. I would be really greatfull if someone would find the mistake in my code.

function F1()
{
  var x = document.getElementById("step1DIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
<!DOCTYPE html>
<html>

<body>

  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV">
    <p> text </p>
  </div>

</body>

</html>

Share Improve this question edited Mar 11, 2020 at 23:18 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Mar 11, 2020 at 23:15 Save PainSave Pain 2572 silver badges11 bronze badges 1
  • style.display is not available until it's set. I remend having .hide{ display:none; } in CSS then have Element.classList.add('hide') and Element.classList.remove('hide') or Element.classList.toggle('hide'). Unless you want to do a lot of typing, I generally remend using CSS classes to style. – StackSlave Commented Mar 11, 2020 at 23:18
Add a ment  | 

4 Answers 4

Reset to default 2

You need to give it an initial style that hides it in the HTML.

function F1()
{
  var x = document.getElementById("step1DIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV" style="display: none;">
    <p> text </p>
  </div>

But inline styles are poor design, it's better to use a class with CSS.

function F1()
{
  var x = document.getElementById("step1DIV");
  x.classList.toggle("hidden");
}
.hidden {
  display: none;
}
<button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV" class="hidden">
    <p> text </p>
  </div>

I just defined it as 'none' to begin with as such:

<div id="step1DIV" style="display: none"> 

Try to initially set the display of your step1 DIV to none. Either using inline styling or CSS.

You can also try to run your function on page load.

You want to toggle the hidden attribute defined in the HTML Standard.

function F1 () {
  document.getElementById("a").toggleAttribute("hidden");
}
<!DOCTYPE html>
<html>

<body>

  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id=a>
    <p> text </p>
  </div>

</body>

</html>

本文标签: htmlHow to hide Elements in JavascriptStack Overflow