Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:44:24+00:00 2026-06-01T14:44:24+00:00

I am nesting a lot of elements inside of a g element like so:

  • 0

I am nesting a lot of elements inside of a g element like so:

<g>
    <rect></rect>
    <text></text>
    ...
</g>

However, there are some rects that I want to be able to have drag events of their own. The problem is that when you put stuff inside of a g tag, its size expands to contain those tags. So even though I can assign events there is no way they can be triggered because the g tag’s event is somehow more important even though the rect is on top of it.

Is there some sort of workaround that you guys know of?

EDIT: Here’s a simple complete case in its entirety. A rect and a circle inside a g. The g is draggable and the circle should be draggable too but is not.

var gDragBehav = d3.behavior.drag()
    .on('drag', gDragDrag)

function gDragDrag(d,i) {
    d.x += d3.event.dx;
    d.y += d3.event.dy;
    d3.select(this)
        .attr('x', d.x)
        .attr('y', d.y)
        .attr("transform", "translate(" + d.x + "," + d.y + ")");
}

var circleDragBehav = d3.behavior.drag()
    .on('drag', circleDragDrag);

function circleDragDrag(d,i) {
    console.log('dragging a circle')
    d.cx += d3.event.dx;
    d.cy += d3.event.dy;
    d3.select(this)
        .attr('cx', d.cx)
        .attr('cy', d.cy)
}

var svg = d3.select('body').append('svg')

var g = svg.selectAll('g').data([{x: 10, y:10}])
    .enter().append('g').call( gDragBehav )

g.append( 'rect' ).attr('width', 100 ).attr('height', 100 )

g.selectAll( 'circle' ).data([{cx: 0, cy:0}])
    .enter().append( 'circle' )
    .attr('cx', function(d) { return d.cx } ).attr('cy', function(d) { return d.cy } )
    .attr('r', 40 )
    .call( circleDragBehav )

EDIT: Here’s some of the code

var group = this.d3svg.selectAll('g' + '.' + this.className)
  .attr('x', this.drawFuncs['x'] )
  .attr('y', this.drawFuncs['y'] )
  .attr("transform", this.drawFuncs['translate'] )
  .attr('class', this.className )
  .call(gDragBehav)
  .on( 'click', blockClickMenu )

ports = ['AtomicPort']
for ( port in ports ) {  
  drawPort.call( this, group, ports[port] )
}

function drawPort( d3svg, portName, redraw ) {
  d3svg.selectAll('rect.' + portName)
    .data( function(d) { return d.ports[ portName ] } )
    .enter().append('rect')
    .attr('x', this.drawFuncs['x'] )
    .attr('y', this.drawFuncs['y'] )
    .attr('width', this.drawFuncs['width'] )
    .attr('height', this.drawFuncs['height'] )
    .attr('class', portName )
    .call(portDragBehav)

  var portDragBehav = d3.behavior.drag()
    .on('drag', portDragDrag);

  function portDragDrag(d,i) {
    d.x += d3.event.dx;
    d.y += d3.event.dy;
    d3.select(this)
      .attr('x', d.x)
      .attr('y', d.y)
    d3.event.stopPropagation();
  }

  var gDragBehav = d3.behavior.drag()
    .on('dragstart', gDragStart)

  function gDragDrag(d,i) {
    d.x += d3.event.dx;
    d.y += d3.event.dy;
    d3.select(this)
      .attr('x', d.x)
      .attr('y', d.y)
      .attr("transform", "translate(" + d.x + "," + d.y + ")");
    d3.event.stopPropagation(); //Uncaught TypeError: Object #<Object> has no method 'stopPropagation'
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:44:25+00:00Added an answer on June 1, 2026 at 2:44 pm

    An SVG <g> element does not have a size or area; it is a transparent container for all its descendants, and cannot intercept events for other elements (unless you are adding an event listener to be triggered during the ‘capture phase’).

    As a parent element, events bubble up to it; likely what you are seeing is that whatever event you’re using to trigger the drag start (mousedown?) on the <rect> is also bubbling up to the <g> and starting a concurrent drag of that.

    To fix this, you want to stop your event from bubbling. In your event handler for the mousedown (or whatever) on the <rect> add:

    function startDrag(evt){
      // whatever your code is here
      evt.stopPropagation();
    }
    

    Without your actual code (or better, a pared-down test case) it’s hard to know for sure, or help you further.


    Edit Here’s a working version of your simple example: http://jsfiddle.net/ZrCQE/2/

    Specifically, apparently d3.event is not the event itself, but an object with a sourceEvent property that references the actual event.

    var dragGroup = d3.behavior.drag()
      .on('dragstart', function() {
        console.log('Start Dragging Group');
      }).on('drag', function(d, i) {
        d.x += d3.event.dx;
        d.y += d3.event.dy;
        d3.select(this).attr("transform", "translate("+d.x+","+d.y+")");
      });
    
    var dragCircle = d3.behavior.drag()
      .on('dragstart', function(){
        d3.event.sourceEvent.stopPropagation();
        console.log('Start Dragging Circle');
      })
      .on('drag', function(d,i){
        d.cx += d3.event.dx;
        d.cy += d3.event.dy;
        d3.select(this).attr('cx', d.cx).attr('cy', d.cy)
      });
    
    var svg = d3.select('body').append('svg').attr('viewBox','-50 -50 300 300');
    
    var g = svg.selectAll('g').data([{x:10,y:10}])
      .enter().append('g').call(dragGroup);
    
    g.append('rect').attr('width', 100).attr('height', 100);
    
    g.selectAll('circle').data([{cx: 90,cy:80}])
      .enter().append('circle')
        .attr('cx', function(d){ return d.cx })
        .attr('cy', function(d){ return d.cy })
        .attr('r', 30)
        .call(dragCircle);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like two different HTML elements that are at the same nesting level to
Something that happens to me a lot while web programming: I want to run
I'm working on some code that generates a lot of ignoring return value of
Is there a best practice concerning the nesting of label and input HTML elements?
A lot of text editors and IDEs have a feature that highlights matching parentheses,
We are nesting several entities. However upon retrieving we only want to get those
I'm working on nesting a series of pages that break off of a certain
Is the nesting of functions possible in the object oriented languages like C#, Java,
I have a 2 level nesting objects that i need help with My routes
Is there a way to create two dimensional NSArray without nesting arrays in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.