All perfect praise be to Allah, The Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad is His slave and Messenger.
We have not come across any Islamic text that indicates that the jinn possess the person because impurity stained his body or clothing, but the Shariah encouraged us to protect ourselves from the jinn by saying ‘Bismillah’ (in the name of Allah) and ‘A‘oothu-billahi min Ash-Shaytaani-r-Rajeem’ (I seek refuge in Allah from the accursed Satan) before relieving oneself.
‘Ali ibn Abi Taalib, may Allah be pleased with him, narrated that the Prophet said, “The screen between the eyes of the jinn and the nakedness of the children of Adam when one of them enters the place of relieving oneself is that he says ‘Bismillah.’” [At-Tirmithi - Al-Albaani graded it saheeh (sound)]
Also, Anas ibn Maalik, may Allah be pleased with him, narrated that the Prophet said, “The screen between the eyes of the jinn and the nakedness of the children of Adam when they take off their clothes is that they say ‘Bismillah.’” [At-Tirmithi]
Anas, may Allah be pleased with him, also narrated that whenever the Prophet entered the toilet, he would say, “Allahumma Inni ‘A‘oothu Bika Min al-Khubuth Wal Khabaa'ith’ (O Allah, I seek refuge with You from male and female devils).”
Shaykh Ibn Taymiyyah mentioned some reasons that make the jinn possess or attack the human being, among which is that the human being urinates on the jinn.
Allah knows best.