2018-10-04 14:37:37 +01:00
---
id: 587d7fa8367417b2b2512bc9
title: Update the Height of an Element Dynamically
challengeType: 6
2019-08-05 09:17:33 -07:00
forumTopicId: 301493
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
The previous challenges covered how to display data from an array and how to add CSS classes. You can combine these lessons to create a simple bar chart. There are two steps to this:
1) Create a < code > div< / code > for each data point in the array
2) Give each < code > div< / code > a dynamic height, using a callback function in the < code > style()< / code > method that sets height equal to the data value
Recall the format to set a style using a callback function:
< code > selection.style("cssProperty", (d) => d)< / code >
< / section >
## Instructions
< section id = 'instructions' >
Add the < code > style()< / code > method to the code in the editor to set the < code > height< / code > property for each element. Use a callback function to return the value of the data point with the string "px" added to it.
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: The first < code > div</ code > should have a < code > height</ code > of 12 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(0)[0].style.height === '12px');
2018-10-04 14:37:37 +01:00
- text: The second < code > div</ code > should have a < code > height</ code > of 31 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(1)[0].style.height === '31px');
2018-10-04 14:37:37 +01:00
- text: The third < code > div</ code > should have a < code > height</ code > of 22 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(2)[0].style.height === '22px');
2018-10-04 14:37:37 +01:00
- text: The fourth < code > div</ code > should have a < code > height</ code > of 17 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(3)[0].style.height === '17px');
2018-10-04 14:37:37 +01:00
- text: The fifth < code > div</ code > should have a < code > height</ code > of 25 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(4)[0].style.height === '25px');
2018-10-04 14:37:37 +01:00
- text: The sixth < code > div</ code > should have a < code > height</ code > of 18 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(5)[0].style.height === '18px');
2018-10-04 14:37:37 +01:00
- text: The seventh < code > div</ code > should have a < code > height</ code > of 29 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(6)[0].style.height === '29px');
2018-10-04 14:37:37 +01:00
- text: The eighth < code > div</ code > should have a < code > height</ code > of 14 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(7)[0].style.height === '14px');
2018-10-04 14:37:37 +01:00
- text: The ninth < code > div</ code > should have a < code > height</ code > of 9 pixels.
2019-12-16 16:18:30 +01:00
testString: assert($('div').eq(8)[0].style.height === '9px');
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'html-seed' >
```html
< 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];
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
.attr("class", "bar")
// Add your code below this line
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
// Add your code above this line
< / script >
< / body >
```
< / div >
< / section >
## Solution
< section id = 'solution' >
2019-12-16 16:18:30 +01:00
```html
< 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")
.attr("class", "bar")
.style('height', d => `${d}px` )
< / script >
< / body >
2018-10-04 14:37:37 +01:00
```
2019-07-18 08:24:12 -07:00
2018-10-04 14:37:37 +01:00
< / section >