Because you add the modalOrigin class before calling this function, this will remove it from the element you just added. You should instead pass the origin element into this function like before and then add the class AFTER this line.