'change edges line color when the connected node clicked in cytoscape.js

could someone give an example of Cytoscape.js about a node that when it clicked, the neighbors edges changed its styles.

this code doesn't work:

cy.$('node:selected').neighborhood('edge').style({
  'line-color': 'black'
});
cy.$('node:selected').connectedEdges().style({
  'line-color': 'black'
});


Solution 1:[1]

cy.$('node').on('grab', function (e) {
    var ele = e.target;
    ele.connectedEdges().style({ 'line-color': 'red' });
});


cy.$('node').on('free', function (e) {
    var ele = e.target;
    ele.connectedEdges().style({ 'line-color': '#FAFAFA' });
});

Solution 2:[2]

You have a race condition by assuming the order of events of tap/click and select.

Use :selected selectors for querying only with select events or use tap with the element passed in the event object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eric Aya
Solution 2 maxkfranz