question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create DOM element without appending it?

See original GitHub issue

Is it possible to create a DOM element without appending it? In building an SVG tree I want to create a <g> element, add a bunch of children to it, then append the entire tree at once. I believe this is more efficient than calling append() right after select().data().enter() which would cause the browser to do more redraws than necessary.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
mbostockcommented, Apr 13, 2018

D3 5.0 added d3.create for creating a selection from a new detached DOM element. So you could say:

function createSvg() {
  const svg = d3.create("svg");
  // Add stuff to the SVG element here.
  return svg.node();
}

document.body.appendChild(createSvg());
9reactions
mbostockcommented, Jan 5, 2015

You haven’t set a width and height attribute on your svg element, and you haven’t specified the appropriate namespace in conjunction with document.createElementNS when creating the svg element. Another strategy you could consider is to remove the element from the DOM immediately after creating it:

var svg = d3.select("body").append("svg")
    .remove()
    .attr("width", 400)
    .attr("height", 400);

svg.append("circle")
    .attr("r", 200);

document.body.appendChild(svg.node());
Read more comments on GitHub >

github_iconTop Results From Across the Web

creating element node without appending - Stack Overflow
createElement () creates an element owned by the document from which it is created but does not give it any position in the...
Read more >
Document.createElement() - Web APIs | MDN
In an HTML document, the document.createElement() method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName ...
Read more >
Creating, Deleting, and Moving Elements - Using D3.js
D3.js has a number of selection methods that add, remove, and reposition elements in the DOM. They include the following: selection.append(type) - create, ......
Read more >
JavaScript HTML DOM Elements (Nodes) - W3Schools
To add a new element to the HTML DOM, you must create the element (element node) first, and then append it to an...
Read more >
Data Binding to DOM in D3 - TutorialsTeacher
Learn how to bind data to DOM elements and create new elements based on your ... append() will create DOM elements for which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found