Moved messages jquery object to outside the function.