Accepted answer

You can break down the steps required to toggle on/off the visibility of a node's neighbors/edges by clicking that node as follows:

  1. You need to class links based on the names of the sources/targets, and give nodes ids based on their names. This makes determining which nodes/links to toggle trivial.
  2. You need to store whether or not each node is currently "active," which you can use to determine whether or not you should hide/show the node's neighbors.
  3. You need to add an on("click", function(){ ... }) to each node.

I created a JSFiddle that demonstrates this approach. The main points to highlight are that, first, I create a dictionary to map node names to the names of their neighbors, which makes determining which links to show/hide easy:

// Set up dictionary of neighbors
var node2neighbors = {};
for (var i =0; i < json.nodes.length; i++){
    var name = json.nodes[i].name;
    node2neighbors[name] = json.links.filter(function(d){
            return == name || == name;
            return == name ? :;

To get the names of the neighbors of a node n, I am first filtering all the links that include n, and then extracting the name of the node that is not n in each of those links.

Next, here's the logic performed when you click on a node. I'm using an array of nodes to keep track of the nodes we want to toggle on/off (the OP said this behavior should only apply for the blue node "Node1"):

var clickableNodes = ["Node1"];
nodes.filter(function(n){ return clickableNodes.indexOf( != -1; })
        // Determine if current node's neighbors and their links are visible
        var active   = ? false : true // toggle whether node is active
        , newOpacity = active ? 0 : 1;

        // Extract node's name and the names of its neighbors
        var name     =
        , neighbors  = node2neighbors[name];

        // Hide the neighbors and their links
        for (var i = 0; i < neighbors.length; i++){
  "circle#" + neighbors[i]).style("opacity", newOpacity);
            d3.selectAll("line." + neighbors[i]).style("opacity", newOpacity);
        // Update whether or not the node is active = active;

Related Query

More Query from same tag