7

I'm trying to display a div onclick of a btn by changing the style property of the div. But I can't read the display property of that div. I read somewhere that the code doesn't work because the script tries to get the value before the div has loaded so the script should be triggered after window.onload. How do I make the script work after window has loaded but only when the button has been clicked ?

<!DOCTYPE html>
<html>
<head>
<style type="text/css"> 
#hidden-div{
display: none;
}
</style>
<script type="text/javascript">
function showMe() {
   var foo = document.getElementById('hidden-div');
   alert(foo.style.display); //this gives a blank alert

   if(foo.style.display == ''){
    foo.style.display = 'block';
   }
   else {
    foo.style.display == 'none';
   } 
}
</script>
</head>
<body>
  <input type="button" id="btn-me" onclick="showMe()">+ clicking here should display the checkboxes</input><br>

  <div id="hidden-div">

  <input type="checkbox" id="check-1"> Check 1</input>
  <br>
  <input type="checkbox" id="check-2"> Check 2</input>
  <br>
  <input type="checkbox" id="check-3"> Check 3</input>

  </div>
</body>
</html>
7
  • There is no</input> tag. Inputs are self-closing Commented Sep 2, 2016 at 19:43
  • 4
    You do understand that == is for comparison and = is for assignment Commented Sep 2, 2016 at 19:44
  • </input> was included by sublime. Didn't notice. Commented Sep 2, 2016 at 19:45
  • @AndrewL. Yes I do know that. Kindly explain how is that relevant here ? Commented Sep 2, 2016 at 19:47
  • You did foo.style.display == 'none';. You attempted to assign, but compared :) Commented Sep 2, 2016 at 19:49

2 Answers 2

10

Something to note: == is for comparison, = is for assignment. Also, there are no closing input tags. Here's the finished snippet:

function showMe() {
   var foo = document.getElementById('hidden-div');

   if(foo.style.display == '' || foo.style.display == 'none'){
        foo.style.display = 'block';
   }
   else {
        foo.style.display = 'none';
   }
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input type="button" id="btn-me" onclick="showMe()">+ clicking here should display the checkboxes</input><br>

  <div id="hidden-div" style="display:none;">

  <input type="checkbox" id="check-1"> Check 1
  <br>
  <input type="checkbox" id="check-2"> Check 2
  <br>
  <input type="checkbox" id="check-3"> Check 3

  </div>
</body>
</html>

I also changed some logic. If the display is none, and the button is clicked, toggle the visibility. If display is not none, then make it none.

The thing is element.style.<style> is only accessible if the style is set inline.

Sign up to request clarification or add additional context in comments.

7 Comments

You could note that it'd also be better to get the element styles by using getComputedStyle, which returns the active styles in a CSSStyleDeclaration object.
Of course, but for demonstration purposes, element.style.<style> will be fine @nicematt
Thanks. Your code works. But if you notice I have set display = 'none' in the style tag. Can you please tell me why doesn't the script read from the internal style tag ?
@Etherealm It's not a tag, but a property. You were comparing the value of HTMLElement().style.display
The OP set the style via a style block and shouldn't need to inline the CSS.
|
1

foo.style.display only works for styling that has been set inline, not for stylehseets or blocks. For that you would use getComputedStyle:

function showMe() {
  var elem = document.getElementById("hidden-div");
  var foo = window.getComputedStyle(elem, null);
  if (foo.getPropertyValue("display") == 'none') {
    elem.style.display = 'block';
  } else {
    elem.style.display = 'none';
  }
}
#hidden-div {
  display: none;
}
<input type="button" id="btn-me" onclick="showMe()">+ clicking here should display the checkboxes
<br>
<div id="hidden-div">
  <input type="checkbox" id="check-1"> Check 1
  <br>
  <input type="checkbox" id="check-2"> Check 2
  <br>
  <input type="checkbox" id="check-3"> Check 3
</div>

1 Comment

@nicematt Aside from the incorrect == actually it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.