How do you dynamically add and remove classes from an element in jQuery?
Instruction: Demonstrate with code how to add and then remove a class from a selected element.
Context: Manipulating an element's class attribute is a fundamental jQuery skill, allowing for dynamic changes to styling and functionality.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Use .addClass() and .removeClass() on the selected elements after they exist:
const $menu = $('#menu'); $menu.addClass('active'); console.log($menu.hasClass('active')); // true, if #menu exists $menu.removeClass('active'); console.log($menu.hasClass('active')); // false...