0

How to check if a div has some specific css?

Here is what I have tried without success:

	$("#red").click(function() {
		
		if( $("#red").css("top") == "10") {
			$("#red").css("top", "100");
		} else {
			$("#red").css("top", "10");
		}
		
		/*
		if( $("#red").css("background-color") == "red") {
			$("#red").css("background-color", "grey");
		} else {
			$("#red").css("background-color", "red");
		}
		*/

    });
#red{
	position: absolute;
	top:10px; left:10px;
	width:100px; height:100px;
	background-color:red;
	cursor:pointer;	
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="red"></div>

2
  • 1
    A simple check of what css('top') returns in your console would show you why. .... console.log($(this).css('top')) Commented Apr 28, 2018 at 14:05
  • When you ask question, you have to mention what kind of debugging you have already done, seems like you haven't debugged at all Commented Apr 28, 2018 at 14:20

1 Answer 1

2

You forgot to check for pixels.
For example:

css("top") == "10px"

Instead of

css("top") == "10"

As for the background-color, .css returns it as rgb.

$("#red").click(function() {
  var redColor = "rgb(255, 0, 0)";
  var greyColor = "rgb(128, 128, 128)";

  if ($(this).css("top") == "10px") {
    $(this).css("top", "100px");
  } else {
    $(this).css("top", "10px");
  }

  if ($(this).css("background-color") == redColor) {
    $(this).css("background-color", greyColor);
  } else {
    $(this).css("background-color", redColor);
  }
});
#red {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 100px;
  height: 100px;
  background-color: red;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="red"></div>

Edit
I would take a different approach and use css classes instead of inline styling.

Here is a very simple example for this use case:

$("#box").click(function() {
  if ($(this).hasClass('up')) {
    $(this).removeClass('up');
    $(this).addClass('down');
  } else {
    $(this).removeClass('down');
    $(this).addClass('up');
  }
});
#box {
  position: absolute;
  width: 100px;
  height: 100px;
  cursor: pointer;
}

.up {
  top: 10px;
  left: 10px;
  background-color: red;
}

.down {
  top: 100px;
  background-color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box" class="up"></div>

Sign up to request clarification or add additional context in comments.

4 Comments

Also note $(this) is $('#red') inside the handler....no need to search dom for it again
You are right. Now it works. How about the option to check the color (in my example in comments)
@nrc don't be greedy, you already have your answer, use it to figure out your other problem, do some debugging instead of asking others to write/fix your code for you
the color returned as rgb, a simple console.log can verify that. with that said, i would do it with css classes instead of inline styling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.