You can try using another HTMLSnippet widget with Refresh on context change and update which resets your class, perhaps something like:
I’m not 100% sure this will work but the jQuery should be re-run every time the context changes so you should be removing the class on object change. If it works you can enhance it by actually getting it to detect whether the class is needed or not on context change.
Another potential alternative is to have two snippets, one that adds the class, one that removes it, and to put conditional visibility on them. I would assume if the widget is disabled by conditional visibility it does not build therefore you should in theory be able to run the jQuery selectively based on entity attributes.