CKTIS / JavaScript
Element: classes, contents, and attribute presence
Use `className` to read or replace the class string, `innerHTML` to read or replace contents as markup, and `toggleAttribute()` to change whether an attribute exists.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
element.className
element.className = classNames
element.innerHTML
element.innerHTML = markup
element.toggleAttribute(name)
element.toggleAttribute(name, force)Element.className/innerHTML/toggleAttribute covers three DOM members, not ECMAScript 2024 features. The ECMAScript version listed in this entry does not indicate support for these APIs. They require a host that provides the relevant DOM APIs; support depends on the runtime.
On an HTML element, className reflects the class attribute as a string of whitespace-separated class names. Assigning it replaces the whole string. To change one class without disturbing the others, use classList. On SVG elements, className can be an SVGAnimatedString; use getAttribute('class') or setAttribute('class', value) to work with the attribute directly.
Reading innerHTML returns HTML or XML markup for the contents, excluding the element itself and its shadow root. Setting it parses markup in the element's context and replaces existing content nodes rather than appending. On a template element, it replaces the template contents. Listeners stay on the old nodes and do not transfer to the new ones. In an XML document, malformed markup can cause SyntaxError on write, and a failure to serialize can cause InvalidStateError on read.
Do not pass untrusted markup directly to innerHTML: it does not sanitize by default, which creates an XSS risk. Use textContent to display plain text in elements such as span. Do not assign untrusted text to a script element's textContent: the text can execute as code when the element is inserted into the document. If you need markup, sanitize untrusted input before insertion. If CSP enforces Trusted Types and there is no default policy, assigning a plain string throws TypeError; use TrustedHTML from an appropriate policy.
toggleAttribute(name) removes an existing attribute or adds a missing one with an empty value. With force, true ensures presence without changing an existing value, and false ensures absence. The boolean result says whether the attribute is present afterward, not whether anything changed. For an HTML boolean attribute such as disabled, even the value "false" does not turn it off: presence is what counts. On an HTML element in an HTML document, ASCII letters in the name are lowercased; an invalid name throws InvalidCharacterError.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Element DOM example</title>
</head>
<body>
<script>
(() => {
const button = document.createElement('button');
button.className = 'action muted';
button.className = 'action ready';
button.innerHTML = '<span>Draft</span>';
const oldLabel = button.firstElementChild;
button.innerHTML = '<strong>Save</strong>';
console.log(button.className);
console.log(button.innerHTML);
console.log(oldLabel.parentNode === null);
console.log(button.toggleAttribute('disabled'));
console.log(button.toggleAttribute('disabled', true));
console.log(button.toggleAttribute('disabled', false));
})();
</script>
</body>
</html>Result
action ready <strong>Save</strong> true true true false
Save the example as an HTML file, open it in a browser without Trusted Types enforcement, and check the developer console. The button is never added to the document. The first `true` confirms that the old label node was detached; the last three results show the state of `disabled` after adding, ensuring presence, and removing it.
Related lessons
- JavaScript · Lesson 39 · Intermediate · Version: 3