1

For this to function I need to click twice on my toggle link. And cannot really see why nor get solution to make it work on the first click. Can somepone explain why like this and what to do? The idea is to show hide a navigation bar (fixed) here is JS

    <script>
      function toggle_visibility(id) {
            var e = document.getElementById(id);
       e.style.display = ((e.style.display!='none') ? 'none' : 'block');
      }
   </script>

And the Html

<div class="nav">
  <a onclick="toggle_visibility('navbar');" style="cursor:pointer;">V</a>
  <div id="navbar" class="hidden">navigation items</div>
 </div>

And some CSS

.hidden {display:none;}

Reason of the .hidden class is to have it hidden in the beggining. i didnt get done othe way. It should be possible though i think. :) thanks in advance

Mik_A
  • 266
  • 5
  • 16

5 Answers5

4

Ok this is why you have to click twice:

When you click for the first time, the default inline CSS will be applied display:none because it is not none it is undefined. Next time you click the test is false, becaue it is none and thus the inline CSS will be applied display:block. The next piece of code will check if there is any style applied first, it will continue your scrip normally if it returns true, else it will apply the display:block (this way it will prevent your first time run to apply display:none).

 function toggle_visibility(id) {
   var e = document.getElementById(id);
   if (e.style.display){
   e.style.display = ((e.style.display!='none') ? 'none' : 'block');
   }
   else {e.style.display='block'}
  }
Michelangelo
  • 5,888
  • 5
  • 31
  • 50
0

Update

How about using classList and then toggle since you are using classes

function toggle_visibility(id) {
  var e = document.getElementById(id);
  e.classList.toggle('hidden');
}

function toggle_visibility(id) {
  var e = document.getElementById(id);
  e.classList.toggle('hidden');
}
.hidden {display:none;}
<div class="nav">
  <a onclick="toggle_visibility('navbar');" style="cursor:pointer;">V</a>
  <div id="navbar" class="hidden">navigation items</div>
 </div>

You should send the string `"navbar"` in `toggle_visibility` method like this `toggle_visibility('navbar');`
Dhiraj
  • 33,140
  • 10
  • 61
  • 78
0

Can you check an object's CSS display with javascript?

if the element's display is being inherited or being specified by a CSS rule, you'll need to get its computed style:

return element.currentStyle ? element.currentStyle.display : getComputedStyle(element, null).display;

greetz

0

First time page loads the element has CSS style but it doesn't have style in JS, so all you have to do is adding display:none in using JS just when page loads. Add this line at the end of the page:

document.getElementById("navbar").style.display="none";
Jafar Akhondali
  • 1,552
  • 1
  • 11
  • 25
0

I found that it was because the "hidden" or "none" css values were declared only in a css file, and not in an inline statement. So in your html, be sure to declare your initial css style e.g. "display:none;" inline, which means to add it to the code in your html file and not to rely on javascript working with a css file. You see the css of display: none must be in the DOM first for javascript to manipulate it. It seems css from a css file is not in the DOM. Correct me if im wrong.

Dane
  • 9,242
  • 5
  • 33
  • 56
ax.falcon
  • 43
  • 1
  • 10