Skip to content

Instantly share code, notes, and snippets.

@nkabrown
Last active March 1, 2016 14:48
Show Gist options
  • Save nkabrown/155f72a4cc38b92cbd96 to your computer and use it in GitHub Desktop.
Save nkabrown/155f72a4cc38b92cbd96 to your computer and use it in GitHub Desktop.
a way to access and style last child elements with d3 and DOM web api
// a workaround to apply style properties to only one element of a collection of elements in a d3 selection
d3.select('.container-div')
.selectAll('div')
.data(data)
.enter().append('div')
.attr('class', 'contained-divs')
// show last child element on hover
.on('mouseover', function(d) {
var s = d3.select(this)[0][0],
s0 = s.lastChild;
s0.style.opacity = 1;
})
.on('mouseout', function(d) {
var s = d3.select(this)[0][0],
s0 = s.lastChild;
s0.style.opacity = 0;
});
// create a child element
d3.selectAll('.contained-divs')
.append('div')
.attr('class', 'first-child');
// create a last child element
d3.selectAll('.contained-divs')
.append('div')
.attr('class', 'last-child');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment