optimized class toggle to consider if cid is undefined