---
id: 587d7fa7367417b2b2512bc8
title: Add Classes with D3
required:
- src: 'https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js'
challengeType: 6
---
## Description
Using a lot of inline styles on HTML elements gets hard to manage, even for smaller apps. It's easier to add a class to elements and style that class one time using CSS rules. D3 has the attr()
method to add any HTML attribute to an element, including a class name.
The attr()
method works the same way that style()
does. It takes comma-separated values, and can use a callback function. Here's an example to add a class of "container" to a selection:
selection.attr("class", "container");
## Instructions
Add the attr()
method to the code in the editor and put a class of bar
on the div
elements.
## Tests
```yml
tests:
- text: Your div
elements should have a class of bar
.
testString: 'assert($("div").attr("class") == "bar", "Your div
elements should have a class of bar
.");'
- text: Your code should use the attr()
method.
testString: 'assert(code.match(/\.attr/g), "Your code should use the attr()
method.");'
```
## Challenge Seed
## Solution
```js
// solution required
```