2.4 KiB
2.4 KiB
id | title | challengeType |
---|---|---|
587d7fa7367417b2b2512bc8 | Add Classes with D3 | 6 |
Description
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");
Note that the "class" parameter will remain the same whenever you need to add a class and only the "container" parameter will change.
Instructions
attr()
method to the code in the editor and put a class of bar
on the div
elements.
Tests
tests:
- text: Your <code>div</code> elements should have a class of <code>bar</code>.
testString: assert($('div').attr('class') == "bar", 'Your <code>div</code> elements should have a class of <code>bar</code>.');
- text: Your code should use the <code>attr()</code> method.
testString: assert(code.match(/\.attr/g), 'Your code should use the <code>attr()</code> method.');
Challenge Seed
<style>
.bar {
width: 25px;
height: 100px;
display: inline-block;
background-color: blue;
}
</style>
<body>
<script>
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
// Add your code below this line
// Add your code above this line
</script>
</body>
Solution
<style>
.bar {
width: 25px;
height: 100px;
display: inline-block;
background-color: blue;
}
</style>
<body>
<script>
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
// Add your code below this line
.attr("class","bar");
// Add your code above this line
</script>
</body>