Search code examples
jquerydrupal-7

jQuery if statement syntax


I am trying to get a jQuery function to work on my Drupal 7 site, but I am having a difficult time to make it work.

This example works:

(function ($) {  

   Drupal.behaviors.theme = {

   attach: function (context, settings) {            

   $("#block-menu-3").css("background-color", "red");

   }

};})(jQuery);

But I am trying to do some a little more advanced stuff. For example: I want to apply black color to div B, only if div A is green.

So if I write

(function ($) {  

  Drupal.behaviors.theme = {

  attach: function (context, settings) {            

     $("div A").each(function(i){
       var color = $(this).css("background-color");
       if (color == "background-color:green;")
       $("div B").css("background-color", "");
       $("div B").css("background-color", "black");
     });

     }

 };})(jQuery);

Well it "works"....but div A could be any other color than green, and div B would become black under any circumstances....Im looking for the right way to wright the if statement....

So this does not work.....any help much obliged!


Solution

  • Got it working!

    $("#block-menu-block-23").each(function(i){
       var color = $(this).css("background-color");
       if (color == 'rgb(0, 0, 255)' || color == 'blue'){
       alert('Hello jQuery');
       $("#block-block-15").css("background-color", "");
       $("#block-block-15").css("background-color", "red");
     }});
    

    It was the parentesis after the if statement which was wrong. So I did:

    (color == 'rgb(0, 0, 255)' || color == 'blue')

    Instead of:

    (color != "background-color:blue;")

    But thank you guys for the feedback!