How to apply a jQuery function conditionally?