In the last unit scripts changed text and structure. Scripts can also change how elements look, which is how pages offer dark mode, highlighted errors and open or closed menus.

The style Property

Every element has a style property that sets inline styles. The statement box.style.backgroundColor = "navy" gives one element a navy background. CSS names that contain a hyphen become camel case in JavaScript, so background-color is written backgroundColor and font-size is written fontSize. Values are strings and must include their unit, for example "20px". The style property reads and writes only the inline style of the element. It does not report the rules that come from a style sheet.

The classList Property

Changing styles one at a time becomes untidy. A better way is to define the styles in CSS as a class, and let JavaScript add or remove the class. The classList property has the methods add, remove, toggle and contains. The method toggle adds the class when it is missing and removes it when it is present, which suits a switch between two states.

Definition: classList. An element property with methods add, remove and toggle for changing its CSS classes.

Table 4.1: Methods of classList

Method Effect Returns
add("dark") Adds the class Nothing
remove("dark") Removes the class Nothing
toggle("dark") Adds if missing, removes if present true or false
contains("dark") Checks for the class true or false

Why Classes Are Better

Keeping the design in CSS and letting JavaScript change only the class name has clear benefits. The designer can change the look without editing the script. One class can change many properties at once. The browser can also animate the change smoothly when the CSS has a transition.

Definition: Dark mode. A colour scheme with light text on a dark background, often switched on by a button.

Example 4.1: A light and dark switch

<style>
  #demo { background: #ffffff; color: #222222; padding: 14px;
          border: 1px solid #1F3864; font-family: Arial; }
  #demo.dark { background: #1b1b2f; color: #f1f1f1; }
</style>
<div id="demo">
  <p>Click the button to switch the theme.</p>
  <button id="mode">Switch theme</button>
</div>
<script>
  const demo = document.getElementById("demo");
  document.getElementById("mode").addEventListener("click", () => {
    demo.classList.toggle("dark");
  });
  demo.classList.add("dark");
</script>

Output in the browser:

The last line turns the dark theme on at load time to show the effect. A click on the button would switch it off again. On a real page the class is toggled on the body element.

Common mistakes

  • Writing background-color with a hyphen in JavaScript instead of backgroundColor.
  • Setting a number without a unit, such as style.width = 20 instead of "20px".
  • Setting many inline styles when one class would do.
  • Expecting style.color to report a colour that comes from a style sheet.

Key takeaways

  • The style property sets inline styles, with camel case names and units in strings.
  • classList.add, remove and toggle change classes, and toggle switches between two states.
  • Keep the design in CSS and let JavaScript flip classes.
  • A transition in CSS makes the change smooth.

Practice questions

  1. What is the JavaScript name of the CSS property font-size?
  2. What does classList.toggle do?
  3. Why is changing a class better than setting many styles?

Answers to the practice questions

  1. fontSize.
  2. It adds the class if it is missing and removes it if it is present.
  3. The design stays in CSS, one class changes many properties and designers can edit it without touching the script.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit IV