title: D3 drag & click in v6 for grouped objects
body: <p>Working off of <a href="https://observablehq.com/@d3/click-vs-drag" rel="nofollow noreferrer">Mike's example</a> I'm trying to figure out how to transfer the concept to grouped elements. I can create initiate drag and click individually, but together I can't seem to get click to work. Below is a snippet - this is just one of many, many approaches I've used.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  const width = 400;
    const height = 300;
    const radius = 5;
    
    const svg = d3.select("#chart").append('svg')
      .attr('width', '400')
      .attr('height', '400')
      .style('border', 'solid 1px');
  
    const circles = d3.range(20).map(i =&gt; ({
      x: Math.random() * (width - radius * 2) + radius,
      y: Math.random() * (height - radius * 2) + radius,
      index: i
    }));
  
  const group =  svg.selectAll("g").data(circles).enter()
        .append("g")
        .attr("cx", d =&gt; d.x)
        .attr("cy", d =&gt; d.y)            
        .call(d3.drag()
        .on("start", dragstarted)
        .on("drag", dragged)
        .on("end", dragended))
    
    group.append("circle")
        .attr("cx", d =&gt; d.x)
        .attr("cy", d =&gt; d.y)            
        .attr("r", radius)
        .attr("fill", d =&gt; d3.schemeCategory10[d.index % 10])
        .on("click", clicked);
        
      group.append("text")
        .attr("x", d =&gt; d.x)
        .attr("y", d =&gt; d.y)            
        .style("fill", 'white')
        .text('a')
      
  
    function clicked(event, d) {
      if (event.defaultPrevented) return; // dragged
  
      d3.select(this).transition()
          .attr("fill", "black")
          .attr("r", radius * 2)
        .transition()
          .attr("r", radius)
          .attr("fill", d3.schemeCategory10[d.index % 10]);
    }
  
  
    function dragstarted() {
      d3.select(this).attr("stroke", "black");
    }
  
    function dragged(event, d) {
      d3.select(this).raise().attr("cx", d.x = event.x).attr("cy", d.y = event.y);
    }
  
    function dragended() {
      d3.select(this).attr("stroke", null);
    }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://d3js.org/d3.v6.js"&gt;&lt;/script&gt;
  &lt;div id="chart"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>