How can I use jQuery to interact with SVG generated by Alchemy.js?
I am attempting to use jQuery to interact with SVG elements built by Alchemy.js.
The following code does not work as expected (allowing me to click on a link and see an alert with the value of the element), but when I go into the Firefox console and type manually $("g").on("click", displayNode) the interaction works as expected.
This would appear to be some issue related to the readiness of the SVG elements during script execution (?).
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/alchemyjs/0.4.2/alchemy.min.css" />
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/alchemyjs/0.4.2/alchemy.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/alchemyjs/0.4.2/scripts/vendor.js"></script>
<link rel="stylesheet" type="text/css" href="svg/jquery.svg.css">
<script type="text/javascript" src="svg/jquery.svg.js"></script>
<script type="text/javascript">
function displayNode ()
{
var label = $(this).find("text").text();
alert("Clicked on [" + label + "]");
}
var config =
{
dataSource: "philosopher.json",
cluster: true,
clusterColours: ["#DD79FF", "#00FF30", "#5168FF", "#f83f00", "#ff8d8f"],
forceLocked: false,
nodeCaption: "title",
edgeCaption: "relatedness",
nodeCaptionsOnByDefault: true,
nodeTypes: {"type":["philosopher"]},
directedEdges:true,
nodeStyle:
{
"philosopher":
{
"radius": 18
}
},
initialScale: 0.7,
initialTranslate: [250,150]
};
$(function () {
alchemy = new Alchemy(config);
$("g.active").on("click",displayNode);
});
</script>
</head>
<body>
<div class="alchemy" id="alchemy"></div>
</body>
</html>