Remove elements using .exit and .remove.
From D3 in Depth book by Peter Cook.
| license: gpl-3.0 | |
| height: 130 | |
| border: no |
Remove elements using .exit and .remove.
From D3 in Depth book by Peter Cook.
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <head> | |
| <title>Remove elements using .exit and .remove</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> | |
| <div></div> | |
| <div></div> | |
| </div> | |
| <div id="menu"> | |
| <button onClick="doExit();">Remove elements using .exit and .remove</button> | |
| </div> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script> | |
| <script> | |
| var myData = ['A']; | |
| function doExit() { | |
| d3.select('#content') | |
| .selectAll('div') | |
| .data(myData) | |
| .exit() | |
| .remove(); | |
| } | |
| </script> | |
| </body> | |
| </html> |