you should not search against the document instead use last parent this way if its detached from the document in will still work and if its in the document it will still work