Manipulate addClass and removeClass using jQuery in Tamil
In jQuery, addClass() and removeClass() are methods used for dynamically manipulating the classes of HTML elements. These methods provide a convenient way to add or remove CSS classes to/from elements on a webpage, allowing for dynamic changes to the styling of those elements.
addClass() Method :
The addClass() method is used to add one or more classes to the selected elements.
$(selector).addClass(className1, className2, ...)
selector : A string containing a selector expression to match the elements against.
className1, className2, ... : One or more class names to be added to the elements.
removeClass() Method :
The removeClass() method is used to remove one or more classes from the selected elements.
selector : A string containing a selector expression to match the elements against.
className1, className2, ... : One or more class names to be removed from the elements.
Features
These methods are often used in response to user interactions, such as button clicks or other events.
You can add or remove multiple classes by separating them with spaces within a single addClass() or removeClass() call.
These methods are commonly used to create interactive and dynamic user interfaces, where the appearance of elements can change based on user actions.
Source Code
$('#b1').click(function(){...}); : This code sets up a click event handler for the button with the ID 'b1'. When this button is clicked, it adds the 'design' class to the <h3> element.
$('#b2').click(function(){...}); : This code sets up a click event handler for the button with the ID 'b2'. When this button is clicked, it removes the 'design' class from the <h3> element.
Initially, the heading "Tutor Joes" has the 'design' class applied, so it has a red color, a font size of 25px, and a specific font family.
Clicking the "Show" button (#b1) has no visible effect because the 'design' class is already applied.
Clicking the "Hide" button (#b2) removes the 'design' class from the <h3> element, resulting in the removal of the specific styling defined in the 'design' class.
Subsequent clicks on the "Show" and "Hide" buttons toggle the presence of the 'design' class, affecting the styling of the <h3> element dynamically.
In summary, addClass() and removeClass() are powerful tools in jQuery for dynamically modifying the classes of HTML elements, allowing for flexible and responsive styling in web applications.