title: How to access the DOM element that correlates to a D3 SVG object?
body: <p>I'm trying to learn D3 by experimenting with one of their <a href="http://mbostock.github.com/d3/ex/bubble.html" rel="noreferrer">basic bubblecharts</a>. First task: figure out how to drag an bubble and have it become the topmost object while it's being dragged. (The problem is getting D3's object model to map onto the DOM, but I'll get there...)</p>
  
  <p>To drag it, we can simply invoke d3's drag behavior using the code they provide:</p>
  
  <pre><code>var drag = d3.behavior.drag()
      .on("dragstart", dragstart)
      .on("drag", dragmove)
      .on("dragend", dragend);
  </code></pre>
  
  <p>Works great. Drags well. Now, how do we get it to be the topmost item? Search for "svg z-index" here and it becomes quickly apparent that the only way to change the index is to move an object further down in the DOM. OK. They don't make it easy because the individual bubbles don't have ID's, but messing around with the console, we can locate one of those bubble objects with:</p>
  
  <pre><code>$("text:contains('TimeScale')").parent()
  </code></pre>
  
  <p>and we can move it to the end of the containing svg element with:</p>
  
  <pre><code>.appendTo('svg')
  </code></pre>
  
  <p>Drag it after you do that, and it's the top-most item. So far, so good, <em>if</em> you're working entirely within the DOM. </p>
  
  <p>BUT: what I really want to do is have that happen automatically whenever a given object/bubble is being dragged. D3 provides a model for <code>dragstart()</code> and <code>dragend()</code> functions that will allow us to embed a statement to do what we want during the dragging process. And D3 provides the <code>d3.select(this)</code> syntax  that allows us to access <em>d3's</em> object representation of the object/bubble you're currently dragging. But how do I cleanly turn that massive array they return into a reference to a DOM element that I can interact with and - for instance - move it to the end of the svg container, or perform other references in the DOM, such as form submission?</p>