How to Change the Style of an Element in JavaScript

In JavaScript, you can easily modify the CSS styles of an HTML element to change its appearance dynamically. This is useful when you want to change styles based on user interaction, such as button clicks or form submissions. This post will cover different ways to change the style of an element in JavaScript.

Table of Contents

  1. Using style Property
  2. Adding and Removing CSS Classes
  3. Using setAttribute() to Change Styles
  4. Conclusion

1. Using style Property

The style property is the most direct way to change an element's inline CSS styles in JavaScript. You can access and set individual CSS properties using this property.

Example:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Change Element Style</title>
</head>
<body>
  <h1 id="heading">Hello, World!</h1>
  <button onclick="changeStyle()">Change Style</button>
  <script src="script.js"></script>
</body>
</html>

JavaScript (script.js):

function changeStyle() {
  const heading = document.getElementById("heading");
  heading.style.color = "blue";
  heading.style.fontSize = "36px";
  heading.style.backgroundColor = "yellow";
}

Explanation:

  • heading.style.color = "blue"; changes the text color of the heading to blue.
  • heading.style.fontSize = "36px"; changes the font size.
  • heading.style.backgroundColor = "yellow"; adds a yellow background to the heading.

Important Notes:

  • CSS properties with hyphens (e.g., background-color) are converted to camelCase in JavaScript (e.g., backgroundColor).
  • This method sets the style inline, meaning it will override any styles set in external or internal CSS files.

2. Adding and Removing CSS Classes

Instead of directly changing the inline styles, you can use CSS classes to apply multiple styles at once. This is a more structured approach, especially when you need to apply the same styles to multiple elements.

Example:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    .highlight {
      color: white;
      background-color: red;
      padding: 10px;
    }
  </style>
  <title>Change Element Style</title>
</head>
<body>
  <h1 id="heading">Hello, World!</h1>
  <button onclick="toggleHighlight()">Toggle Highlight</button>
  <script src="script.js"></script>
</body>
</html>

JavaScript (script.js):

function toggleHighlight() {
  const heading = document.getElementById("heading");
  heading.classList.toggle("highlight");
}

Explanation:

  • The toggleHighlight() function uses classList.toggle() to add or remove the "highlight" class from the <h1> element.
  • The .highlight class contains predefined styles in the CSS block (color, background-color, and padding).
  • This method keeps your styling separated from your JavaScript logic, which is better for maintainability.

Other Class Manipulation Methods:

  • element.classList.add("className"): Adds a class to the element.
  • element.classList.remove("className"): Removes a class from the element.
  • element.classList.contains("className"): Checks if the element has a specific class.

3. Using setAttribute() to Change Styles

Another way to change the style is by using setAttribute(), which modifies the style attribute directly.

Example:

const heading = document.getElementById("heading");
heading.setAttribute("style", "color: green; background-color: lightgray;");

Explanation:

  • setAttribute("style", "css-rules") allows you to directly modify the style attribute, setting multiple CSS properties at once.
  • However, this method overwrites the existing inline styles. If the element already has some inline styles, they will be replaced.

4. Conclusion

Changing the style of an element in JavaScript is simple, and there are multiple ways to achieve it. The style property allows you to set individual styles directly, while CSS classes provide a cleaner, more maintainable approach. Using setAttribute() is another option, but it can overwrite existing styles.

Key Points:

  • style property: Directly modifies inline styles for individual CSS properties.
  • CSS classes: Use classList.add(), classList.remove(), and classList.toggle() to apply styles via CSS classes.
  • setAttribute(): Directly sets the style attribute with a string of CSS rules, but may overwrite existing inline styles.

Using CSS classes is generally preferred for better organization and maintainability, while the style property is useful for specific, dynamic changes.

Comments