diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index c3fc3032f3..57ba838ee3 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -754,6 +754,61 @@ function test_submit() { $("#target").submit(); } +function test_trigger() { + + $("#foo").on("click", function () { + alert($(this).text()); + }); + $("#foo").trigger("click"); + + $("#foo").on("custom", function (event, param1?, param2?) { + alert(param1 + "\n" + param2); + }); + $("#foo").trigger("custom", ["Custom", "Event"]); + + $("button:first").click(function () { + update($("span:first")); + }); + + $("button:last").click(function () { + $("button:first").trigger("click"); + update($("span:last")); + }); + + function update(j) { + var n = parseInt(j.text(), 10); + j.text(n + 1); + } + + $("form:first").trigger("submit"); + + var event = jQuery.Event("submit"); + $("form:first").trigger(event); + if (event.isDefaultPrevented()) { + // Perform an action... + } + + $("p") + .click(function (event, a, b) { + // When a normal click fires, a and b are undefined + // for a trigger like below a refers to "foo" and b refers to "bar" + }) + .trigger("click", ["foo", "bar"]); + + var event = jQuery.Event("logged"); + (event).user = "foo"; + (event).pass = "bar"; + $("body").trigger(event); + + // Adapted from jQuery documentation which may be wrong on this occasion + var event2 = jQuery.Event("logged"); + $("body").trigger(event2, { + type: "logged", + user: "foo", + pass: "bar" + }); +} + function test_clone() { $('.hello').clone().appendTo('.goodbye'); var $elem = $('#elem').data({ "arr": [1] }), diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index bc7886e448..235022bf99 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -2531,8 +2531,34 @@ interface JQuery { */ submit(eventData?: any, handler?: (eventObject: JQueryEventObject) => any): JQuery; - trigger(eventType: string, ...extraParameters: any[]): JQuery; - trigger(event: JQueryEventObject): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param eventType A string containing a JavaScript event type, such as click or submit. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(eventType: string, extraParameters?: any[]): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param eventType A string containing a JavaScript event type, such as click or submit. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(eventType: string, extraParameters?: Object): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param event A jQuery.Event object. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(event: JQueryEventObject, extraParameters?: any[]): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param event A jQuery.Event object. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(event: JQueryEventObject, extraParameters?: Object): JQuery; triggerHandler(eventType: string, ...extraParameters: any[]): Object;