Fix the error when the element or target is undefined