With jQuery 1.9.1 arguments are not passed from a trigger to the click handler, as where with jquery 1.7.2 they are nicely passed.
An example to demonstrate:
<!doctype html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<!--script src="http://code.jquery.com/jquery-1.7.2.min.js"></script-->
</head>
<body>
<input id="test" type="checkbox">
<button id='trigger'>trigger</button>
</body>
<script>
$('#test').bind('click',
function (e, data){
if (typeof data == 'undefined') {
alert('no data passed');
return;
}
alert('first passed=' + data.passed1 + ' second passed='+data.passed2);
});
$('#trigger').click(
function (e){
$('#test').trigger('click',{passed1:'first',passed2:'second'});
});
</script>
</html>
When I use jquery-1.7.2.js it still works fine.
What am I missing? Please help me.
jQuery click trigger event handling system is a wrapper over native browser events. Because jQuery retains a reference to an event handler when it is introduced using on click function, it can be activated using jQuery’s trigger (“click”) command. Additionally, the JavaScript within the onclick attribute will be triggered.
To trigger handlers bound via jQuery without also triggering the native event, use .triggerHandler() instead. When we define a custom event type using the .on() method, the second argument to .trigger() can become useful.
The .trigger () method can be used on jQuery collections that wrap plain JavaScript objects similar to a pub/sub mechanism; any event handlers bound to the object will be called when the event is triggered. Note: For both plain objects and DOM objects other than window, if a triggered event name matches the name of a property on the object, ...
An array of arguments can also be passed to the .trigger () call, and these parameters will be passed along to the handler as well following the event object. As of jQuery 1.6.2, single string or numeric argument can be passed without being wrapped in an array.
It's a known issue: http://bugs.jquery.com/ticket/13353
Workaround is to use .triggerHandler() instead e.g.:
$('#trigger').click(function (e) {
var $test = $('#test');
$test
.prop('checked', !$test.prop('checked'))
.triggerHandler('click',{passed1:'first',passed2:'second'});
});
JSFiddle: http://jsfiddle.net/antishok/xHVDx/2
Triggering clicks is evil in any case (if the intention is just to run the callback)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With