D3.js prepend (similar to jQuery prepend)
Asked Answered
H

2

84

I like the usage of append in D3, and I'm looking for prepend.

Does this exist in D3?

Hurwit answered 7/10, 2014 at 11:9 Comment(0)
T
138

You can use

selection.insert(newElement[, anotherExistingElement])

For example:

selection.insert("div",":first-child")

The above code will insert a div before the first child of selected element. Check documentation to learn more.

Another possible way of inserting elements before any node (including plain texts):

var parentEl = d3.select("div").node();
parentEl.insertBefore(document.createElement("div"), parentEl.childNodes[0]);
<script src="https://d3js.org/d3.v3.min.js"></script>
<div>
  This is a plain text
  <a></a>
</div>
Treasonous answered 7/10, 2014 at 11:58 Comment(4)
This will not, however insert before any plain text in the node/Malamud
The documentation moved here: github.com/d3/d3-selection/blob/master/…Uxmal
@AdamTolley any suggestion for how to insert before plan text?Purtenance
@ben I have added a possible solution in the answer. Hope it helps.Treasonous
D
22

Selection.lower()

selection.lower() will place an element as the first child of its parent.

Together with d3's append, selection.append().lower() can replicate jQuery's prepend

Since D3 v4+, D3 has both selection.raise() and selection.lower() methods. These are used most frequently to move elements in an SVG so that certain elements appear overtop of others, where ordering of SVG elements in the DOM determines draw order. But, they can be used for any element in the DOM.

Here's a quick demonstration using divs and paragraphs:

var div = d3.select("div");

div
  .append("p")
  .text("Inserted")
  .lower();

console.log(div.html());
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div id="div">
Text
<p> Child Paragraph </p>
</div>

The snippet takes a div with the following contents:

Text
<p> Child Paragraph </p>

And uses d3 to append a new paragraph and then lower it so that the structure is as follows:

<p>Inserted</p>
Text
<p> Child Paragraph </p>

And for comparison with with jQuery's prepend:

var div = $("div");

div
  .prepend("<p>Inserted</p>");

console.log(div.html());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div">
Text
<p> Child Paragraph </p>
</div>

More Info

The selection.lower() is implemented as such (see docs for more info):

selection.each(function() {
  this.parentNode.insertBefore(this, this.parentNode.firstChild);
});
Deterge answered 31/3, 2019 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.