Problem with display condition in Js

Asked

Viewed 119 times

5

I’m trying to make parole with display: block; and display: none; but JS is not bringing the value of my element’s css display #slide.

What’s the matter?

var slider = document.getElementById('slide');
if(slider.style.display == 'block'){
   alert('teste');
}
  • You can add the/a snippet of html?

1 answer

5

A fairly probable cause is that the property display is actually blank. A simple solution is to reverse the logic:

var slider = document.getElementById('slide');
if(slider.style.display !== 'none'){
   alert('visível');
} else {
   alert('oculto');    
}
  • This behavior is kind of unexpected for me: fiddle. I got something wrong?

  • 1

    @Guilhermebernal Values will only be visible if they are set as element properties (eg: el.style.display). When you assign via CSS, you need to get the computed value, with getComputedStyle + polyfill if necessary. http://jsfiddle.net/7LMfH/2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.