seb
seb

Reputation: 2321

D3: load hyperlink dynamically on click

I'm new to D3 and javascript. I have found a tree layout that suits my needs and now I'm trying to make it display information dynamically. This is the jsfiddle for the tree.

Right now, when you click on a node, a hyperlink is displayed on top of the tree. The problem is that this is just a text string and not a clickable hyperlink.

I know that this is related to my code not actually telling D3 to display this as a hyperlink:

function click(d) {
  d3.select("#link").text(d.url);
  update(d);
}

I've tried to make it work using the javascript link() method, but wasn't very successful (the debugger said: link() method not found). How can I turn the text string into a clickable link?

Upvotes: 0

Views: 196

Answers (1)

schrieveslaach
schrieveslaach

Reputation: 1819

Add a <a> tag to the <div> displaying your link:

<div>
  <a id="#link"></a>
</div>

Then, you can update the link tag, like this:

d3.select("#link")
  .attr("href", d.url)
  .text(d.url);

Upvotes: 1

Related Questions