ShroomBandit
ShroomBandit

Reputation: 441

Jquery If statement to check margin left of div to apply opacity to a separate id

I've been trying to tweak things a bit to get this to work and so far I haven't been able to use jquery to check a div's left margin and on the condition it is in the left: 0px position, to output whatever action, in my case to apply opacity to a separate div. Here's my jfiddle. In the jfiddle I want to check the gray div "trim", and on the condition it has marginLeft: 0px, to apply the opacity to the div "l1". Let me know if I need to explain this more.

Upvotes: 0

Views: 3982

Answers (3)

Ram
Ram

Reputation: 144689

jQuery object doesn't have marginLeft property, you should use css method, also note that = is a assignment operator which valuates to true, you should use == or === comparison operators instead.

$(document).ready(function(){
    if ( $('#trim').css('marginLeft') == '0px' ){
        $('#l1').animate({opacity:".5"});
    } else {
        $('#l1').animate({opacity:"1"});
    }
}); // You are missing `)` here

http://jsfiddle.net/5djZ4/

Upvotes: 3

Brad
Brad

Reputation: 603

Well, in the fiddle you're doing

$('#trim').marginLeft

How about this instead:

$('#trim').attr('marginLeft') 

Upvotes: -1

Horen
Horen

Reputation: 11382

http://jsfiddle.net/K8CMp/7/

Use the jQuery .css() method:

$(document).ready(function(){
    if($('#trim').css("margin-left") == "0px"){
        $('#l1').animate({opacity:".5"});
    }else{
        $('#l1').animate({opacity:"1"});
    }
});

Upvotes: 1

Related Questions