Skip to content
Advertisement

Change html CSS style property with jquery

How do I implement this JavaScript block with jQuery?

var x = document.getElementById("name");
if (x.style.backgroundColor == "black"){
//do this
}

I get stuck at this point:

if ($("#name").css() == ???){
//Do this
});

Advertisement

Answer

This should work!

var color = $("#name").css("background-color");

if(color == "black"){
    //do this
}

OR

if you would like it in one line:

if($("#name").css("background-color") == "black"){
  //do this
}
Advertisement