Reputation: 1889
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.
Upvotes: 11
Views: 15166
Reputation:
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())
});
Upvotes: 9
Reputation: 12240
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() );
});
Upvotes: 12
Reputation: 6331
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'));
Upvotes: 3