Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

trigger failure in jquery trigger

$(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

Answers (2)

Mike Edwards
Mike Edwards

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'});
});

http://jsfiddle.net/RTXxY/33/

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

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

Related Questions