daGUY
daGUY

Reputation: 28793

If statement with two jQuery selectors

I'm trying to get an alert to appear if two conditions are met:

I have this:

if ($("#playerbox").css("display") == "block" && $("#playerinfo").css("display") == "none"){
    alert("test");
}

But the alert never appears. However, testing just one element will work:

if ($("#playerbox").css("display") == "block"){
    alert("test");
}

So I'm assuming I'm not chaining the two selectors together correctly in the if statement. What am I doing wrong?


UPDATE: I should have specified that #playerinfo is inside #playerbox. Does that change anything?

Upvotes: 0

Views: 1483

Answers (3)

AGoodDisplayName
AGoodDisplayName

Reputation: 5653

Are you sure you JQuery actually found the elements you are looking for?

alert($("#playerbox").length);

alert($("#playerinfo").length);

if either of those are coming back as 0, then you aren't really getting those elements. Maybe you are have a typo, or the class name set rather than the id.

Upvotes: 3

Johan
Johan

Reputation: 35223

Use is

For example

 if($("#playerinfo").is(":hidden")) /* :visible for your other selector */
       //do stuff

Upvotes: 0

ori
ori

Reputation: 7847

Why not use :visible and :hidden?

if ($("#playerbox:visible,#playerinfo:hidden").length == 2){
     ...
}

Upvotes: 5

Related Questions