Michael
Michael

Reputation: 43

Trouble using if is() addClass()/removeClass()

I'm trying to make a sliding element toggle by animating it's alignment and adding/removing classes instead of just using the .toggle() function.

Unfortunatly my code doesn't feel like removing or adding any classes since I added the

if

$(document).ready(function() {  
    $(".slide-img-4").click(function(){
   if ("#slide4").is("mright") {
     $("#slide4").animate({ left: -610 }, "normal");
         $("#slide4").removeClass("mright");
        }
       else {
         $("#slide4").animate({ left: 610 }, "normal");
         $("#slide4").addClass("mright");
       };
    });
}); 

Upvotes: 0

Views: 489

Answers (3)

RhinoDevX64
RhinoDevX64

Reputation: 707

I think you're missing the period for the class mright.

if (("#slide4").is(".mright")) {

I'm not so sure that would work. You might want to try.

if (("#slide4").hasClass("mright")) {

Upvotes: 1

icktoofay
icktoofay

Reputation: 129099

The syntax for the if statement is not valid; the condition must have parentheses around it. Additionally, "$slide4".is("mright") would get a runtime error; strings have no is method. You probably meant to select #slide4 as a jQuery object first:

$(document).ready(function () {
    $(".slide-img-4").click(function () {
        if($("#slide4").is("mright")) {
            $("#slide4").animate({
                left: -610
            }, "normal");
            $("#slide4").removeClass("mright");
        } else {
            $("#slide4").animate({
                left: 610
            }, "normal");
            $("#slide4").addClass("mright");
        };
    });
});

Upvotes: 1

xdazz
xdazz

Reputation: 160893

Change if ("$slide4").is("mright") to if ($('#slide4').hasClass('mright'))

Upvotes: 1

Related Questions