0

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>
1
  • what about creating jsFiddle - jsfiddle.net for this Commented Sep 30, 2012 at 18:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response, but unfortunately no luck after copy/pasting your example.
it's curious, still, if I remove the if statement, elements start scaling as expected (of course, without the control I want).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.