This seemed to be buggy in elements which had been moved around using css already. There's a jquery function that might be more robust.