Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check if element has a specific style property (display)

I need to have a if/else statement inside a function. How do you check if an element (e.g. #cadrage) has a display style property? This is what I have found around the net and yet, it is not working..

if( $('#cadrage').attr('style').display == 'block' ) {
      // do something
} else {
      // do something
}
like image 501
user3038607 Avatar asked Jul 05 '14 23:07

user3038607


2 Answers

The jQuery .css() function seems to be what you want.

if( $('#cadrage').css('display') == 'block' ) {
   console.log('It equal block');
} else {
   console.log('It did not equal block');
}

http://jsfiddle.net/SamMonk/FtP6W/

like image 59
SamMonk Avatar answered Sep 28 '22 06:09

SamMonk


Your code doesn't work because style property only contains inline styles, not those coming from a stylesheet.

To get the computed style, you can use css method:

$('#cadrage').css('display') == 'block'
like image 31
Oriol Avatar answered Sep 28 '22 06:09

Oriol