score:10
Remember that the key function will be invoked on each element of your data array (i.e. dt), as well as on each element in your selection (i.e. d3.selectAll(".input")). In both cases, the key function needs to return a valid value and those output values will used to make the association.
To achieve your goal you can do something like the following example. See fiddle for live version: http://jsfiddle.net/2Fkjq/
If your document contains the following:
<div id="c"></div>
<div id="a"></div>
<div id="b"></div>
then executing this:
var data = [
{ key: "a", val: "aaa" },
{ key: "b", val: "bbb" },
{ key: "c", val: "ccc" }
];
d3.selectAll("div")
.data(data, function(d) { return (d && d.key) || d3.select(this).attr("id"); })
.text(function(d) { return d.val; });
will result in this document:
<div id="c">ccc</div>
<div id="a">aaa</div>
<div id="b">bbb</div>
You see that the key function consists of two parts. The first part is designed for the data array:
(d && d.key)
That part will return undefined
for the elements in the d3 selection. The second part targets those elements:
d3.select(this).attr("id")
Source: stackoverflow.com
Related Query
- Join existing elements of the DOM to data with d3.js
- How to update the fill color on existing svg elements with d3.js?
- Get attributes of existing SVG elements and bind as data with d3.js
- D3.js: plot dots on the existing line in a multiseries line chart with long formatted data
- Handling Removal of Elements with Data Join in D3.js
- Generating multiple DOM elements from a single data point with D3.js
- How do you update an existing svg element with the d3v5 join syntax
- If you specify a data method the second argument in D3.js, existing elements are undefined
- A very simple d3 data join adds the new elements in the wrong place
- How to rearrange the DOM order of svg elements based on their data values in a dynamically changing d3 vis?
- d3: drawing elements correctly but not adding new data to the dom
- How to update all existing elements in the same level with same parent value?
- d3.js: grouping with <g> (with the data join enter/update/exit cycle)
- Understanding D3 data join using the new syntax - Array data is update but DOM not
- populating existing SVG elements with JSON data using D3
- Can I update DOM elements in a D3 selector based on a data join to a different data object?
- Can I update DOM elements in a D3 selector based on a data join to a different data object?
- Appending multiple non-nested elements for each data member with D3.js
- D3: update data with multiple elements in a group
- How to update elements of D3 force layout when the underlying data changes
- nvd3.js : unable to bind onClick event with the data points in the svg
- D3 updating graph with new elements create edges with the wrong nodes
- How to update elements of an HTML that the elements are created using data from a CSV file?
- What is the equivalent of `link` function in Angular to access DOM elements
- Looping through data attributes to create 4 separate bar charts... why are there "phantom" data elements being bound to the xAxis?
- D3.js: Zooming the x-axis and data with mouse wheel scroll
- D3.js - Highlight chart elements when interacting with the legend & vice versa
- How to use D3 force layout with existing SVG elements as nodes
- Update with DOM children and data propagation
- Unable to reference d3.js data imported from a csv file with spaces in the header
More Query from same tag
- foreignObject bug with Chrome with d3?
- How to use D3 to append several html line of codes
- SVG expand line verticaly
- Reverse the d3-hierarchy (d3-tree) graph to left side to show downstream as well
- d3: how to visualize a simple 2D matrix?
- D3 zoom large images performance has huge lag
- Using d3.nest() with geojson files
- svg circle not registering mouseover very well
- When reading a Json file it would not understand HTML content?
- Getting the Correct Viewport Coordinates via getBoundingClientRect()
- D3.js scope issue with external data.json file and brush
- Turn list of edges into nested tree structure in d3
- Creating multi graph using multiple JSON array datasets
- graph works on Chrome but not on Firefox
- Where is the difference between addEventListener (classic dom svg-elment) and .on (at d3js)
- Partition a map into uniform regions
- D3 grouping bars
- Chart's Y-axis does not re-render properly in Billboard.js
- How to manually apply Transition of color in d3js at path attribute
- Load new page using d3
- d3 event handler on node not in HTML DOM
- How to render a directive only after data is loaded from tsv in angular js
- how to integrate d3 with require.js
- D3: How to set symbols insted of points in a ScatterPlot
- NetworkD3 sankey chart in R creating really messy links, even though my code should be good. What is wrong?
- How can I find out what projection my pre-projected GeoJSON is using?
- Trouble with getting a bubble chart working in d3
- Adding a data-* attribute to charts with r2d3.js
- Force directed graph with circular group levels
- Inserting image inside donut