1

I currently have the following structure

svg
  g
    circle
    text
    text
    text
    text
      tspan class='minus-clicks'
...

I have several g elements with the same nodes. What I want is to access the tspan when the circle is clicked. The logic when the circle is clicked goes like this:

    var node = svg.selectAll("circle")
  .data(nodes)
  .enter()
  .append('g')
  .append("circle")
  .style("fill", function (d) {
    return 'rgb(108,181,205)';
  }).on("click", function (d, i) {
    if (i < 5) {
      d.radius *= 1.1;
      d3.select(this).attr("r", d.radius);

      // positions minus sign on every circle
      d3.select(this).select('.minus-clicks').style('display', 'block');
      d3.selectAll('.minus-clicks')
        .attr('x', d.radius/2)
        .attr('dy', -((1/30*d.radius)-1)+'em');

      force.resume();
    }
  }).call(force.drag);

So far I have tried the following:

d3.select('circle').select('.minus-clicks')
d3.select('circle').selectAll('.minus-clicks')
d3.select('circle .minus-clicks')

But none of these work.

4
  • 2
    How do you know they are not selected?
    – eko
    Commented Nov 24, 2016 at 5:31
  • @echonax when I do a console.log it prints null
    – Monica
    Commented Nov 24, 2016 at 13:46
  • 1
    I've tried to reproduce your problem here: jsfiddle.net/w8v2skx1 (tell me if it's not the same hierarchy). As you can see if you give a callback to your x (I used cx), it logs the value and actually changes the cx attribute of the tspan. So I think you have another problem
    – eko
    Commented Nov 24, 2016 at 13:49
  • 1
    @echonax Someone just answered it correctly. You can try to check his answer. It does exactly what I needed. Thanks though.
    – Monica
    Commented Nov 24, 2016 at 14:03

1 Answer 1

1

You can select the parentNode first and then get the right child

d3.select(this.parentNode).select('.minus-clicks')

See this fiddle

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.