2018-10-04 13:37:37 +00:00
---
id: 587d7fa8367417b2b2512bc9
title: Update the Height of an Element Dynamically
challengeType: 6
2019-08-05 16:17:33 +00:00
forumTopicId: 301493
2021-01-13 02:31:00 +00:00
dashedName: update-the-height-of-an-element-dynamically
2018-10-04 13:37:37 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2018-10-04 13:37:37 +00:00
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:
2020-11-27 18:02:05 +00:00
1) Create a `div` for each data point in the array
2) Give each `div` a dynamic height, using a callback function in the `style()` method that sets height equal to the data value
2018-10-04 13:37:37 +00:00
Recall the format to set a style using a callback function:
2020-11-27 18:02:05 +00:00
`selection.style("cssProperty", (d) => d)`
# --instructions--
2021-03-05 19:25:29 +00:00
Add the `style()` method to the code in the editor to set the `height` property for each element. Use a callback function to return the value of the data point with the string `px` added to it.
2020-11-27 18:02:05 +00:00
# --hints--
2021-03-05 19:25:29 +00:00
The first `div` should have a `height` of `12` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(0)[0].style.height === '12px');
2018-10-04 13:37:37 +00:00
```
2021-03-05 19:25:29 +00:00
The second `div` should have a `height` of `31` pixels.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(1)[0].style.height === '31px');
```
2021-03-05 19:25:29 +00:00
The third `div` should have a `height` of `22` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(2)[0].style.height === '22px');
```
2021-03-05 19:25:29 +00:00
The fourth `div` should have a `height` of `17` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(3)[0].style.height === '17px');
```
2021-03-05 19:25:29 +00:00
The fifth `div` should have a `height` of `25` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(4)[0].style.height === '25px');
```
2018-10-04 13:37:37 +00:00
2021-03-05 19:25:29 +00:00
The sixth `div` should have a `height` of `18` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(5)[0].style.height === '18px');
```
2021-03-05 19:25:29 +00:00
The seventh `div` should have a `height` of `29` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(6)[0].style.height === '29px');
```
2021-03-05 19:25:29 +00:00
The eighth `div` should have a `height` of `14` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(7)[0].style.height === '14px');
```
2021-03-05 19:25:29 +00:00
The ninth `div` should have a `height` of `9` pixels.
2020-11-27 18:02:05 +00:00
```js
assert($('div').eq(8)[0].style.height === '9px');
```
# --seed--
## --seed-contents--
2018-10-04 13:37:37 +00: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];
2018-10-08 00:01:53 +00:00
2018-10-04 13:37:37 +00:00
d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
.attr("class", "bar")
// Add your code below this line
2018-10-08 00:01:53 +00:00
2018-10-04 13:37:37 +00:00
// Add your code above this line
< / script >
< / body >
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-10-04 13:37:37 +00:00
2019-12-16 15:18:30 +00: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 13:37:37 +00:00
```