Accepted answer


with latest versions of d3, you can use selection.raise() as explained by tmpearce in its answer. please scroll down and upvote!

original answer

you will have to change the order of object and make the circle you mouseover being the last element added. as you can see here: and as suggested by nautat, you have to define the following before your main script:

d3.selection.prototype.movetofront = function() {
  return this.each(function(){

then you will just have to call the movetofront function on your object (say circles) on mouseover:

  var sel =;

edit: as suggested by henrik nordberg it is necessary to bind the data to the dom by using the second argument of the .data(). this is necessary to not lose binding on elements. please read henrick's answer (and upvote it!) for more info. as a general advice, always use the second argument of .data() when binding data to the dom in order to leverage the full performance capabilities of d3.

edit: as mentioned by clemens tolboom, the reverse function would be:

d3.selection.prototype.movetoback = function() {
    return this.each(function() {
        var firstchild = this.parentnode.firstchild;
        if (firstchild) {
            this.parentnode.insertbefore(this, firstchild);


from my painful experience with ie9, using parentnode.appendchild may lead to lost event handlers in the nodes. so i tend to use another approach, that is, sorting the nodes so that the selected one is above the others:

     .on("mouseover", function(selected) {
        .sort(function(a, b) {
          if ( === {
            return 1;
          } else {
            if ( === {
              return -1;
            } else {
              return 0;


if you use the movetofront() method, make sure you are specifying the second argument to the data() join method, or your data will be out of synch with your dom.

d3.js joins your data (provided to a parse()) method with dom elements you create. if you manipulate the dom as proposed above, d3.js won't know what dom element corresponds to what data 'point' unless you specify a unique value for each data 'point' in the data() call as the api reference shows:

.data(data, function(d) { return; })


as of d3 version 4, there are a set of built in functions that handle this type of behavior without needing to implement it yourself. see the d3v4 documentation for more info.

long story short, to bring an element to the front, use selection.raise()


re-inserts each selected element, in order, as the last child of its parent. equivalent to:

selection.each(function() {

Related Query