This example in the documentation:
cy.on('click', function(evt){
console.log( 'clicked ' + this.id() );
});
Results in:
Uncaught TypeError: Object [object Object] has no method 'id'
And evt.cyTarget.data() returns undefined.
This example in the documentation:
cy.on('click', function(evt){
console.log( 'clicked ' + this.id() );
});
Results in:
Uncaught TypeError: Object [object Object] has no method 'id'
And evt.cyTarget.data() returns undefined.
The .id() function works on elements, but you don't have an element in your event handler. You bound to the core without any delegate element selector, so you bound to the core itself -- meaning the reference to this
points to cy
.
This is probably what you meant:
cy.on('click', 'node', function(evt){
console.log( 'clicked ' + this.id() );
});
evt.target
is necessarily a node because of the delegate selector. –
Kitts I use this for 2.x:
cy.on('tap', 'node', function (evt) {
console.log(evt.cyTarget.id())
});
Or for 3.x:
cy.on('tap', 'node', function (evt) {
console.log(evt.target.id())
});
As mentioned in the documentation for accessing data, you access element data with the eles.data()
method. In your case it would be that you defined the id as the node name, then it's just a matter of calling
console.log('clicked ' + this.data('id'));
© 2022 - 2024 — McMap. All rights reserved.