JavaScript DOM Modifying CSS classes


Modifying CSS classes in JavaScript allows you to dynamically change the appearance of HTML elements based on user interactions or other conditions. This approach is often preferred over inline styles because it leverages the power of CSS for styling while keeping the styling logic separate from the JavaScript code.

Working with CSS Classes

  1. Adding CSS Classes
  2. Removing CSS Classes
  3. Toggling CSS Classes
  4. Checking for CSS Classes

1. Adding CSS Classes

You can add one or more CSS classes to an element using the classList.add() method. This method can accept multiple class names as arguments.

Example: Adding CSS Classes

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Add CSS Class Example</title> <style> .highlight { background-color: yellow; font-weight: bold; } </style> </head> <body> <div id="myDiv">This is a div element.</div> <button id="addClassButton">Add Highlight</button> <script> const button = document.getElementById('addClassButton'); const div = document.getElementById('myDiv'); button.addEventListener('click', function() { div.classList.add('highlight'); }); </script> </body> </html>

In this example:

  • CSS Class: .highlight is defined in the <style> block.
  • Adding Class: The classList.add('highlight') method is used to add the highlight class to the div element when the button is clicked.

2. Removing CSS Classes

You can remove one or more CSS classes from an element using the classList.remove() method. Similar to add(), it can accept multiple class names as arguments.

Example: Removing CSS Classes

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Remove CSS Class Example</title> <style> .highlight { background-color: yellow; font-weight: bold; } </style> </head> <body> <div id="myDiv" class="highlight">This is a div element.</div> <button id="removeClassButton">Remove Highlight</button> <script> const button = document.getElementById('removeClassButton'); const div = document.getElementById('myDiv'); button.addEventListener('click', function() { div.classList.remove('highlight'); }); </script> </body> </html>

In this example:

  • Removing Class: The classList.remove('highlight') method is used to remove the highlight class from the div element when the button is clicked.

3. Toggling CSS Classes

The classList.toggle() method adds a class if it is not present, and removes it if it is present. This method is useful for implementing features like showing/hiding elements or switching between states.

Example: Toggling CSS Classes

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Toggle CSS Class Example</title> <style> .highlight { background-color: yellow; font-weight: bold; } </style> </head> <body> <div id="myDiv">This is a div element.</div> <button id="toggleClassButton">Toggle Highlight</button> <script> const button = document.getElementById('toggleClassButton'); const div = document.getElementById('myDiv'); button.addEventListener('click', function() { div.classList.toggle('highlight'); }); </script> </body> </html>

In this example:

  • Toggling Class: The classList.toggle('highlight') method is used to toggle the highlight class on the div element when the button is clicked.

4. Checking for CSS Classes

You can check if an element has a specific class using the classList.contains() method. This method returns true if the class is present, and false if it is not.

Example: Checking for CSS Classes

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Check CSS Class Example</title> <style> .highlight { background-color: yellow; font-weight: bold; } </style> </head> <body> <div id="myDiv" class="highlight">This is a div element.</div> <button id="checkClassButton">Check Highlight</button> <script> const button = document.getElementById('checkClassButton'); const div = document.getElementById('myDiv'); button.addEventListener('click', function() { if (div.classList.contains('highlight')) { alert('The highlight class is present.'); } else { alert('The highlight class is not present.'); } }); </script> </body> </html>

In this example:

  • Checking Class: The classList.contains('highlight') method is used to check if the highlight class is present on the div element when the button is clicked. An alert message is displayed based on the result.

Best Practices

  1. Use CSS Classes for Styling: Rely on CSS classes to manage styles, as they provide better separation of concerns and keep your styles centralized in CSS files.
  2. Dynamic Class Management: Use JavaScript class manipulation methods to dynamically update styles in response to user interactions or other events.
  3. Performance Considerations: While class manipulation is generally efficient, be mindful of performance when working with a large number of elements or frequent style changes.
  4. Avoid Inline Styles: Prefer using CSS classes over inline styles for maintainability and reusability.