var data = [137,2,3,4];
var next = 5;
function update () {
var items = d3.selectAll('li')
.data(data);
// Enter -- what needs to be done to add a DOM element for each datum
items.enter()
.append('li');
// Update -- what needs to be done to make the datum DOM elements appear as desired
items.text(function (d) { return d; });
// Exit -- what needs to be done to a DOM element whose datum no longer exists
items.exit()
.remove();
}
d3.select('body').append('button')
.text('add one')
.on('click', function () {
data.push(next++);
update();
});
d3.select('body').append('button')
.text('remove one')
.on('click', function () {
data.pop();
update();
});
d3.select('body').append('button')
.text('change one')
.on('click', function () {
if (data[0] & 1) {
data[0] += 1;
} else {
data[0] /= 2;
}
update();
});
update();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.