JavaScript:D3 .classed()
Published Jan 4, 2024
Contribute to Docs
The .classed() method in D3.js is used to manipulate the classes associated with selected DOM elements.
Syntax
selection.classed(name, value);
name- The name of the CSS class to be added or removed.value- A boolean value that determines whether to add (true) or remove (false) the class.
Example
In this example, the .classed() method is used to dynamically add and remove the class highlight from the selected element, showcasing its utility in creating interactive and dynamic visualizations:
// Select an element with the id "myElement"var myElement = d3.select('#myElement');// Add a class called "highlight" to the selected elementmyElement.classed('highlight', true);// Remove the class "highlight" after a delaysetTimeout(function () {myElement.classed('highlight', false);}, 2000);
This example results in the following output:

Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript:D3 on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn how to create bar charts with D3, the popular interactive data visualization library.
- With Certificate
- Intermediate.1 hour