Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Why is this function working on second click only

let btn = document.getElementById('btn');
let hello = document.getElementById('hello');

function test() {
  if (hello.style.display !== 'none') {
    hello.style.display = 'none';
  } else {
    hello.style.display = 'block';
  }
}

btn.addEventListener('click', test)
<div id='hello'>
  hello
</div>

<div id='btn'>
  button
</div>
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript What is the Comment (native) function 
Javascript :: Javascript shows me TypeError saying my variable is undefined 
Javascript :: filter json array based on multiple arguments including lists 
Javascript :: how to create your own event emitter in javascript 
Javascript :: add flag persmison to write file nodejs 
Javascript :: AngularJS Graphs & Charts - Mix of solid & dotted 
Javascript :: angularjs Both outer and inner divs have ng-click and when I click on the inner div, both ng-clicks execute. How to prevent that 
Javascript :: angularjs Prevent from getting rendered 
Javascript :: tabbarbadge style react native 
Javascript :: angularjs How to sort a specific value in a map 
Javascript :: How to get one items from my Firebase realtime Database with Angular Ionic 
Javascript :: how to set a condition so that between the first and second mouse clicks there was a delay not 500mls 
Javascript :: How do I change this React Navigation v5 code to v6 
Javascript :: how to make colspan of table footer flexible with javascript/jQuery 
Javascript :: how to add link during filter and mapping in javascript 
Javascript :: how to build a nested, dynamic JSON in Go 
Javascript :: javascript polyIntersect 
Javascript :: react native mirror text 
Javascript :: Simple Mustache.js 
Javascript :: how can do i open the select tag using keyboard event using javascript site:stackoverflow.com 
Javascript :: phaser remove collider on stop 
Javascript :: react native text input allow only numbers 
Javascript :: Self Invoking Function Tip 
Javascript :: adding amplify in index.js react native 
Javascript :: Assigning A Property The Return Value Of A Function In Class 
Javascript :: nested object data 
Javascript :: withrouter in react-router v6 
Javascript :: Watch an API for Updates 
Javascript :: Backbone Model Fetch 
Javascript :: merge large arrays 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =