2
\$\begingroup\$

I'm sure there's a better more 'jQuery' like way of writing this. In particular the selector $(this).closest('div.login-box') being in there twice. Perhaps it's possible to use $(this)?

$('.close-box').click(function () {
        $(this).closest('div.login-box').slideToggle("400");
        if ($(this).closest('div.login-box').attr('id') == "login-forgot") {
            clearForgottenPasswordInputs();
        }
    });
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You could combine the calls:

if ($(this).closest('div.login-box')
           .slideToggle("400")
           .attr('id'))

However, I do not recommend this.
Although it will work, it's unnecessarily confusing.

\$\endgroup\$
2
  • \$\begingroup\$ I dunno... if he just did loginbox_id = $(this).closest('div.login-box').slideToggle("400").attr('id'); Then he could just do if (loginbox_id == "login-forgot") below it. I think it removes any confusion that may occur by placing animation calls inside a boolean statement. \$\endgroup\$
    – daybreaker
    Commented Jun 14, 2011 at 21:02
  • 1
    \$\begingroup\$ @day: Yes. However, it's still confusing to have a mutation and a property access in the same line. \$\endgroup\$
    – SLaks
    Commented Jun 14, 2011 at 21:19

Your Answer

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

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