Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery 1.9.1 .trigger() not passing arguments to click-handler

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.

like image 543
user2154919 Avatar asked Mar 10 '13 23:03

user2154919


People also ask

How do you trigger a jQuery click event?

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.

How to trigger handlers bound via jQuery without also triggering native event?

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.

When to use trigger in jQuery?

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, ...

How do I pass an array of arguments to trigger?

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.


1 Answers

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)

like image 92
antishok Avatar answered Jan 04 '23 06:01

antishok