I realised that I was using attributes(), but if I replace with addAttributes() it preserves the id & class. I will mark this as solved.
attributes()
addAttributes()