gidzior
gidzior

Reputation: 1805

if statement checking that div hasClass

hi i want to write if statement which is checking if div dont hasClass and if dont has it script should add it using toggleClass (i'm useing jquery ui), i wrote script above whitch is working almost fine but the first (this) should point to (.apla) that has not "min" class and the second (this) should point (.apla) with clicked (.arrow)

point of this is to check if any (.apla) is open and if any (.apla) is open script should close it using toggleClass (with animation), and after this script should open clicked (.apla) also using toggleClass, could u help me pls

fiddle - http://jsfiddle.net/eNqew/

JS

$('.arrow').click(function(){
    if (!$(".apla").hasClass("min")) {
    $(this).toggleClass("min", "slow");
    } else {
    $(this).parent().parent().toggleClass("min", "slow");
    }
});

UPDATE JS works almost

$('.arrow').click(function(){
        if (!$("#apla01").hasClass("min")) {
            $("#apla01").toggleClass("min", "slow");
        }
        if (!$("#apla02").hasClass("min")) {
            $("#apla02").toggleClass("min", "slow");
        }
        if (!$("#apla03").hasClass("min")) {
            $("#apla03").toggleClass("min", "slow");
        }
        else {
            $(this).parent().parent().toggleClass("min", "slow");
        }
    });

HTML

<div id="apla01" class="apla min">
    <div class="apla-wraper">
        <div class="arrow"></div>
    </div>      
</div>
<div id="apla02" class="apla min">
    <div class="apla-wraper">
        <div class="arrow"></div>
    </div>      
</div>
<div id="apla03" class="apla min">
    <div class="apla-wraper">
        <div class="arrow"></div>
    </div>      
</div>

CSS

#apla01.apla.min {
    z-index: 5;
    left: -800px;
}
#apla02.apla.min {
    z-index: 4;
    left: -760px;
}
#apla03.apla.min  {
    z-index: 3;
    left: -720px;
}
.apla {
    width: 838px;
    height: 634px;
    position: absolute;
    top: 20px;
    left: 0;
    background: url(../img/bgApla.png) no-repeat -5px center;
}
.apla.min {
}
.apla-wraper {
    position: relative;
    width: 100%;
    height: 100%;
}
.arrow {
    width: 28px;
    height: 28px;
    background: url(../img/arrow.png) no-repeat 0 0;
    position: absolute;
    top: 50%;
    right: -10px;
    margin-top: -14px;
    cursor: pointer;
}
.apla.min  .arrow{
    background: url(../img/arrow.png) no-repeat -28px 0;
}

Upvotes: 0

Views: 652

Answers (3)

Alex Ball
Alex Ball

Reputation: 4474

This chek the parent .alpa class:

    $('.arrow').click(function(){
         $('.apla').not('.min').not($(this)).toggleClass("min", "slow");
         $(this).closest(".apla").toggleClass("min", "slow");
    });

Upvotes: 1

axl g
axl g

Reputation: 622

Try this :

$('.arrow').click(function(){
    var $currentApla = $(this).parent().parent()
    $('.apla:not(.min)').not('#'+$currentApla.attr('id')).toggleClass("min", "slow");
    $currentApla.toggleClass("min", "slow")
});

Hope this helps :)

Upvotes: 1

Jamie Hutber
Jamie Hutber

Reputation: 28064

You'll be after .is() as it returns true or false

 $('.arrow').click(function(){
     if (!$(".apla").is(".min")) {
     $(this).toggleClass("min", "slow");
     } else {
     $(this).parent().parent().toggleClass("min", "slow");
     }
 });

Upvotes: 0

Related Questions