views:

243

answers:

2

Hello,

I have the following code:

<body>
    <form>
        <input type="text"/>
    </form>

    <script>

        $( function () {

            $(document).bind("EVENT", function (event, element) {
                console.log("BIND", element, event);
            });

            $("form").each( function iterate(index, element) {

                console.log("BEFORE BIND", element);
                $(document).trigger("EVENT", element)
            });
        })
    </script>
</body>

I expect that the element that i pass to the TRIGGER is the same as i get at the BIND but no

BEFORE BIND: this's the FORM, as it's expected

BIND: this is the INPUT box, no idea why

is it a bug or i miss something?

best, Viktor

+1  A: 

If I understand your question correctly this should be what you are looking for:

$(function () {
    $(document).bind("EVENT", function (e) {
        console.log('bind', 
                    e.target // this is the DOM element that triggered the event
                             // the form in this case
                    e);
    });

    // Triggering can be simplified a lot
    $('form').trigger('EVENT');

});
Emil Ivanov
sorry, i was not clear, i was wondering if i pass a parameter to the trigger function why i don't get back the same in the bind.
Viktor
A: 

The jQuery trigger documentation says that extra parameters should be passed in an array. So:

$(document).trigger("EVENT", [ element ])
Sean Hogan
thx, if i pass it as an array it works
Viktor