d3: how to select a node by text content?

How do you use d3.select to get a node by its text content?

This stackoverflow answer made me think that I could use d3.selection.text() as a getter, and thus filter the results of a selectAll by the text content of the returned nodes.

But mimicking the pattern shown in that stackoverflow answer is not working for me. Instead, I’m getting the error

TypeError: Cannot read properties of null (reading 'textContent')

Here is a notebook demonstrating my failed attempt.

1 Like

I don’t believe that arrow functions bind this. Try a function function:

d3.select(test).selectAll('g.test-class > text').filter(function() {
  return d3.select(this).text() === "second node"
}).attr('fill', 'red')
1 Like