Reputation: 7053
$(document).ready(function(){
jQuery("#but1").bind("click",function(e){
alert(e.name);
});
jQuery("#but1").trigger({name:'Dmy', surname:'My'});
});
The alert fails to pass the data, why is that?!? the alert says 'undefined'.
What am I doing wrong, why I fail to pass the data?
JSFiddle here.
Upvotes: 1
Views: 241
Reputation: 3771
It's because the values you pass to trigger come in as other parameters to the click handler, so you can't access it on the event object. You need to accept an additional paramter.
$(document).ready(function(){
jQuery("#but1").bind("click",function(e, data){
alert(data.name);
});
jQuery("#but1").trigger('click', {name:'Dmy', surname:'My'});
});
Upvotes: 2
Reputation: 100175
You have left out the eventName
parameter for trigger()
.
$('#but1').trigger('click', ['Dmy', 'My']);
API reference: http://api.jquery.com/trigger/
Upvotes: 1