[Solved] jquery if background is red doesn’t recognize error, looks easy but stubborn


Why is this fiddle not working?

Many reasons.

1) CSS property background is not background-color

2) background-color is not red but rgb(255, 0, 0)

Working code:

$(document).ready(function(){
  $(".box").click(function(){

      if ($(".box").css("background-color") == "rgb(255, 0, 0)")
      {
          $(".box").html("Success!");
      }
  });
});

Working Jsfiddle link


Side note: debugging with console.log() is much more convenient than alert()

Side note (2): you are able to debug if statement. In your case, a test like this would have solved the issue.

console.log($(".box").css("background") == "red")

1

solved jquery if background is red doesn’t recognize error, looks easy but stubborn