freeCodeCamp Challenge Guide: Add Classes with D3

Add Classes with D3


Hint

Use the .attr() method to add the class attribute and set its value to bar. The method takes a name and a value. Example, .attr('id', 'someIdValue')


Solutions

Solution 1 (Click to Show/Hide)

<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>


Relevant Links

D3 docs: selection.attr(name[, value])

8 Likes