Minor JavaScript optimization

Always use cached jQuery element.
Extract callback to function.