156

I need an jQuery script that will see if any element has an specific class and do an action like change position.

This is the way, but I don`t think this will work.

$("a.contact").toggle(function() {
    $("#contact").animate({
        right: '0'
    }, 2000);

    if ($("#about").hasClass("opened")) {
        $("#about").animate({
            right: -700 + "px"
        }, 2000);
    }
}, function() {
    $("#contact").animate({
        right: -700 + "px"
    }, 2000);
});
4
  • Well you said that you want to know if "any" element has a certain class but your selector in your if statement is only targetting the element that has an id of "about" is that on purpose? Basically trying to figure out what your having a problem with. Also have your tried out the above code yet to see if it works?
    – Adrian
    Dec 30, 2010 at 18:18
  • Voting question down due to the fact that you don't think it will work? How about you try it first?!? If it doesn't work, tell us SPECIFICALLY what you expect to see, and what you actually saw.
    – ken
    Dec 30, 2010 at 18:25
  • @ken: indeed he should have, but since he's just a lowly noob, perhaps a kind nudge in the right direction would have been more appropriate? Incidentally, @Florescu, if you are curious if something will work but you can't get it figured out, mock the problem up on jsfiddle.net and link to it in your question.
    – treeface
    Dec 30, 2010 at 18:29
  • @treeface: that is the nudge (in lieu of calling him a lowly noob); if he updates the question to...you know, actually include a question, then I'll remove my downvote. Bad question (or lack of a question) = down vote, so that others don't have to waste their time on a non-issue such as this.
    – ken
    Dec 30, 2010 at 19:45

1 Answer 1

277

First, you're missing some parentheses in your conditional:

if ($("#about").hasClass("opened")) {
  $("#about").animate({right: "-700px"}, 2000);
}

But you can also simplify this to:

$('#about.opened').animate(...);

If #about doesn't have the opened class, it won't animate.

If the problem is with the animation itself, we'd need to know more about your element positioning (absolute? absolute inside relative parent? does the parent have layout?)

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.