Make sure we use detach instead of remove in replaceWith. Fixes #5785.