Code Monkey home page Code Monkey logo

Comments (5)

mbostock avatar mbostock commented on March 28, 2024 4

Added insert(tagName, referenceSelector). For example, insert("div", ":first-child") prepends child nodes.

from d3.

drarmstr avatar drarmstr commented on March 28, 2024 1

Note a difference is that (at least with Chrome 37 and D3 3.4.6) JQuery's prepend() will insert before any text node content while D3's insert(...,':first-child') will only insert before normal nodes, not text nodes.

from d3.

mbostock avatar mbostock commented on March 28, 2024

Also, it might be nice for these methods to allow references to existing nodes, via the W3C DOM API (e.g., document.getElementById("foo")). But then it seems like you'd want to support the argument being a function, and not just a string... in which case, perhaps the function could return the node to insert (or return a string for the name of the node to create?).

If we allowed a function, perhaps that would allow the function to use a selector internally, especially if apply returned the array of matched nodes.

from d3.

mbostock avatar mbostock commented on March 28, 2024

I like the idea of a prepend method for adding to the front. For inserting at a given location, a simple way to implement that is an insertAfter method, which creates a new node that is inserted after the context node, rather than as a child of the context node. This makes it easy to chain siblings together, as well:

d3.select("div")
  .append("span").attr("class", "sibling-1")
  .insertAfter("span").attr("class", "sibling-2")
  .insertAfter("span").attr("class", "sibling-3");

The alternative would be require capturing a reference to the selected div:

var div = d3.select("div");
div.append("span").attr("class", "sibling-1");
div.append("span").attr("class", "sibling-2");
div.append("span").attr("class", "sibling-3");

Or a way to select the parent. But you get the idea.

from d3.

mbostock avatar mbostock commented on March 28, 2024

I guess we could name it insert for brevity, though I like the idea of doing before and after. And I think after should be the default, since insert-before is a bit awkward.

from d3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.