LML
LML

Reputation: 1

jquery check for class if statement not working

I would like to add a simple if statement to my scaling function, to check for the class ".pic"

but it doesn't seem to work (it does work fine if I remove the if statement part, but then every element on the page scales when clicked)

$(document).ready(function(){

$(this).click(function(event){
    var myTarget = event.target;
    if (myTarget.is('.pic') {
        $(myTarget).animate({ 
        width: "75%"
        }, 650 );
    }

});

});

edit: since I'm still stuck on this one, after the answer below, here is a bit of the html, maybe this is the problem?

<div class=myImages id=group001>
<img class=pic id=img001 src="../images/img001.jpg">
<div class=text id=des001>
mobile sequence 
</div>
</div>

Upvotes: 0

Views: 82

Answers (1)

jfriend00
jfriend00

Reputation: 707198

myTarget needs to be a jQuery object, not just a DOM reference:

$(this).click(function(event){
    var myTarget = $(event.target);
    if (myTarget.hasClass('pic') {
        myTarget.animate({width: "75%"}, 650 );
    }

});

I'd also recommend using .hasClass() instead of .is() for this specific test as it's probably much more efficient.

Upvotes: 1

Related Questions