. toggleClass() is for this specific purpose From the doc. Add or remove one or more classes from each element in the set of matched elements, depending on either jquery hasclass condition Save to Google Drive. If you have a Google account, you can save this code to your Google Drive. Google will ask you to confirm Google Drive access.
jQuery hasClass() Method jQuery HTMLCSS Methods Try it Yourself Definition and Usage. The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return true . Syntax Parameter Description; classname: jquery hasclass condition