user2204897
user2204897

Reputation: 21

cytoscape.js add node dynamicaly but mouseover doesn't work after

The code adds a node dynamically but mouseover doesn't trigger for node "c"

// Mouseover function just give id node and position
cy.$('node').on('mouseover', function(evt) {
  var myClickedID = evt.cyTarget.id();
  console.log(myClickedID);
  console.log(cy.$('#' + myClickedID).position());
});

// add a node dynamicaly
cy.$('node').on('click', function(evt) {
  var elements = cy.add({
    group: 'nodes',
    data: { id: 'c' }
  });
});

Upvotes: 1

Views: 452

Answers (1)

maxkfranz
maxkfranz

Reputation: 12242

You didn't bind to events on the added node. It's the exact same behaviour in the DOM. You can't expect that setting a listener on prior elements will automatically set listeners on newly added elements.

Use a delegate selector if you want to bind only once: http://js.cytoscape.org/#cy.on

Upvotes: 1

Related Questions