score:2
Accepted answer
As @FernOfTheAndes pointed out, the idea is really to append a foreign object to svg
, instead of appending input element to a html
property.
var svg = d3.select("#d3_line").append("svg")
.attr("width", 800)
.attr("height", 800);
line1 = {
"x1": 100,
"y1": 50,
"x2": 750,
"y2": 500
};
line2 = {
"x1": 100,
"y1": 200,
"x2": 750,
"y2": 650
};
var data = [];
data.push(line1);
data.push(line2);
var lineID = "";
function update() {
d3.select("#" + lineID).attr("x1", $("#new_x1").val());
$("#new_x1").remove();
}
for (var i = 0; i < data.length; i++) {
var line = d3.select("svg")
.append("svg:line")
.attr("id", ("line" + i))
.attr("x1", data[i].x1)
.attr("y1", data[i].y1)
.attr("x2", data[i].x2)
.attr("y2", data[i].y2)
.style("stroke", "black")
.style("stroke-width", 6)
.on("click", function () {
$(".externalObject").remove();
lineID = d3.select(this).attr("id");
svg.append("foreignObject")
.attr("class", "externalObject")
.attr("x", (d3.event.pageX - 20) + "px")
.attr("y", (d3.event.pageY - 40) + "px")
.attr("width", 200)
.attr("height", 100)
.append("xhtml:div")
.html("<input type='text' id=new_x1 placeholder='input new x1 here' onchange=update()></input>");
});
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<div id="d3_line"></div>
Source: stackoverflow.com
Related Query
- JavaScript/D3: Create floating textbox on click and update graph from textbox input
- Create a table structure in d3js from array of array and draw text or graph
- Reading from Json file to create graph javascript
- How to create an interactive graph network of resources, diseases, triggers and treatments using JavaScript
- d3 update data and update graph
- D3 update dataset on click and redraw the bar chart
- Reusable charts in d3: how do the create and update selections work?
- javascript d3.js: initialize brush with brush.extent and stop data from spilling over margin
- How to create a d3 radial-tree from a list of nodes and links?
- Create data visualizations using Express and get data from a PostgreSQL database
- d3js pie graph from jquery ajax - correlating json keys and values on the chart
- Both single and double click on a node in d3 force directed graph
- D3 map, click and get id from json
- How to convert d3.value() iterable and d3.rollup from javaScript into TypeScript
- d3js Force Directed Graph - Click on node to popup infobox which read from JSON
- How to create d3 graph using data from Rails database
- create and drag in same click
- Create external JavaScript file and call it on HTML5 canvas
- Create d3.js graph from data on mongodb server
- Clean way to create scatter plot from x and y vectors
- Javascript D3 How to access and manipulate dataset from CSV
- D3 - create network graph from 2 column list of nodes
- Javascript D3 graph sorting the graph and axes
- Changing time scale from days to weeks, how do I update and sum data values?
- Recursively map 3d.js graph nodes and links from nested structure
- d3 Click to create circle and click to remove
- D3 scatterplot click to push and splice data and update the DOM behaves erratically
- Javascript / Momentjs: Elegant way to find the min and max time from a set of dates?
- Create multinested JSON from URL in Javascript for d3.js
- d3.js touch and hold to create a line from one element to another
More Query from same tag
- React + d3js: Use React and parts of d3 or let d3 do it's own thing
- Avoid links criss cross / overlap in d3.js using force layout
- d3-fetch and UTF-16 LE
- Replacing Data in a function through parameter
- D3.js Force Directed Graph with Json - Create graph specifying link distance from all nodes
- Extreme zoom-in in d3 graph causes area (or line) graph to disappear
- Inverting SVG image mask
- How to rotate the angle of the cube in d3js?
- d3js and Canvas: Setting Alpha lower than 0.01
- d3: Plot as a cumulative graph
- D3.js Problems Filtering topojson data
- d3.parseDate Error cannot read property 'forEach' of undefined
- D3 grouped bar chart: How to rotate the text of x axis ticks?
- Confused by data binding and selecting specific data
- $.getJSON returns [object Object]
- set width and height of an SVG element - d3.js
- How to set minimal step for y-axis in d3.js?
- github pages serve javascript / d3 visualizations
- Leaflet: How to get pixel position of a lat lng after zoom, before zoom begins
- Best way to show Y axis values in Millions of £
- DC js pie chart
- d3pie error: no data supplied on d3pie.js
- Set bars sizes according to data percents
- Call class method within D3 method
- 3D Pie Chart using d3.js
- Wrapping every five svg elements in a div with D3.js
- d3js Pie Chart – text labels
- elasticY(true) with rangeChart does not adapt to new range
- Using D3 Zoomable Tree Map Code as a Template
- NVD3.js (d3.js) Scale Break