module( "event", { setup: function() { document.body.focus(); }, teardown: moduleTeardown }); test("null or undefined handler", function() { expect(2); // Supports Fixes bug #7229 try { jQuery("#firstp").on( "click", null ); ok(true, "Passing a null handler will not throw an exception"); } catch ( e ) {} try { jQuery("#firstp").on( "click", undefined ); ok(true, "Passing an undefined handler will not throw an exception"); } catch ( e ) {} }); test("on() with non-null,defined data", function() { expect(2); var handler = function( event, data ) { equal( data, 0, "non-null, defined data (zero) is correctly passed" ); }; jQuery("#foo").on("foo.on", handler); jQuery("div").on("foo.delegate", "#foo", handler); jQuery("#foo").trigger("foo", 0); jQuery("#foo").off("foo.on", handler); jQuery("div").off("foo.delegate", "#foo"); }); test("Handler changes and .trigger() order", function() { expect(1); var markup = jQuery( "

b

" ), path = ""; markup .find( "*" ).addBack().on( "click", function() { path += this.nodeName.toLowerCase() + " "; }) .filter( "b" ).on( "click", function( e ) { // Removing span should not stop propagation to original parents if ( e.target === this ) { jQuery(this).parent().remove(); } }); markup.find( "b" ).trigger( "click" ); equal( path, "b p div div ", "Delivered all events" ); markup.remove(); }); test("on(), with data", function() { expect(4); var test, handler, handler2; handler = function(event) { ok( event.data, "on() with data, check passed data exists" ); equal( event.data["foo"], "bar", "on() with data, Check value of passed data" ); }; jQuery("#firstp").on("click", {"foo": "bar"}, handler).trigger("click").off("click", handler); ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." ); test = function(){}; handler2 = function(event) { equal( event.data, test, "on() with function data, Check value of passed data" ); }; jQuery("#firstp").on("click", test, handler2).trigger("click").off("click", handler2); }); test("click(), with data", function() { expect(3); var handler = function(event) { ok( event.data, "on() with data, check passed data exists" ); equal( event.data["foo"], "bar", "on() with data, Check value of passed data" ); }; jQuery("#firstp").on( "click", {"foo": "bar"}, handler).trigger("click").off("click", handler); ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." ); }); test("on(), with data, trigger with data", function() { expect(4); var handler = function(event, data) { ok( event.data, "check passed data exists" ); equal( event.data.foo, "bar", "Check value of passed data" ); ok( data, "Check trigger data" ); equal( data.bar, "foo", "Check value of trigger data" ); }; jQuery("#firstp").on("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).off("click", handler); }); test("on(), multiple events at once", function() { expect(2); var handler, clickCounter = 0, mouseoverCounter = 0; handler = function(event) { if (event.type === "click") { clickCounter += 1; } else if (event.type === "mouseover") { mouseoverCounter += 1; } }; jQuery("#firstp").on("click mouseover", handler).trigger("click").trigger("mouseover"); equal( clickCounter, 1, "on() with multiple events at once" ); equal( mouseoverCounter, 1, "on() with multiple events at once" ); }); test("on(), five events at once", function() { expect(1); var count = 0, handler = function() { count++; }; jQuery("#firstp").on("click mouseover foo bar baz", handler) .trigger("click").trigger("mouseover") .trigger("foo").trigger("bar") .trigger("baz"); equal( count, 5, "on() five events at once" ); }); test("on(), multiple events at once and namespaces", function() { expect(7); var cur, div, obj = {}; div = jQuery("
").on("focusin.a", function(e) { equal( e.type, cur, "Verify right single event was fired." ); }); cur = "focusin"; div.trigger("focusin.a"); // manually clean up detached elements div.remove(); div = jQuery("
").on("click mouseover", obj, function(e) { equal( e.type, cur, "Verify right multi event was fired." ); equal( e.data, obj, "Make sure the data came in correctly." ); }); cur = "click"; div.trigger("click"); cur = "mouseover"; div.trigger("mouseover"); // manually clean up detached elements div.remove(); div = jQuery("
").on("focusin.a focusout.b", function(e) { equal( e.type, cur, "Verify right multi event was fired." ); }); cur = "focusin"; div.trigger("focusin.a"); cur = "focusout"; div.trigger("focusout.b"); // manually clean up detached elements div.remove(); }); test("on(), namespace with special add", function() { expect(27); var i = 0, div = jQuery("
").appendTo("#qunit-fixture").on( "test", function() { ok( true, "Test event fired." ); }); jQuery.event.special["test"] = { _default: function( e, data ) { equal( e.type, "test", "Make sure we're dealing with a test event." ); ok( data, "And that trigger data was passed." ); strictEqual( e.target, div[0], "And that the target is correct." ); equal( this, window, "And that the context is correct." ); }, setup: function() {}, teardown: function() { ok( true, "Teardown called." ); }, add: function( handleObj ) { var handler = handleObj.handler; handleObj.handler = function( e ) { e.xyz = ++i; handler.apply( this, arguments ); }; }, remove: function() { ok( true, "Remove called." ); } }; div.on( "test.a", { x: 1 }, function( e ) { ok( !!e.xyz, "Make sure that the data is getting passed through." ); equal( e.data["x"], 1, "Make sure data is attached properly." ); }); div.on( "test.b", { x: 2 }, function( e ) { ok( !!e.xyz, "Make sure that the data is getting passed through." ); equal( e.data["x"], 2, "Make sure data is attached properly." ); }); // Should trigger 5 div.trigger( "test", 33.33 ); // Should trigger 2 div.trigger( "test.a", "George Harrison" ); // Should trigger 2 div.trigger( "test.b", { year: 1982 } ); // Should trigger 4 div.off("test"); div = jQuery("
").on( "test", function() { ok( true, "Test event fired." ); }); // Should trigger 2 div.appendTo("#qunit-fixture").remove(); delete jQuery.event.special["test"]; }); test("on(), no data", function() { expect(1); var handler = function(event) { ok ( !event.data, "Check that no data is added to the event object" ); }; jQuery("#firstp").on("click", handler).trigger("click"); }); test("on/one/off(Object)", function(){ expect(6); var $elem, clickCounter = 0, mouseoverCounter = 0; function handler(event) { if (event.type === "click") { clickCounter++; } else if (event.type === "mouseover") { mouseoverCounter++; } } function handlerWithData(event) { if (event.type === "click") { clickCounter += event.data; } else if (event.type === "mouseover") { mouseoverCounter += event.data; } } function trigger(){ $elem.trigger("click").trigger("mouseover"); } $elem = jQuery("#firstp") // Regular bind .on({ "click":handler, "mouseover":handler }) // Bind with data .one({ "click":handlerWithData, "mouseover":handlerWithData }, 2 ); trigger(); equal( clickCounter, 3, "on(Object)" ); equal( mouseoverCounter, 3, "on(Object)" ); trigger(); equal( clickCounter, 4, "on(Object)" ); equal( mouseoverCounter, 4, "on(Object)" ); jQuery("#firstp").off({ "click":handler, "mouseover":handler }); trigger(); equal( clickCounter, 4, "on(Object)" ); equal( mouseoverCounter, 4, "on(Object)" ); }); test("on/off(Object), on/off(Object, String)", function() { expect(6); var events, clickCounter = 0, mouseoverCounter = 0, $p = jQuery("#firstp"), $a = $p.find("a").eq(0); events = { "click": function( event ) { clickCounter += ( event.data || 1 ); }, "mouseover": function( event ) { mouseoverCounter += ( event.data || 1 ); } }; function trigger() { $a.trigger("click").trigger("mouseover"); } jQuery( document ).on( events, "#firstp a" ); $p.on( events, "a", 2 ); trigger(); equal( clickCounter, 3, "on" ); equal( mouseoverCounter, 3, "on" ); $p.off( events, "a" ); trigger(); equal( clickCounter, 4, "off" ); equal( mouseoverCounter, 4, "off" ); jQuery( document ).off( events, "#firstp a" ); trigger(); equal( clickCounter, 4, "off" ); equal( mouseoverCounter, 4, "off" ); }); test("on immediate propagation", function() { expect(2); var lastClick, $p = jQuery("#firstp"), $a = $p.find("a").eq(0); lastClick = ""; jQuery( document ).on( "click", "#firstp a", function(e) { lastClick = "click1"; e.stopImmediatePropagation(); }); jQuery( document ).on( "click", "#firstp a", function() { lastClick = "click2"; }); $a.trigger( "click" ); equal( lastClick, "click1", "on stopImmediatePropagation" ); jQuery( document ).off( "click", "#firstp a" ); lastClick = ""; $p.on( "click", "a", function(e) { lastClick = "click1"; e.stopImmediatePropagation(); }); $p.on( "click", "a", function() { lastClick = "click2"; }); $a.trigger( "click" ); equal( lastClick, "click1", "on stopImmediatePropagation" ); $p.off( "click", "**" ); }); test("on bubbling, isDefaultPrevented, stopImmediatePropagation", function() { expect( 3 ); var $anchor2 = jQuery( "#anchor2" ), $main = jQuery( "#qunit-fixture" ), neverCallMe = function() { ok( false, "immediate propagation should have been stopped" ); }, fakeClick = function($jq) { // Use a native click so we don't get jQuery simulated bubbling var e = document.createEvent( "MouseEvents" ); e.initEvent( "click", true, true ); $jq[ 0 ].dispatchEvent( e ); }; $anchor2.on( "click", function(e) { e.preventDefault(); }); $main.on( "click", "#foo", function( e ) { equal( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" ); }); fakeClick( $anchor2 ); $anchor2.off( "click" ); $main.off( "click", "**" ); $anchor2.on( "click", function() { // Let the default action occur }); $main.on("click", "#foo", function(e) { equal( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" ); }); fakeClick( $anchor2 ); $anchor2.off( "click" ); $main.off( "click", "**" ); // Android 2.3 doesn't support stopImmediatePropagation; jQuery fallbacks to stopPropagation // in such a case. // Support: Android 2.3 if ( /android 2\.3/i.test( navigator.userAgent ) ) { ok( true, "Android 2.3, skipping native stopImmediatePropagation check" ); } else { $anchor2.on( "click", function( e ) { e.stopImmediatePropagation(); ok( true, "anchor was clicked and prop stopped" ); }); $anchor2[0].addEventListener( "click", neverCallMe, false ); fakeClick( $anchor2 ); $anchor2[0].removeEventListener( "click", neverCallMe ); } }); test("on(), iframes", function() { expect( 1 ); // events don't work with iframes, see #939 - this test fails in IE because of contentDocument var doc = jQuery("#loadediframe").contents(); jQuery("div", doc).on("click", function() { ok( true, "Binding to element inside iframe" ); }).trigger("click").off("click"); }); test("on(), trigger change on select", function() { expect(5); var counter = 0; function selectOnChange(event) { equal( event.data, counter++, "Event.data is not a global event object" ); } jQuery("#form select").each(function(i){ jQuery(this).on("change", i, selectOnChange); }).trigger("change"); }); test("on(), namespaced events, cloned events", 18, function() { var firstp = jQuery( "#firstp" ); firstp.on("custom.test",function(){ ok(false, "Custom event triggered"); }); firstp.on("click",function(e){ ok(true, "Normal click triggered"); equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" ); }); firstp.on("click.test",function(e){ var check = "click"; ok( true, "Namespaced click triggered" ); if ( e.namespace ) { check += "test"; } equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" ); }); //clone(true) element to verify events are cloned correctly firstp = firstp.add( firstp.clone( true ).attr( "id", "firstp2" ).insertBefore( firstp ) ); // Trigger both bound fn (8) firstp.trigger("click"); // Trigger one bound fn (4) firstp.trigger("click.test"); // Remove only the one fn firstp.off("click.test"); // Trigger the remaining fn (4) firstp.trigger("click"); // Remove the remaining namespaced fn firstp.off(".test"); // Try triggering the custom event (0) firstp.trigger("custom"); // using contents will get comments regular, text, and comment nodes jQuery("#nonnodes").contents().on("tester", function () { equal(this.nodeType, 1, "Check node,textnode,comment on just does real nodes" ); }).trigger("tester"); // Make sure events stick with appendTo'd elements (which are cloned) #2027 jQuery("test").on( "click", function(){ return false; }).appendTo("#qunit-fixture"); ok( jQuery("a.test").eq(0).triggerHandler("click") === false, "Handler is bound to appendTo'd elements" ); }); test("on(), multi-namespaced events", function() { expect(6); var order = [ "click.test.abc", "click.test.abc", "click.test", "click.test.abc", "click.test", "custom.test2" ]; function check(name, msg){ deepEqual( name, order.shift(), msg ); } jQuery("#firstp").on("custom.test",function() { check("custom.test", "Custom event triggered"); }); jQuery("#firstp").on("custom.test2",function() { check("custom.test2", "Custom event triggered"); }); jQuery("#firstp").on("click.test",function() { check("click.test", "Normal click triggered"); }); jQuery("#firstp").on("click.test.abc",function() { check("click.test.abc", "Namespaced click triggered"); }); // Those would not trigger/off (#5303) jQuery("#firstp").trigger("click.a.test"); jQuery("#firstp").off("click.a.test"); // Trigger both bound fn (1) jQuery("#firstp").trigger("click.test.abc"); // Trigger one bound fn (1) jQuery("#firstp").trigger("click.abc"); // Trigger two bound fn (2) jQuery("#firstp").trigger("click.test"); // Remove only the one fn jQuery("#firstp").off("click.abc"); // Trigger the remaining fn (1) jQuery("#firstp").trigger("click"); // Remove the remaining fn jQuery("#firstp").off(".test"); // Trigger the remaining fn (1) jQuery("#firstp").trigger("custom"); }); test("namespace-only event binding is a no-op", function(){ expect(2); jQuery("#firstp") .on( ".whoops", function() { ok( false, "called a namespace-only event" ); }) .on( "whoops", function() { ok( true, "called whoops" ); }) .trigger("whoops") // 1 .off(".whoops") .trigger("whoops") // 2 .off("whoops"); }); test("on(), with same function", function() { expect(2); var count = 0, func = function(){ count++; }; jQuery("#liveHandlerOrder").on("foo.bar", func).on("foo.zar", func); jQuery("#liveHandlerOrder").trigger("foo.bar"); equal(count, 1, "Verify binding function with multiple namespaces." ); jQuery("#liveHandlerOrder").off("foo.bar", func).off("foo.zar", func); jQuery("#liveHandlerOrder").trigger("foo.bar"); equal(count, 1, "Verify that removing events still work." ); }); test("on(), make sure order is maintained", function() { expect(1); var elem = jQuery("#firstp"), log = [], check = []; jQuery.each( new Array(100), function( i ) { elem.on( "click", function(){ log.push( i ); }); check.push( i ); }); elem.trigger("click"); equal( log.join(","), check.join(","), "Make sure order was maintained." ); elem.off("click"); }); test("on(), with different this object", function() { expect(4); var thisObject = { myThis: true }, data = { myData: true }, handler1 = function() { equal( this, thisObject, "on() with different this object" ); }, handler2 = function( event ) { equal( this, thisObject, "on() with different this object and data" ); equal( event.data, data, "on() with different this object and data" ); }; jQuery("#firstp") .on("click", jQuery.proxy(handler1, thisObject)).trigger("click").off("click", handler1) .on("click", data, jQuery.proxy(handler2, thisObject)).trigger("click").off("click", handler2); ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." ); }); test("on(name, false), off(name, false)", function() { expect(3); var main = 0; jQuery("#qunit-fixture").on("click", function(){ main++; }); jQuery("#ap").trigger("click"); equal( main, 1, "Verify that the trigger happened correctly." ); main = 0; jQuery("#ap").on("click", false); jQuery("#ap").trigger("click"); equal( main, 0, "Verify that no bubble happened." ); main = 0; jQuery("#ap").off("click", false); jQuery("#ap").trigger("click"); equal( main, 1, "Verify that the trigger happened correctly." ); // manually clean up events from elements outside the fixture jQuery("#qunit-fixture").off("click"); }); test("on(name, selector, false), off(name, selector, false)", function() { expect(3); var main = 0; jQuery("#qunit-fixture").on("click", "#ap", function(){ main++; }); jQuery("#ap").trigger("click"); equal( main, 1, "Verify that the trigger happened correctly." ); main = 0; jQuery("#ap").on("click", "#groups", false); jQuery("#groups").trigger("click"); equal( main, 0, "Verify that no bubble happened." ); main = 0; jQuery("#ap").off("click", "#groups", false); jQuery("#groups").trigger("click"); equal( main, 1, "Verify that the trigger happened correctly." ); jQuery("#qunit-fixture").off("click", "#ap"); }); test("on()/trigger()/off() on plain object", function() { expect( 7 ); var events, obj = {}; // Make sure it doesn't complain when no events are found jQuery(obj).trigger("test"); // Make sure it doesn't complain when no events are found jQuery(obj).off("test"); jQuery(obj).on({ "test": function() { ok( true, "Custom event run." ); }, "submit": function() { ok( true, "Custom submit event run." ); } }); events = jQuery._data(obj, "events"); ok( events, "Object has events bound." ); equal( obj["events"], undefined, "Events object on plain objects is not events" ); equal( obj["test"], undefined, "Make sure that test event is not on the plain object." ); equal( obj["handle"], undefined, "Make sure that the event handler is not on the plain object." ); // Should trigger 1 jQuery(obj).trigger("test"); jQuery(obj).trigger("submit"); jQuery(obj).off("test"); jQuery(obj).off("submit"); // Should trigger 0 jQuery(obj).trigger("test"); // Make sure it doesn't complain when no events are found jQuery(obj).off("test"); equal( obj && obj[ jQuery.expando ] && obj[ jQuery.expando ][ jQuery.expando ] && obj[ jQuery.expando ][ jQuery.expando ]["events"], undefined, "Make sure events object is removed" ); }); test("off(type)", function() { expect( 1 ); var message, func, $elem = jQuery("#firstp"); function error(){ ok( false, message ); } message = "unbind passing function"; $elem.on("error1", error).off("error1", error).triggerHandler("error1"); message = "unbind all from event"; $elem.on("error1", error).off("error1").triggerHandler("error1"); message = "unbind all"; $elem.on("error1", error).off().triggerHandler("error1"); message = "unbind many with function"; $elem.on("error1 error2",error) .off("error1 error2", error ) .trigger("error1").triggerHandler("error2"); message = "unbind many"; // #3538 $elem.on("error1 error2", error) .off("error1 error2") .trigger("error1").triggerHandler("error2"); message = "unbind without a type or handler"; $elem.on("error1 error2.test",error) .off() .trigger("error1").triggerHandler("error2"); // Should only unbind the specified function jQuery( document ).on( "click", function(){ ok( true, "called handler after selective removal"); }); func = function() {}; jQuery( document ) .on( "click", func ) .off( "click", func ) .trigger("click") .off( "click" ); }); test("off(eventObject)", function() { expect(4); var $elem = jQuery("#firstp"), num; function assert( expected ){ num = 0; $elem.trigger("foo").triggerHandler("bar"); equal( num, expected, "Check the right handlers are triggered" ); } $elem // This handler shouldn't be unbound .on("foo", function(){ num += 1; }) .on("foo", function(e){ $elem.off( e ); num += 2; }) // Neither this one .on("bar", function(){ num += 4; }); assert( 7 ); assert( 5 ); $elem.off("bar"); assert( 1 ); $elem.off(); assert( 0 ); }); if ( jQuery.fn.hover ) { test("hover() mouseenter mouseleave", function() { expect(1); var times = 0, handler1 = function() { ++times; }, handler2 = function() { ++times; }; jQuery("#firstp") .hover(handler1, handler2) .mouseenter().mouseleave() .off("mouseenter", handler1) .off("mouseleave", handler2) .hover(handler1) .mouseenter().mouseleave() .off("mouseenter mouseleave", handler1) .mouseenter().mouseleave(); equal( times, 4, "hover handlers fired" ); }); } test("mouseover triggers mouseenter", function() { expect(1); var count = 0, elem = jQuery(""); elem.on( "mouseenter", function () { count++; }); elem.trigger("mouseover"); equal(count, 1, "make sure mouseover triggers a mouseenter" ); elem.remove(); }); test("pointerover triggers pointerenter", function() { expect(1); var count = 0, elem = jQuery(""); elem.on( "pointerenter", function () { count++; }); elem.trigger("pointerover"); equal(count, 1, "make sure pointerover triggers a pointerenter" ); elem.remove(); }); test("withinElement implemented with jQuery.contains()", function() { expect(1); jQuery("#qunit-fixture").append("
"); jQuery("#jc-outer").on("mouseenter mouseleave", function( event ) { equal( this.id, "jc-outer", this.id + " " + event.type ); }).trigger("mouseenter"); jQuery("#jc-inner").trigger("mousenter"); jQuery("#jc-outer").off("mouseenter mouseleave").remove(); jQuery("#jc-inner").remove(); }); test("mouseenter, mouseleave don't catch exceptions", function() { expect(2); var elem = jQuery("#firstp").on( "mouseenter mouseleave", function() { throw "an Exception"; }); try { elem.trigger("mouseenter"); } catch (e) { equal( e, "an Exception", "mouseenter doesn't catch exceptions" ); } try { elem.trigger("mouseleave"); } catch (e) { equal( e, "an Exception", "mouseleave doesn't catch exceptions" ); } }); if ( jQuery.fn.click ) { test("trigger() shortcuts", function() { expect(6); var counter, clickCounter, elem = jQuery("
  • Change location
  • ").prependTo("#firstUL"); elem.find("a").on("click", function() { var close = jQuery("spanx", this); // same with jQuery(this).find("span"); equal( close.length, 0, "Context element does not exist, length must be zero" ); ok( !close[0], "Context element does not exist, direct access to element must return undefined" ); return false; }).click(); // manually clean up detached elements elem.remove(); jQuery("#check1").click(function() { ok( true, "click event handler for checkbox gets fired twice, see #815" ); }).click(); counter = 0; jQuery("#firstp")[0].onclick = function() { counter++; }; jQuery("#firstp").click(); equal( counter, 1, "Check that click, triggers onclick event handler also" ); clickCounter = 0; jQuery("#simon1")[0].onclick = function() { clickCounter++; }; jQuery("#simon1").click(); equal( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" ); elem = jQuery("").load(function(){ ok( true, "Trigger the load event, using the shortcut .load() (#2819)"); }).load(); // manually clean up detached elements elem.remove(); // test that special handlers do not blow up with VML elements (#7071) jQuery("").appendTo("head"); jQuery(" ").appendTo("#form"); jQuery("#oval").click().keydown(); }); } test("trigger() bubbling", function() { expect(18); var win = 0, doc = 0, html = 0, body = 0, main = 0, ap = 0; jQuery(window).on("click", function(){ win++; }); jQuery(document).on("click", function( e ){ if ( e.target !== document) { doc++; } }); jQuery("html").on("click", function(){ html++; }); jQuery("body").on("click", function(){ body++; }); jQuery("#qunit-fixture").on("click", function(){ main++; }); jQuery("#ap").on("click", function(){ ap++; return false; }); jQuery("html").trigger("click"); equal( win, 1, "HTML bubble" ); equal( doc, 1, "HTML bubble" ); equal( html, 1, "HTML bubble" ); jQuery("body").trigger("click"); equal( win, 2, "Body bubble" ); equal( doc, 2, "Body bubble" ); equal( html, 2, "Body bubble" ); equal( body, 1, "Body bubble" ); jQuery("#qunit-fixture").trigger("click"); equal( win, 3, "Main bubble" ); equal( doc, 3, "Main bubble" ); equal( html, 3, "Main bubble" ); equal( body, 2, "Main bubble" ); equal( main, 1, "Main bubble" ); jQuery("#ap").trigger("click"); equal( doc, 3, "ap bubble" ); equal( html, 3, "ap bubble" ); equal( body, 2, "ap bubble" ); equal( main, 1, "ap bubble" ); equal( ap, 1, "ap bubble" ); jQuery( document ).trigger("click"); equal( win, 4, "doc bubble" ); // manually clean up events from elements outside the fixture jQuery(document).off("click"); jQuery("html, body, #qunit-fixture").off("click"); }); test("trigger(type, [data], [fn])", function() { expect(16); var $elem, pass, form, elem2, handler = function(event, a, b, c) { equal( event.type, "click", "check passed data" ); equal( a, 1, "check passed data" ); equal( b, "2", "check passed data" ); equal( c, "abc", "check passed data" ); return "test"; }; $elem = jQuery("#firstp"); // Simulate a "native" click $elem[0].click = function(){ ok( true, "Native call was triggered" ); }; jQuery( document ).on("mouseenter", "#firstp", function(){ ok( true, "Trigger mouseenter bound by on" ); }); jQuery( document ).on("mouseleave", "#firstp", function(){ ok( true, "Trigger mouseleave bound by on" ); }); $elem.trigger("mouseenter"); $elem.trigger("mouseleave"); jQuery( document ).off( "mouseenter mouseleave", "#firstp"); // Triggers handlers and native // Trigger 5 $elem.on("click", handler).trigger("click", [1, "2", "abc"]); // Simulate a "native" click $elem[0].click = function(){ ok( false, "Native call was triggered" ); }; // Trigger only the handlers (no native) // Triggers 5 equal( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" ); pass = true; try { elem2 = jQuery("#form input").eq(0); elem2.get(0).style.display = "none"; elem2.trigger("focus"); } catch( e ) { pass = false; } ok( pass, "Trigger focus on hidden element" ); pass = true; try { jQuery("#qunit-fixture table").eq(0).on("test:test", function(){}).trigger("test:test"); } catch ( e ) { pass = false; } ok( pass, "Trigger on a table with a colon in the even type, see #3533" ); form = jQuery("
    ").appendTo("body"); // Make sure it can be prevented locally form.on( "submit", function(){ ok( true, "Local `on` still works." ); return false; }); // Trigger 1 form.trigger("submit"); form.off("submit"); jQuery(document).on( "submit", function(){ ok( true, "Make sure bubble works up to document." ); return false; }); // Trigger 1 form.trigger("submit"); jQuery(document).off("submit"); form.remove(); }); test( "submit event bubbles on copied forms (#11649)", function() { expect( 3 ); var $formByClone, $formByHTML, $testForm = jQuery("#testForm"), $fixture = jQuery("#qunit-fixture"), $wrapperDiv = jQuery("
    ").appendTo( $fixture ); function noSubmit( e ) { e.preventDefault(); } function delegatedSubmit() { ok( true, "Make sure submit event bubbles up." ); return false; } // Attach a delegated submit handler to the parent element $fixture.on( "submit", "form", delegatedSubmit ); // Trigger form submission to introduce the _submit_attached property $testForm.on( "submit", noSubmit ).find("input[name=sub1]").trigger("click"); // Copy the form via .clone() and .html() $formByClone = $testForm.clone( true, true ).removeAttr("id"); $formByHTML = jQuery( jQuery.parseHTML($fixture.html()) ).filter("#testForm").removeAttr("id"); $wrapperDiv.append( $formByClone, $formByHTML ); // Check submit bubbling on the copied forms $wrapperDiv.find("form").on( "submit", noSubmit ).find("input[name=sub1]").trigger("click"); // Clean up $wrapperDiv.remove(); $fixture.off( "submit", "form", delegatedSubmit ); $testForm.off( "submit", noSubmit ); }); test( "change event bubbles on copied forms (#11796)", function(){ expect( 3 ); var $formByClone, $formByHTML, $form = jQuery("#form"), $fixture = jQuery("#qunit-fixture"), $wrapperDiv = jQuery("
    ").appendTo( $fixture ); function delegatedChange() { ok( true, "Make sure change event bubbles up." ); return false; } // Attach a delegated change handler to the form $fixture.on( "change", "form", delegatedChange ); // Trigger change event to introduce the _change_attached property $form.find("select[name=select1]").val("1").trigger("change"); // Copy the form via .clone() and .html() $formByClone = $form.clone( true, true ).removeAttr("id"); $formByHTML = jQuery( jQuery.parseHTML($fixture.html()) ).filter("#form").removeAttr("id"); $wrapperDiv.append( $formByClone, $formByHTML ); // Check change bubbling on the copied forms $wrapperDiv.find("form select[name=select1]").val("2").trigger("change"); // Clean up $wrapperDiv.remove(); $fixture.off( "change", "form", delegatedChange ); }); test("trigger(eventObject, [data], [fn])", function() { expect(28); var event, $parent = jQuery("
    ").appendTo("body"), $child = jQuery("

    foo

    ").appendTo( $parent ); $parent.get( 0 ).style.display = "none"; event = jQuery.Event("noNew"); ok( event !== window, "Instantiate jQuery.Event without the 'new' keyword" ); equal( event.type, "noNew", "Verify its type" ); equal( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" ); equal( event.isPropagationStopped(), false, "Verify isPropagationStopped" ); equal( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" ); event.preventDefault(); equal( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" ); event.stopPropagation(); equal( event.isPropagationStopped(), true, "Verify isPropagationStopped" ); event.isPropagationStopped = function(){ return false; }; event.stopImmediatePropagation(); equal( event.isPropagationStopped(), true, "Verify isPropagationStopped" ); equal( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" ); $parent.on("foo",function( e ) { // Tries bubbling equal( e.type, "foo", "Verify event type when passed passing an event object" ); equal( e.target.id, "child", "Verify event.target when passed passing an event object" ); equal( e.currentTarget.id, "par", "Verify event.currentTarget when passed passing an event object" ); equal( e.secret, "boo!", "Verify event object's custom attribute when passed passing an event object" ); }); // test with an event object event = new jQuery.Event("foo"); event.secret = "boo!"; $child.trigger(event); // test with a literal object $child.trigger({"type": "foo", "secret": "boo!"}); $parent.off(); function error(){ ok( false, "This assertion shouldn't be reached"); } $parent.on("foo", error ); $child.on("foo",function(e, a, b, c ){ equal( arguments.length, 4, "Check arguments length"); equal( a, 1, "Check first custom argument"); equal( b, 2, "Check second custom argument"); equal( c, 3, "Check third custom argument"); equal( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" ); equal( e.isPropagationStopped(), false, "Verify isPropagationStopped" ); equal( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" ); // Skips both errors e.stopImmediatePropagation(); return "result"; }); // We should add this back in when we want to test the order // in which event handlers are iterated. //$child.on("foo", error ); event = new jQuery.Event("foo"); $child.trigger( event, [1,2,3] ).off(); equal( event.result, "result", "Check event.result attribute"); // Will error if it bubbles $child.triggerHandler("foo"); $child.off(); $parent.off().remove(); // Ensure triggerHandler doesn't molest its event object (#xxx) event = jQuery.Event( "zowie" ); jQuery( document ).triggerHandler( event ); equal( event.type, "zowie", "Verify its type" ); equal( event.isPropagationStopped(), false, "propagation not stopped" ); equal( event.isDefaultPrevented(), false, "default not prevented" ); }); test(".trigger() bubbling on disconnected elements (#10489)", function() { expect(2); jQuery( window ).on( "click", function(){ ok( false, "click fired on window" ); }); jQuery( "

    hi

    " ) .on( "click", function() { ok( true, "click fired on div" ); }) .find( "p" ) .on( "click", function() { ok( true, "click fired on p" ); }) .trigger("click") .off( "click" ) .end() .off( "click" ) .remove(); jQuery( window ).off( "click" ); }); test(".trigger() doesn't bubble load event (#10717)", function() { expect(1); jQuery( window ).on( "load", function(){ ok( false, "load fired on window" ); }); // It's not an image, but as long as it fires load... jQuery("") .appendTo( "body" ) .on( "load", function() { ok( true, "load fired on img" ); }) .trigger( "load" ) .remove(); jQuery( window ).off( "load" ); }); test("Delegated events in SVG (#10791; #13180)", function() { expect(2); var useElem, e, svg = jQuery( "" + "" + "" + "" + "" + "" ); jQuery("#qunit-fixture") .append( svg ) .on( "click", "#svg-by-id", function() { ok( true, "delegated id selector" ); }) .on( "click", "[class~='svg-by-class']", function() { ok( true, "delegated class selector" ); }) .find( "#svg-by-id, [class~='svg-by-class']" ) .trigger("click") .end(); // Fire a native click on an SVGElementInstance (the instance tree of an SVG ) // to confirm that it doesn't break our event delegation handling (#13180) useElem = svg.find("#use")[0]; if ( document.createEvent && useElem && useElem.instanceRoot ) { e = document.createEvent("MouseEvents"); e.initEvent( "click", true, true ); useElem.instanceRoot.dispatchEvent( e ); } jQuery("#qunit-fixture").off("click"); }); test("Delegated events in forms (#10844; #11145; #8165; #11382, #11764)", function() { expect(5); // Alias names like "id" cause havoc var form = jQuery( "
    " + "" + "
    " ) .on( "submit", function( event ) { event.preventDefault(); }) .appendTo("body"); jQuery("body") .on( "submit", "#myform", function() { ok( true, "delegated id selector with aliased id" ); }) .find("#myform") .trigger("submit") .end() .off("submit"); form.append(""); jQuery("body") .on( "submit", "#myform", function() { ok( true, "delegated id selector with aliased disabled" ); }) .find("#myform") .trigger("submit") .end() .off("submit"); form .append( "" ) .on( "click", "#nestyDisabledBtn", function() { ok( true, "click on enabled/disabled button with nesty elements" ); }) .on( "mouseover", "#nestyDisabledBtn", function() { ok( true, "mouse on enabled/disabled button with nesty elements" ); }) .find( "span" ) .trigger( "click" ) // yep .trigger( "mouseover" ) // yep .end() .find( "#nestyDisabledBtn" ).prop( "disabled", true ).end() .find( "span" ) .trigger( "click" ) // nope .trigger( "mouseover" ) // yep .end() .off( "click" ); form.remove(); }); test("Submit event can be stopped (#11049)", function() { expect(1); // Since we manually bubble in IE, make sure inner handlers get a chance to cancel var form = jQuery( "
    " + "" + "" + "
    " ) .appendTo("body"); jQuery( "body" ) .on( "submit", function() { ok( true, "submit bubbled on first handler" ); return false; }) .find( "#myform input[type=submit]" ) .each( function(){ this.click(); } ) .end() .on( "submit", function() { ok( false, "submit bubbled on second handler" ); return false; }) .find( "#myform input[type=submit]" ) .each( function(){ jQuery( this.form ).on( "submit", function( e ) { e.preventDefault(); e.stopPropagation(); }); this.click(); }) .end() .off( "submit" ); form.remove(); }); // Test beforeunload event only if it supported. // Support: iOS 7+, Android<4.0 // iOS & old Android have the window.onbeforeunload field but don't support the beforeunload // handler making it impossible to feature-detect the support. if ( window.onbeforeunload === null && !/(ipad|iphone|ipod|android 2\.3)/i.test( navigator.userAgent ) ) { asyncTest("on(beforeunload)", 1, function() { var iframe = jQuery(jQuery.parseHTML("