Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active October 27, 2019 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3indepth/8c88eef33524c142152038fc14798fd2 to your computer and use it in GitHub Desktop.
Save d3indepth/8c88eef33524c142152038fc14798fd2 to your computer and use it in GitHub Desktop.
Add elements using .enter and .append (starting with empty selection).
license: gpl-3.0
height: 130
border: no

Add elements using .enter and .append (starting with empty selection).

From D3 in Depth book by Peter Cook.

<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Add elements using .enter and .append (starting with empty selection)</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
color: #333;
}
#content div {
display: inline-block;
margin: 10px;
background-color: orange;
color: white;
padding: 30px;
width: 10px;
height: 10px;
text-align: center;
}
</style>
<body>
<div id="content">
</div>
<div id="menu">
<button onClick="doEnter();">Add elements using .enter and .append</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var myData = ['A', 'B', 'C', 'D', 'E'];
function doEnter() {
d3.select('#content')
.selectAll('div')
.data(myData)
.enter()
.append('div');
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment