Skip to content

API reference

cy.on — Core > Events. Cytoscape.js signatures, parameters and return contract.

cy.on — Core › Events

Preview · Core › Events

Read in the complete API reference · All API members

#cy.on(events, callback)

// Bind a direct listener
cy.on(events: string, callback: EventHandler<NodeData, EdgeData>): this

Listen for core and bubbled element events: cy.on('tap', handler). For delegation, use cy.on('tap', ele => ele.isNode(), handler). Event names match literally; 'tap.ns' is different from 'tap'. Custom names are allowed and fire when your application emits them.

Parameters
NameDescription
events: string

one or more space-separated event names

callback: EventHandler<NodeData, EdgeData>

the event handler

Returns this

this core, for chaining

Example

const seen = [];
const handler = event => seen.push(event.type);
cy.on('example', handler);
cy.emit('example');
cy.off('example', handler);
console.log(seen); // ['example']

Example graph and handle setup

#cy.on(events, predicate, callback)

// Bind a delegated listener
cy.on(events: string, predicate: ElePredicate<NodeData, EdgeData>, callback: EventHandler<NodeData, EdgeData>): this

Listen for events on the core. Use cy.on('tap', handler) for core and bubbled element events. To delegate, use cy.on('tap', ele => ele.isNode(), handler).

Custom event names are accepted; a name the library never emits fires only when your application emits it. Event names match literally: 'tap.ns' is different from 'tap'. See the event reference for emitted names.

To remove a delegated listener, pass the same predicate and handler objects to off().

Parameters
NameDescription
events: string

one or more space-separated event names

predicate: ElePredicate<NodeData, EdgeData>

( ele) => boolean over the event target; only runs for element targets

callback: EventHandler<NodeData, EdgeData>

the handler

Returns this

this core, for chaining

Example

const seen = [];
const handler = event => seen.push(event.target.id());
cy.on('example', node => node.isNode(), handler);
cy.getElementById('a').emit('example');
console.log(seen); // ['a']

Example graph and handle setup

Also spelled cy.addListener.

See

Core#off — remove listeners using the same function objects