Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery if then statement for css value

I want to check whether a div with a CSS class="x" has height="auto"

If yes I want (with a jQuery script) the css-class="a" removed from all elements with the css-class="y"

If not the script does not have to do anything. Thanks

like image 783
andrej Avatar asked Jul 21 '09 17:07

andrej


People also ask

How to add CSS in if condition?

No, We can not use if-else conditions in CSS as CSS doesn't support logics. But we can use some alternatives to if-else which are discussed below: Method 1: In this method, we will use classes in HTML file to achieve this. We will define different class names according to the conditions which we want to apply in CSS.

Can you use an if statement in CSS?

They're not programming languages, there is no built logic. No if/else, variables, functions, methods, etc. You cannot use if statements in CSS nor can you in HTML. They're not programming languages, there is no built logic.

How can check CSS property value in jQuery?

Get a CSS Property Value Here's the basic syntax: $(selector). css("propertyName"); The following example will retrieve and display the computed value of the CSS background-color property of a <div> element, when it is clicked.


2 Answers

if ($('div.x').css('height') === 'auto') {
    $('.y').removeClass('a');
}
like image 156
Ken Browning Avatar answered Nov 02 '22 07:11

Ken Browning


$(document).ready(function(){
  if ($('div.x').css('height') === 'auto') {
    $('.y').removeClass('a');
  }   

});

You may need to do that within a each() call

like image 30
AutomatedTester Avatar answered Nov 02 '22 07:11

AutomatedTester