10

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.

6
  • Why are you closing body before the script?? Commented Mar 10, 2013 at 23:15
  • None of the syntaxes seem to work after testing, seems like a bug. Commented Mar 10, 2013 at 23:21
  • 1
    Looks like a strange bug. jsfiddle.net/xHVDx/1 Commented Mar 10, 2013 at 23:24
  • 1
    Your original syntax seems to work with non-checkboxes: jsfiddle.net/9mD6a it also works in checkboxes with events other than click jsfiddle.net/9mD6a/1 seems like a corner-case bug. Commented Mar 10, 2013 at 23:25
  • This still works with 1.8.3, it seems support ends there however. Commented Mar 21, 2013 at 18:09

1 Answer 1

7
+25

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Echoing @antishok: Don't trigger clicks this way. Separate the handler code out into its own function and call it directly instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.