5 teardown: moduleTeardown
8 test("on() with non-null,defined data", function() {
12 var handler = function( event, data ) {
13 equal( data, 0, "non-null, defined data (zero) is correctly passed" );
16 jQuery("#foo").on("foo.on", handler);
17 jQuery("div").on("foo.delegate", "#foo", handler);
19 jQuery("#foo").trigger("foo", 0);
21 jQuery("#foo").off("foo.on", handler);
22 jQuery("div").off("foo.delegate", "#foo");
26 test("Handler changes and .trigger() order", function() {
30 "<div><div><p><span><b class=\"a\">b</b></span></p></div></div>"
35 .find( "*" ).addBack().on( "click", function() {
36 path += this.nodeName.toLowerCase() + " ";
38 .filter( "b" ).on( "click", function( e ) {
39 // Removing span should not stop propagation to original parents
40 if ( e.target === this ) {
41 jQuery(this).parent().remove();
45 markup.find( "b" ).trigger( "click" );
47 equal( path, "b p div div ", "Delivered all events" );
52 test("on(), with data", function() {
54 var test, handler, handler2;
56 handler = function(event) {
57 ok( event.data, "on() with data, check passed data exists" );
58 equal( event.data["foo"], "bar", "on() with data, Check value of passed data" );
60 jQuery("#firstp").on("click", {"foo": "bar"}, handler).trigger("click").off("click", handler);
62 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
65 handler2 = function(event) {
66 equal( event.data, test, "on() with function data, Check value of passed data" );
68 jQuery("#firstp").on("click", test, handler2).trigger("click").off("click", handler2);
71 test("click(), with data", function() {
73 var handler = function(event) {
74 ok( event.data, "on() with data, check passed data exists" );
75 equal( event.data["foo"], "bar", "on() with data, Check value of passed data" );
77 jQuery("#firstp").on( "click", {"foo": "bar"}, handler).trigger("click").off("click", handler);
79 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
82 test("on(), with data, trigger with data", function() {
84 var handler = function(event, data) {
85 ok( event.data, "check passed data exists" );
86 equal( event.data.foo, "bar", "Check value of passed data" );
87 ok( data, "Check trigger data" );
88 equal( data.bar, "foo", "Check value of trigger data" );
90 jQuery("#firstp").on("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).off("click", handler);
93 test("on(), multiple events at once", function() {
98 handler = function(event) {
99 if (event.type === "click") {
102 else if (event.type === "mouseover") {
103 mouseoverCounter += 1;
107 jQuery("#firstp").on("click mouseover", handler).trigger("click").trigger("mouseover");
108 equal( clickCounter, 1, "on() with multiple events at once" );
109 equal( mouseoverCounter, 1, "on() with multiple events at once" );
112 test("on(), five events at once", function() {
116 handler = function() {
120 jQuery("#firstp").on("click mouseover foo bar baz", handler)
121 .trigger("click").trigger("mouseover")
122 .trigger("foo").trigger("bar")
125 equal( count, 5, "on() five events at once" );
128 test("on(), multiple events at once and namespaces", function() {
134 div = jQuery("<div/>").on("focusin.a", function(e) {
135 equal( e.type, cur, "Verify right single event was fired." );
139 div.trigger("focusin.a");
141 // manually clean up detached elements
144 div = jQuery("<div/>").on("click mouseover", obj, function(e) {
145 equal( e.type, cur, "Verify right multi event was fired." );
146 equal( e.data, obj, "Make sure the data came in correctly." );
150 div.trigger("click");
153 div.trigger("mouseover");
155 // manually clean up detached elements
158 div = jQuery("<div/>").on("focusin.a focusout.b", function(e) {
159 equal( e.type, cur, "Verify right multi event was fired." );
163 div.trigger("focusin.a");
166 div.trigger("focusout.b");
168 // manually clean up detached elements
172 test("on(), namespace with special add", function() {
176 div = jQuery("<div/>").appendTo("#qunit-fixture").on( "test", function() {
177 ok( true, "Test event fired." );
180 jQuery.event.special["test"] = {
181 _default: function( e, data ) {
182 equal( e.type, "test", "Make sure we're dealing with a test event." );
183 ok( data, "And that trigger data was passed." );
184 strictEqual( e.target, div[0], "And that the target is correct." );
185 equal( this, window, "And that the context is correct." );
187 setup: function() {},
188 teardown: function() {
189 ok( true, "Teardown called." );
191 add: function( handleObj ) {
192 var handler = handleObj.handler;
193 handleObj.handler = function( e ) {
195 handler.apply( this, arguments );
199 ok( true, "Remove called." );
203 div.on( "test.a", { x: 1 }, function( e ) {
204 ok( !!e.xyz, "Make sure that the data is getting passed through." );
205 equal( e.data["x"], 1, "Make sure data is attached properly." );
208 div.on( "test.b", { x: 2 }, function( e ) {
209 ok( !!e.xyz, "Make sure that the data is getting passed through." );
210 equal( e.data["x"], 2, "Make sure data is attached properly." );
214 div.trigger( "test", 33.33 );
217 div.trigger( "test.a", "George Harrison" );
220 div.trigger( "test.b", { year: 1982 } );
225 div = jQuery("<div/>").on( "test", function() {
226 ok( true, "Test event fired." );
230 div.appendTo("#qunit-fixture").remove();
232 delete jQuery.event.special["test"];
235 test("on(), no data", function() {
237 var handler = function(event) {
238 ok ( !event.data, "Check that no data is added to the event object" );
240 jQuery("#firstp").on("click", handler).trigger("click");
243 test("on/one/off(Object)", function(){
248 mouseoverCounter = 0;
250 function handler(event) {
251 if (event.type === "click") {
253 } else if (event.type === "mouseover") {
258 function handlerWithData(event) {
259 if (event.type === "click") {
260 clickCounter += event.data;
261 } else if (event.type === "mouseover") {
262 mouseoverCounter += event.data;
267 $elem.trigger("click").trigger("mouseover");
270 $elem = jQuery("#firstp")
278 "click":handlerWithData,
279 "mouseover":handlerWithData
284 equal( clickCounter, 3, "on(Object)" );
285 equal( mouseoverCounter, 3, "on(Object)" );
288 equal( clickCounter, 4, "on(Object)" );
289 equal( mouseoverCounter, 4, "on(Object)" );
291 jQuery("#firstp").off({
297 equal( clickCounter, 4, "on(Object)" );
298 equal( mouseoverCounter, 4, "on(Object)" );
301 test("on/off(Object), on/off(Object, String)", function() {
306 mouseoverCounter = 0,
307 $p = jQuery("#firstp"),
308 $a = $p.find("a").eq(0);
311 "click": function( event ) {
312 clickCounter += ( event.data || 1 );
314 "mouseover": function( event ) {
315 mouseoverCounter += ( event.data || 1 );
320 $a.trigger("click").trigger("mouseover");
323 jQuery( document ).on( events, "#firstp a" );
324 $p.on( events, "a", 2 );
327 equal( clickCounter, 3, "on" );
328 equal( mouseoverCounter, 3, "on" );
330 $p.off( events, "a" );
333 equal( clickCounter, 4, "off" );
334 equal( mouseoverCounter, 4, "off" );
336 jQuery( document ).off( events, "#firstp a" );
339 equal( clickCounter, 4, "off" );
340 equal( mouseoverCounter, 4, "off" );
343 test("on immediate propagation", function() {
347 $p = jQuery("#firstp"),
348 $a = $p.find("a").eq(0);
351 jQuery( document ).on( "click", "#firstp a", function(e) {
352 lastClick = "click1";
353 e.stopImmediatePropagation();
355 jQuery( document ).on( "click", "#firstp a", function() {
356 lastClick = "click2";
358 $a.trigger( "click" );
359 equal( lastClick, "click1", "on stopImmediatePropagation" );
360 jQuery( document ).off( "click", "#firstp a" );
363 $p.on( "click", "a", function(e) {
364 lastClick = "click1";
365 e.stopImmediatePropagation();
367 $p.on( "click", "a", function() {
368 lastClick = "click2";
370 $a.trigger( "click" );
371 equal( lastClick, "click1", "on stopImmediatePropagation" );
372 $p.off( "click", "**" );
375 test("on bubbling, isDefaultPrevented, stopImmediatePropagation", function() {
378 var $anchor2 = jQuery( "#anchor2" ),
379 $main = jQuery( "#qunit-fixture" ),
380 neverCallMe = function() {
381 ok( false, "immediate propagation should have been stopped" );
383 fakeClick = function($jq) {
384 // Use a native click so we don't get jQuery simulated bubbling
385 var e = document.createEvent( "MouseEvents" );
386 e.initEvent( "click", true, true );
387 $jq[ 0 ].dispatchEvent( e );
389 $anchor2.on( "click", function(e) {
392 $main.on( "click", "#foo", function( e ) {
393 equal( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
395 fakeClick( $anchor2 );
396 $anchor2.off( "click" );
397 $main.off( "click", "**" );
398 $anchor2.on( "click", function() {
399 // Let the default action occur
401 $main.on("click", "#foo", function(e) {
402 equal( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
404 fakeClick( $anchor2 );
405 $anchor2.off( "click" );
406 $main.off( "click", "**" );
408 // Android 2.3 doesn't support stopImmediatePropagation; jQuery fallbacks to stopPropagation
410 // Support: Android 2.3
411 if ( /android 2\.3/i.test( navigator.userAgent ) ) {
412 ok( true, "Android 2.3, skipping native stopImmediatePropagation check" );
414 $anchor2.on( "click", function( e ) {
415 e.stopImmediatePropagation();
416 ok( true, "anchor was clicked and prop stopped" );
418 $anchor2[0].addEventListener( "click", neverCallMe, false );
419 fakeClick( $anchor2 );
420 $anchor2[0].removeEventListener( "click", neverCallMe );
424 test("on(), iframes", function() {
427 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
428 var doc = jQuery("#loadediframe").contents();
430 jQuery("div", doc).on("click", function() {
431 ok( true, "Binding to element inside iframe" );
432 }).trigger("click").off("click");
435 test("on(), trigger change on select", function() {
438 function selectOnChange(event) {
439 equal( event.data, counter++, "Event.data is not a global event object" );
441 jQuery("#form select").each(function(i){
442 jQuery(this).on("change", i, selectOnChange);
443 }).trigger("change");
446 test("on(), namespaced events, cloned events", 18, function() {
447 var firstp = jQuery( "#firstp" );
449 firstp.on("custom.test",function(){
450 ok(false, "Custom event triggered");
453 firstp.on("click",function(e){
454 ok(true, "Normal click triggered");
455 equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" );
458 firstp.on("click.test",function(e){
460 ok( true, "Namespaced click triggered" );
464 equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" );
467 //clone(true) element to verify events are cloned correctly
468 firstp = firstp.add( firstp.clone( true ).attr( "id", "firstp2" ).insertBefore( firstp ) );
470 // Trigger both bound fn (8)
471 firstp.trigger("click");
473 // Trigger one bound fn (4)
474 firstp.trigger("click.test");
476 // Remove only the one fn
477 firstp.off("click.test");
479 // Trigger the remaining fn (4)
480 firstp.trigger("click");
482 // Remove the remaining namespaced fn
485 // Try triggering the custom event (0)
486 firstp.trigger("custom");
488 // using contents will get comments regular, text, and comment nodes
489 jQuery("#nonnodes").contents().on("tester", function () {
490 equal(this.nodeType, 1, "Check node,textnode,comment on just does real nodes" );
491 }).trigger("tester");
493 // Make sure events stick with appendTo'd elements (which are cloned) #2027
494 jQuery("<a href='#fail' class='test'>test</a>").on( "click", function(){ return false; }).appendTo("#qunit-fixture");
495 ok( jQuery("a.test").eq(0).triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
498 test("on(), multi-namespaced events", function() {
510 function check(name, msg){
511 deepEqual( name, order.shift(), msg );
514 jQuery("#firstp").on("custom.test",function() {
515 check("custom.test", "Custom event triggered");
518 jQuery("#firstp").on("custom.test2",function() {
519 check("custom.test2", "Custom event triggered");
522 jQuery("#firstp").on("click.test",function() {
523 check("click.test", "Normal click triggered");
526 jQuery("#firstp").on("click.test.abc",function() {
527 check("click.test.abc", "Namespaced click triggered");
530 // Those would not trigger/off (#5303)
531 jQuery("#firstp").trigger("click.a.test");
532 jQuery("#firstp").off("click.a.test");
534 // Trigger both bound fn (1)
535 jQuery("#firstp").trigger("click.test.abc");
537 // Trigger one bound fn (1)
538 jQuery("#firstp").trigger("click.abc");
540 // Trigger two bound fn (2)
541 jQuery("#firstp").trigger("click.test");
543 // Remove only the one fn
544 jQuery("#firstp").off("click.abc");
546 // Trigger the remaining fn (1)
547 jQuery("#firstp").trigger("click");
549 // Remove the remaining fn
550 jQuery("#firstp").off(".test");
552 // Trigger the remaining fn (1)
553 jQuery("#firstp").trigger("custom");
556 test("namespace-only event binding is a no-op", function(){
560 .on( ".whoops", function() {
561 ok( false, "called a namespace-only event" );
563 .on( "whoops", function() {
564 ok( true, "called whoops" );
566 .trigger("whoops") // 1
568 .trigger("whoops") // 2
572 test("Empty namespace is ignored", function(){
576 .on( "meow.", function( e ) {
577 equal( e.namespace, "", "triggered a namespace-less meow event" );
583 test("on(), with same function", function() {
586 var count = 0, func = function(){
590 jQuery("#liveHandlerOrder").on("foo.bar", func).on("foo.zar", func);
591 jQuery("#liveHandlerOrder").trigger("foo.bar");
593 equal(count, 1, "Verify binding function with multiple namespaces." );
595 jQuery("#liveHandlerOrder").off("foo.bar", func).off("foo.zar", func);
596 jQuery("#liveHandlerOrder").trigger("foo.bar");
598 equal(count, 1, "Verify that removing events still work." );
601 test("on(), make sure order is maintained", function() {
604 var elem = jQuery("#firstp"), log = [], check = [];
606 jQuery.each( new Array(100), function( i ) {
607 elem.on( "click", function(){
615 elem.trigger("click");
617 equal( log.join(","), check.join(","), "Make sure order was maintained." );
622 test("on(), with different this object", function() {
624 var thisObject = { myThis: true },
625 data = { myData: true },
626 handler1 = function() {
627 equal( this, thisObject, "on() with different this object" );
629 handler2 = function( event ) {
630 equal( this, thisObject, "on() with different this object and data" );
631 equal( event.data, data, "on() with different this object and data" );
635 .on("click", jQuery.proxy(handler1, thisObject)).trigger("click").off("click", handler1)
636 .on("click", data, jQuery.proxy(handler2, thisObject)).trigger("click").off("click", handler2);
638 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
641 test("on(name, false), off(name, false)", function() {
645 jQuery("#qunit-fixture").on("click", function(){ main++; });
646 jQuery("#ap").trigger("click");
647 equal( main, 1, "Verify that the trigger happened correctly." );
650 jQuery("#ap").on("click", false);
651 jQuery("#ap").trigger("click");
652 equal( main, 0, "Verify that no bubble happened." );
655 jQuery("#ap").off("click", false);
656 jQuery("#ap").trigger("click");
657 equal( main, 1, "Verify that the trigger happened correctly." );
659 // manually clean up events from elements outside the fixture
660 jQuery("#qunit-fixture").off("click");
663 test("on(name, selector, false), off(name, selector, false)", function() {
668 jQuery("#qunit-fixture").on("click", "#ap", function(){ main++; });
669 jQuery("#ap").trigger("click");
670 equal( main, 1, "Verify that the trigger happened correctly." );
673 jQuery("#ap").on("click", "#groups", false);
674 jQuery("#groups").trigger("click");
675 equal( main, 0, "Verify that no bubble happened." );
678 jQuery("#ap").off("click", "#groups", false);
679 jQuery("#groups").trigger("click");
680 equal( main, 1, "Verify that the trigger happened correctly." );
681 jQuery("#qunit-fixture").off("click", "#ap");
684 test("on()/trigger()/off() on plain object", function() {
690 // Make sure it doesn't complain when no events are found
691 jQuery(obj).trigger("test");
693 // Make sure it doesn't complain when no events are found
694 jQuery(obj).off("test");
698 ok( true, "Custom event run." );
700 "submit": function() {
701 ok( true, "Custom submit event run." );
705 events = jQuery._data(obj, "events");
706 ok( events, "Object has events bound." );
707 equal( obj["events"], undefined, "Events object on plain objects is not events" );
708 equal( obj["test"], undefined, "Make sure that test event is not on the plain object." );
709 equal( obj["handle"], undefined, "Make sure that the event handler is not on the plain object." );
712 jQuery(obj).trigger("test");
713 jQuery(obj).trigger("submit");
715 jQuery(obj).off("test");
716 jQuery(obj).off("submit");
719 jQuery(obj).trigger("test");
721 // Make sure it doesn't complain when no events are found
722 jQuery(obj).off("test");
724 equal( obj && obj[ jQuery.expando ] &&
725 obj[ jQuery.expando ][ jQuery.expando ] &&
726 obj[ jQuery.expando ][ jQuery.expando ]["events"], undefined, "Make sure events object is removed" );
729 test("off(type)", function() {
733 $elem = jQuery("#firstp");
736 ok( false, message );
739 message = "unbind passing function";
740 $elem.on("error1", error).off("error1", error).triggerHandler("error1");
742 message = "unbind all from event";
743 $elem.on("error1", error).off("error1").triggerHandler("error1");
745 message = "unbind all";
746 $elem.on("error1", error).off().triggerHandler("error1");
748 message = "unbind many with function";
749 $elem.on("error1 error2",error)
750 .off("error1 error2", error )
751 .trigger("error1").triggerHandler("error2");
753 message = "unbind many"; // #3538
754 $elem.on("error1 error2", error)
755 .off("error1 error2")
756 .trigger("error1").triggerHandler("error2");
758 message = "unbind without a type or handler";
759 $elem.on("error1 error2.test",error)
761 .trigger("error1").triggerHandler("error2");
763 // Should only unbind the specified function
764 jQuery( document ).on( "click", function(){
765 ok( true, "called handler after selective removal");
767 func = function() {};
770 .off( "click", func )
775 test("off(eventObject)", function() {
778 var $elem = jQuery("#firstp"),
781 function assert( expected ){
783 $elem.trigger("foo").triggerHandler("bar");
784 equal( num, expected, "Check the right handlers are triggered" );
788 // This handler shouldn't be unbound
789 .on("foo", function(){
792 .on("foo", function(e){
797 .on("bar", function(){
811 if ( jQuery.fn.hover ) {
812 test("hover() mouseenter mouseleave", function() {
816 handler1 = function() { ++times; },
817 handler2 = function() { ++times; };
820 .hover(handler1, handler2)
821 .mouseenter().mouseleave()
822 .off("mouseenter", handler1)
823 .off("mouseleave", handler2)
825 .mouseenter().mouseleave()
826 .off("mouseenter mouseleave", handler1)
827 .mouseenter().mouseleave();
829 equal( times, 4, "hover handlers fired" );
834 test("mouseover triggers mouseenter", function() {
838 elem = jQuery("<a />");
839 elem.on( "mouseenter", function () {
842 elem.trigger("mouseover");
843 equal(count, 1, "make sure mouseover triggers a mouseenter" );
848 test("pointerover triggers pointerenter", function() {
852 elem = jQuery("<a />");
853 elem.on( "pointerenter", function () {
856 elem.trigger("pointerover");
857 equal(count, 1, "make sure pointerover triggers a pointerenter" );
862 test("withinElement implemented with jQuery.contains()", function() {
866 jQuery("#qunit-fixture").append("<div id='jc-outer'><div id='jc-inner'></div></div>");
868 jQuery("#jc-outer").on("mouseenter mouseleave", function( event ) {
870 equal( this.id, "jc-outer", this.id + " " + event.type );
872 }).trigger("mouseenter");
874 jQuery("#jc-inner").trigger("mousenter");
876 jQuery("#jc-outer").off("mouseenter mouseleave").remove();
877 jQuery("#jc-inner").remove();
881 test("mouseenter, mouseleave don't catch exceptions", function() {
884 var elem = jQuery("#firstp").on( "mouseenter mouseleave", function() {
885 throw "an Exception";
889 elem.trigger("mouseenter");
891 equal( e, "an Exception", "mouseenter doesn't catch exceptions" );
895 elem.trigger("mouseleave");
897 equal( e, "an Exception", "mouseleave doesn't catch exceptions" );
901 if ( jQuery.fn.click ) {
903 test("trigger() shortcuts", function() {
906 var counter, clickCounter,
907 elem = jQuery("<li><a href='#'>Change location</a></li>").prependTo("#firstUL");
908 elem.find("a").on("click", function() {
909 var close = jQuery("spanx", this); // same with jQuery(this).find("span");
910 equal( close.length, 0, "Context element does not exist, length must be zero" );
911 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
915 // manually clean up detached elements
918 jQuery("#check1").click(function() {
919 ok( true, "click event handler for checkbox gets fired twice, see #815" );
923 jQuery("#firstp")[0].onclick = function() {
926 jQuery("#firstp").click();
927 equal( counter, 1, "Check that click, triggers onclick event handler also" );
930 jQuery("#simon1")[0].onclick = function() {
933 jQuery("#simon1").click();
934 equal( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
936 elem = jQuery("<img />").load(function(){
937 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
940 // manually clean up detached elements
943 // test that special handlers do not blow up with VML elements (#7071)
944 jQuery("<xml:namespace ns='urn:schemas-microsoft-com:vml' prefix='v' />").appendTo("head");
945 jQuery("<v:oval id='oval' style='width:100pt;height:75pt;' fillcolor='red'> </v:oval>").appendTo("#form");
946 jQuery("#oval").click().keydown();
951 test("trigger() bubbling", function() {
954 var win = 0, doc = 0, html = 0, body = 0, main = 0, ap = 0;
956 jQuery(window).on("click", function(){ win++; });
957 jQuery(document).on("click", function( e ){ if ( e.target !== document) { doc++; } });
958 jQuery("html").on("click", function(){ html++; });
959 jQuery("body").on("click", function(){ body++; });
960 jQuery("#qunit-fixture").on("click", function(){ main++; });
961 jQuery("#ap").on("click", function(){ ap++; return false; });
963 jQuery("html").trigger("click");
964 equal( win, 1, "HTML bubble" );
965 equal( doc, 1, "HTML bubble" );
966 equal( html, 1, "HTML bubble" );
968 jQuery("body").trigger("click");
969 equal( win, 2, "Body bubble" );
970 equal( doc, 2, "Body bubble" );
971 equal( html, 2, "Body bubble" );
972 equal( body, 1, "Body bubble" );
974 jQuery("#qunit-fixture").trigger("click");
975 equal( win, 3, "Main bubble" );
976 equal( doc, 3, "Main bubble" );
977 equal( html, 3, "Main bubble" );
978 equal( body, 2, "Main bubble" );
979 equal( main, 1, "Main bubble" );
981 jQuery("#ap").trigger("click");
982 equal( doc, 3, "ap bubble" );
983 equal( html, 3, "ap bubble" );
984 equal( body, 2, "ap bubble" );
985 equal( main, 1, "ap bubble" );
986 equal( ap, 1, "ap bubble" );
988 jQuery( document ).trigger("click");
989 equal( win, 4, "doc bubble" );
991 // manually clean up events from elements outside the fixture
992 jQuery(window).off("click");
993 jQuery(document).off("click");
994 jQuery("html, body, #qunit-fixture").off("click");
997 test("trigger(type, [data], [fn])", function() {
1000 var $elem, pass, form, elem2,
1001 handler = function(event, a, b, c) {
1002 equal( event.type, "click", "check passed data" );
1003 equal( a, 1, "check passed data" );
1004 equal( b, "2", "check passed data" );
1005 equal( c, "abc", "check passed data" );
1009 $elem = jQuery("#firstp");
1011 // Simulate a "native" click
1012 $elem[0].click = function(){
1013 ok( true, "Native call was triggered" );
1017 jQuery( document ).on("mouseenter", "#firstp", function(){
1018 ok( true, "Trigger mouseenter bound by on" );
1021 jQuery( document ).on("mouseleave", "#firstp", function(){
1022 ok( true, "Trigger mouseleave bound by on" );
1025 $elem.trigger("mouseenter");
1027 $elem.trigger("mouseleave");
1029 jQuery( document ).off( "mouseenter mouseleave", "#firstp");
1031 // Triggers handlers and native
1033 $elem.on("click", handler).trigger("click", [1, "2", "abc"]);
1035 // Simulate a "native" click
1036 $elem[0].click = function(){
1037 ok( false, "Native call was triggered" );
1040 // Trigger only the handlers (no native)
1042 equal( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
1046 elem2 = jQuery("#form input").eq(0);
1047 elem2.get(0).style.display = "none";
1048 elem2.trigger("focus");
1052 ok( pass, "Trigger focus on hidden element" );
1056 jQuery("#qunit-fixture table").eq(0).on("test:test", function(){}).trigger("test:test");
1060 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
1062 form = jQuery("<form action=''></form>").appendTo("body");
1064 // Make sure it can be prevented locally
1065 form.on( "submit", function(){
1066 ok( true, "Local `on` still works." );
1071 form.trigger("submit");
1075 jQuery(document).on( "submit", function(){
1076 ok( true, "Make sure bubble works up to document." );
1081 form.trigger("submit");
1083 jQuery(document).off("submit");
1088 test( "submit event bubbles on copied forms (#11649)", function() {
1091 var $formByClone, $formByHTML,
1092 $testForm = jQuery("#testForm"),
1093 $fixture = jQuery("#qunit-fixture"),
1094 $wrapperDiv = jQuery("<div/>").appendTo( $fixture );
1096 function noSubmit( e ) {
1099 function delegatedSubmit() {
1100 ok( true, "Make sure submit event bubbles up." );
1104 // Attach a delegated submit handler to the parent element
1105 $fixture.on( "submit", "form", delegatedSubmit );
1107 // Trigger form submission to introduce the _submit_attached property
1108 $testForm.on( "submit", noSubmit ).find("input[name=sub1]").trigger("click");
1110 // Copy the form via .clone() and .html()
1111 $formByClone = $testForm.clone( true, true ).removeAttr("id");
1112 $formByHTML = jQuery( jQuery.parseHTML($fixture.html()) ).filter("#testForm").removeAttr("id");
1113 $wrapperDiv.append( $formByClone, $formByHTML );
1115 // Check submit bubbling on the copied forms
1116 $wrapperDiv.find("form").on( "submit", noSubmit ).find("input[name=sub1]").trigger("click");
1119 $wrapperDiv.remove();
1120 $fixture.off( "submit", "form", delegatedSubmit );
1121 $testForm.off( "submit", noSubmit );
1124 test( "change event bubbles on copied forms (#11796)", function(){
1127 var $formByClone, $formByHTML,
1128 $form = jQuery("#form"),
1129 $fixture = jQuery("#qunit-fixture"),
1130 $wrapperDiv = jQuery("<div/>").appendTo( $fixture );
1132 function delegatedChange() {
1133 ok( true, "Make sure change event bubbles up." );
1137 // Attach a delegated change handler to the form
1138 $fixture.on( "change", "form", delegatedChange );
1140 // Trigger change event to introduce the _change_attached property
1141 $form.find("select[name=select1]").val("1").trigger("change");
1143 // Copy the form via .clone() and .html()
1144 $formByClone = $form.clone( true, true ).removeAttr("id");
1145 $formByHTML = jQuery( jQuery.parseHTML($fixture.html()) ).filter("#form").removeAttr("id");
1146 $wrapperDiv.append( $formByClone, $formByHTML );
1148 // Check change bubbling on the copied forms
1149 $wrapperDiv.find("form select[name=select1]").val("2").trigger("change");
1152 $wrapperDiv.remove();
1153 $fixture.off( "change", "form", delegatedChange );
1156 test("trigger(eventObject, [data], [fn])", function() {
1160 $parent = jQuery("<div id='par' />").appendTo("body"),
1161 $child = jQuery("<p id='child'>foo</p>").appendTo( $parent );
1163 $parent.get( 0 ).style.display = "none";
1165 event = jQuery.Event("noNew");
1166 ok( event !== window, "Instantiate jQuery.Event without the 'new' keyword" );
1167 equal( event.type, "noNew", "Verify its type" );
1169 equal( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
1170 equal( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
1171 equal( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
1173 event.preventDefault();
1174 equal( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
1175 event.stopPropagation();
1176 equal( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
1178 event.isPropagationStopped = function(){ return false; };
1179 event.stopImmediatePropagation();
1180 equal( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
1181 equal( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
1183 $parent.on("foo",function( e ) {
1185 equal( e.type, "foo", "Verify event type when passed passing an event object" );
1186 equal( e.target.id, "child", "Verify event.target when passed passing an event object" );
1187 equal( e.currentTarget.id, "par", "Verify event.currentTarget when passed passing an event object" );
1188 equal( e.secret, "boo!", "Verify event object's custom attribute when passed passing an event object" );
1191 // test with an event object
1192 event = new jQuery.Event("foo");
1193 event.secret = "boo!";
1194 $child.trigger(event);
1196 // test with a literal object
1197 $child.trigger({"type": "foo", "secret": "boo!"});
1202 ok( false, "This assertion shouldn't be reached");
1205 $parent.on("foo", error );
1207 $child.on("foo",function(e, a, b, c ){
1208 equal( arguments.length, 4, "Check arguments length");
1209 equal( a, 1, "Check first custom argument");
1210 equal( b, 2, "Check second custom argument");
1211 equal( c, 3, "Check third custom argument");
1213 equal( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
1214 equal( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
1215 equal( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
1217 // Skips both errors
1218 e.stopImmediatePropagation();
1223 // We should add this back in when we want to test the order
1224 // in which event handlers are iterated.
1225 //$child.on("foo", error );
1227 event = new jQuery.Event("foo");
1228 $child.trigger( event, [1,2,3] ).off();
1229 equal( event.result, "result", "Check event.result attribute");
1231 // Will error if it bubbles
1232 $child.triggerHandler("foo");
1235 $parent.off().remove();
1237 // Ensure triggerHandler doesn't molest its event object (#xxx)
1238 event = jQuery.Event( "zowie" );
1239 jQuery( document ).triggerHandler( event );
1240 equal( event.type, "zowie", "Verify its type" );
1241 equal( event.isPropagationStopped(), false, "propagation not stopped" );
1242 equal( event.isDefaultPrevented(), false, "default not prevented" );
1245 test(".trigger() bubbling on disconnected elements (#10489)", function() {
1248 jQuery( window ).on( "click", function(){
1249 ok( false, "click fired on window" );
1252 jQuery( "<div><p>hi</p></div>" )
1253 .on( "click", function() {
1254 ok( true, "click fired on div" );
1257 .on( "click", function() {
1258 ok( true, "click fired on p" );
1266 jQuery( window ).off( "click" );
1269 test(".trigger() doesn't bubble load event (#10717)", function() {
1272 jQuery( window ).on( "load", function(){
1273 ok( false, "load fired on window" );
1276 // It's not an image, but as long as it fires load...
1277 jQuery("<img src='index.html' />")
1279 .on( "load", function() {
1280 ok( true, "load fired on img" );
1285 jQuery( window ).off( "load" );
1288 test("Delegated events in SVG (#10791; #13180)", function() {
1293 "<svg height='1' version='1.1' width='1' xmlns='http://www.w3.org/2000/svg'>" +
1294 "<defs><rect id='ref' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect></defs>" +
1295 "<rect class='svg-by-class' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect>" +
1296 "<rect id='svg-by-id' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect>" +
1297 "<use id='use' xlink:href='#ref'></use>" +
1301 jQuery("#qunit-fixture")
1303 .on( "click", "#svg-by-id", function() {
1304 ok( true, "delegated id selector" );
1306 .on( "click", "[class~='svg-by-class']", function() {
1307 ok( true, "delegated class selector" );
1309 .find( "#svg-by-id, [class~='svg-by-class']" )
1313 // Fire a native click on an SVGElementInstance (the instance tree of an SVG <use>)
1314 // to confirm that it doesn't break our event delegation handling (#13180)
1315 useElem = svg.find("#use")[0];
1316 if ( document.createEvent && useElem && useElem.instanceRoot ) {
1317 e = document.createEvent("MouseEvents");
1318 e.initEvent( "click", true, true );
1319 useElem.instanceRoot.dispatchEvent( e );
1322 jQuery("#qunit-fixture").off("click");
1325 test("Delegated events in forms (#10844; #11145; #8165; #11382, #11764)", function() {
1328 // Alias names like "id" cause havoc
1330 "<form id='myform'>" +
1331 "<input type='text' name='id' value='secret agent man' />" +
1334 .on( "submit", function( event ) {
1335 event.preventDefault();
1340 .on( "submit", "#myform", function() {
1341 ok( true, "delegated id selector with aliased id" );
1348 form.append("<input type='text' name='disabled' value='differently abled' />");
1350 .on( "submit", "#myform", function() {
1351 ok( true, "delegated id selector with aliased disabled" );
1359 .append( "<button id='nestyDisabledBtn'><span>Zing</span></button>" )
1360 .on( "click", "#nestyDisabledBtn", function() {
1361 ok( true, "click on enabled/disabled button with nesty elements" );
1363 .on( "mouseover", "#nestyDisabledBtn", function() {
1364 ok( true, "mouse on enabled/disabled button with nesty elements" );
1367 .trigger( "click" ) // yep
1368 .trigger( "mouseover" ) // yep
1370 .find( "#nestyDisabledBtn" ).prop( "disabled", true ).end()
1372 .trigger( "click" ) // nope
1373 .trigger( "mouseover" ) // yep
1380 test("Submit event can be stopped (#11049)", function() {
1383 // Since we manually bubble in IE, make sure inner handlers get a chance to cancel
1385 "<form id='myform'>" +
1386 "<input type='text' name='sue' value='bawls' />" +
1387 "<input type='submit' />" +
1393 .on( "submit", function() {
1394 ok( true, "submit bubbled on first handler" );
1397 .find( "#myform input[type=submit]" )
1398 .each( function(){ this.click(); } )
1400 .on( "submit", function() {
1401 ok( false, "submit bubbled on second handler" );
1404 .find( "#myform input[type=submit]" )
1406 jQuery( this.form ).on( "submit", function( e ) {
1408 e.stopPropagation();
1418 // Test beforeunload event only if it supported.
1419 // Support: iOS 7+, Android<4.0
1420 // iOS & old Android have the window.onbeforeunload field but don't support the beforeunload
1421 // handler making it impossible to feature-detect the support.
1422 if ( window.onbeforeunload === null &&
1423 !/(ipad|iphone|ipod|android 2\.3)/i.test( navigator.userAgent ) ) {
1424 asyncTest("on(beforeunload)", 1, function() {
1425 var iframe = jQuery(jQuery.parseHTML("<iframe src='data/event/onbeforeunload.html'><iframe>"));
1427 window.onmessage = function( event ) {
1428 var payload = JSON.parse( event.data );
1430 ok( payload.event, "beforeunload", "beforeunload event" );
1433 window.onmessage = null;
1437 iframe.appendTo("#qunit-fixture");
1441 test("jQuery.Event( type, props )", function() {
1445 var event = jQuery.Event( "keydown", { keyCode: 64 }),
1446 handler = function( event ) {
1447 ok( "keyCode" in event, "Special property 'keyCode' exists" );
1448 equal( event.keyCode, 64, "event.keyCode has explicit value '64'" );
1451 // Supports jQuery.Event implementation
1452 equal( event.type, "keydown", "Verify type" );
1454 // ensure "type" in props won't clobber the one set by constructor
1455 equal( jQuery.inArray("type", jQuery.event.props), -1, "'type' property not in props (#10375)" );
1457 ok( "keyCode" in event, "Special 'keyCode' property exists" );
1459 strictEqual( jQuery.isPlainObject( event ), false, "Instances of $.Event should not be identified as a plain object." );
1461 jQuery("body").on( "keydown", handler ).trigger( event );
1463 jQuery("body").off( "keydown" );
1467 test("jQuery.Event properties", function(){
1471 $structure = jQuery("<div id='ancestor'><p id='delegate'><span id='target'>shiny</span></p></div>"),
1472 $target = $structure.find("#target");
1474 handler = function( e ) {
1475 strictEqual( e.currentTarget, this, "currentTarget at " + this.id );
1476 equal( e.isTrigger, 3, "trigger at " + this.id );
1478 $structure.one( "click", handler );
1479 $structure.one( "click", "p", handler );
1480 $target.one( "click", handler );
1481 $target[0].onclick = function( e ) {
1482 strictEqual( e.currentTarget, this, "currentTarget at target (native handler)" );
1483 equal( e.isTrigger, 3, "trigger at target (native handler)" );
1485 $target.trigger("click");
1487 $target.one( "click", function( e ) {
1488 equal( e.isTrigger, 2, "triggerHandler at target" );
1490 $target[0].onclick = function( e ) {
1491 equal( e.isTrigger, 2, "triggerHandler at target (native handler)" );
1493 $target.triggerHandler("click");
1495 handler = function( e ) {
1496 strictEqual( e.isTrigger, undefined, "native event at " + this.id );
1498 $target.one( "click", handler );
1499 $target[0].onclick = function( e ) {
1500 strictEqual( e.isTrigger, undefined, "native event at target (native handler)" );
1502 fireNative( $target[0], "click" );
1505 test(".on()/.off()", function() {
1508 var event, clicked, hash, called, livec, lived, livee,
1509 submit = 0, div = 0, livea = 0, liveb = 0;
1511 jQuery("#body").on("submit", "#qunit-fixture div", function(){ submit++; return false; });
1512 jQuery("#body").on("click", "#qunit-fixture div", function(){ div++; });
1513 jQuery("#body").on("click", "div#nothiddendiv", function(){ livea++; });
1514 jQuery("#body").on("click", "div#nothiddendivchild", function(){ liveb++; });
1516 // Nothing should trigger on the body
1517 jQuery("body").trigger("click");
1518 equal( submit, 0, "Click on body" );
1519 equal( div, 0, "Click on body" );
1520 equal( livea, 0, "Click on body" );
1521 equal( liveb, 0, "Click on body" );
1523 // This should trigger two events
1524 submit = 0; div = 0; livea = 0; liveb = 0;
1525 jQuery("div#nothiddendiv").trigger("click");
1526 equal( submit, 0, "Click on div" );
1527 equal( div, 1, "Click on div" );
1528 equal( livea, 1, "Click on div" );
1529 equal( liveb, 0, "Click on div" );
1531 // This should trigger three events (w/ bubbling)
1532 submit = 0; div = 0; livea = 0; liveb = 0;
1533 jQuery("div#nothiddendivchild").trigger("click");
1534 equal( submit, 0, "Click on inner div" );
1535 equal( div, 2, "Click on inner div" );
1536 equal( livea, 1, "Click on inner div" );
1537 equal( liveb, 1, "Click on inner div" );
1539 // This should trigger one submit
1540 submit = 0; div = 0; livea = 0; liveb = 0;
1541 jQuery("div#nothiddendivchild").trigger("submit");
1542 equal( submit, 1, "Submit on div" );
1543 equal( div, 0, "Submit on div" );
1544 equal( livea, 0, "Submit on div" );
1545 equal( liveb, 0, "Submit on div" );
1547 // Make sure no other events were removed in the process
1548 submit = 0; div = 0; livea = 0; liveb = 0;
1549 jQuery("div#nothiddendivchild").trigger("click");
1550 equal( submit, 0, "off Click on inner div" );
1551 equal( div, 2, "off Click on inner div" );
1552 equal( livea, 1, "off Click on inner div" );
1553 equal( liveb, 1, "off Click on inner div" );
1555 // Now make sure that the removal works
1556 submit = 0; div = 0; livea = 0; liveb = 0;
1557 jQuery("#body").off("click", "div#nothiddendivchild");
1558 jQuery("div#nothiddendivchild").trigger("click");
1559 equal( submit, 0, "off Click on inner div" );
1560 equal( div, 2, "off Click on inner div" );
1561 equal( livea, 1, "off Click on inner div" );
1562 equal( liveb, 0, "off Click on inner div" );
1564 // Make sure that the click wasn't removed too early
1565 submit = 0; div = 0; livea = 0; liveb = 0;
1566 jQuery("div#nothiddendiv").trigger("click");
1567 equal( submit, 0, "off Click on inner div" );
1568 equal( div, 1, "off Click on inner div" );
1569 equal( livea, 1, "off Click on inner div" );
1570 equal( liveb, 0, "off Click on inner div" );
1572 // Make sure that stopPropagation doesn't stop live events
1573 submit = 0; div = 0; livea = 0; liveb = 0;
1574 jQuery("#body").on("click", "div#nothiddendivchild", function( e ){ liveb++; e.stopPropagation(); });
1575 jQuery("div#nothiddendivchild").trigger("click");
1576 equal( submit, 0, "stopPropagation Click on inner div" );
1577 equal( div, 1, "stopPropagation Click on inner div" );
1578 equal( livea, 0, "stopPropagation Click on inner div" );
1579 equal( liveb, 1, "stopPropagation Click on inner div" );
1581 // Make sure click events only fire with primary click
1582 submit = 0; div = 0; livea = 0; liveb = 0;
1583 event = jQuery.Event("click");
1585 jQuery("div#nothiddendiv").trigger(event);
1587 equal( livea, 0, "on secondary click" );
1589 jQuery("#body").off("click", "div#nothiddendivchild");
1590 jQuery("#body").off("click", "div#nothiddendiv");
1591 jQuery("#body").off("click", "#qunit-fixture div");
1592 jQuery("#body").off("submit", "#qunit-fixture div");
1594 // Test binding with a different context
1596 jQuery("#qunit-fixture").on("click", "#foo", function(){ clicked++; });
1597 jQuery("#qunit-fixture div").trigger("click");
1598 jQuery("#foo").trigger("click");
1599 jQuery("#qunit-fixture").trigger("click");
1600 jQuery("body").trigger("click");
1601 equal( clicked, 2, "on with a context" );
1603 // Test unbinding with a different context
1604 jQuery("#qunit-fixture").off("click", "#foo");
1605 jQuery("#foo").trigger("click");
1606 equal( clicked, 2, "off with a context");
1608 // Test binding with event data
1609 jQuery("#body").on("click", "#foo", true, function( e ){ equal( e.data, true, "on with event data" ); });
1610 jQuery("#foo").trigger("click");
1611 jQuery("#body").off("click", "#foo");
1613 // Test binding with trigger data
1614 jQuery("#body").on("click", "#foo", function(e, data){ equal( data, true, "on with trigger data" ); });
1615 jQuery("#foo").trigger("click", true);
1616 jQuery("#body").off("click", "#foo");
1618 // Test binding with different this object
1619 jQuery("#body").on("click", "#foo", jQuery.proxy(function(){ equal( this["foo"], "bar", "on with event scope" ); }, { "foo": "bar" }));
1620 jQuery("#foo").trigger("click");
1621 jQuery("#body").off("click", "#foo");
1623 // Test binding with different this object, event data, and trigger data
1624 jQuery("#body").on("click", "#foo", true, jQuery.proxy(function(e, data){
1625 equal( e.data, true, "on with with different this object, event data, and trigger data" );
1626 equal( this.foo, "bar", "on with with different this object, event data, and trigger data" );
1627 equal( data, true, "on with with different this object, event data, and trigger data");
1628 }, { "foo": "bar" }));
1629 jQuery("#foo").trigger("click", true);
1630 jQuery("#body").off("click", "#foo");
1632 // Verify that return false prevents default action
1633 jQuery("#body").on("click", "#anchor2", function(){ return false; });
1634 hash = window.location.hash;
1635 jQuery("#anchor2").trigger("click");
1636 equal( window.location.hash, hash, "return false worked" );
1637 jQuery("#body").off("click", "#anchor2");
1639 // Verify that .preventDefault() prevents default action
1640 jQuery("#body").on("click", "#anchor2", function(e){ e.preventDefault(); });
1641 hash = window.location.hash;
1642 jQuery("#anchor2").trigger("click");
1643 equal( window.location.hash, hash, "e.preventDefault() worked" );
1644 jQuery("#body").off("click", "#anchor2");
1646 // Test binding the same handler to multiple points
1648 function callback(){ called++; return false; }
1650 jQuery("#body").on("click", "#nothiddendiv", callback);
1651 jQuery("#body").on("click", "#anchor2", callback);
1653 jQuery("#nothiddendiv").trigger("click");
1654 equal( called, 1, "Verify that only one click occurred." );
1657 jQuery("#anchor2").trigger("click");
1658 equal( called, 1, "Verify that only one click occurred." );
1660 // Make sure that only one callback is removed
1661 jQuery("#body").off("click", "#anchor2", callback);
1664 jQuery("#nothiddendiv").trigger("click");
1665 equal( called, 1, "Verify that only one click occurred." );
1668 jQuery("#anchor2").trigger("click");
1669 equal( called, 0, "Verify that no click occurred." );
1671 // Make sure that it still works if the selector is the same,
1672 // but the event type is different
1673 jQuery("#body").on("foo", "#nothiddendiv", callback);
1676 jQuery("#body").off("click", "#nothiddendiv", callback);
1679 jQuery("#nothiddendiv").trigger("click");
1680 equal( called, 0, "Verify that no click occurred." );
1683 jQuery("#nothiddendiv").trigger("foo");
1684 equal( called, 1, "Verify that one foo occurred." );
1687 jQuery("#body").off("foo", "#nothiddendiv", callback);
1689 // Make sure we don't loose the target by DOM modifications
1690 // after the bubble already reached the liveHandler
1692 jQuery("#nothiddendivchild").html("<span></span>");
1694 jQuery("#body").on("click", "#nothiddendivchild", function(){ jQuery("#nothiddendivchild").html(""); });
1695 jQuery("#body").on("click", "#nothiddendivchild", function(e){ if(e.target) {livec++;} });
1697 jQuery("#nothiddendiv span").trigger("click");
1698 equal( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1699 equal( livec, 1, "Verify that second handler occurred even with nuked target." );
1702 jQuery("#body").off("click", "#nothiddendivchild");
1704 // Verify that .live() occurs and cancel bubble in the same order as
1705 // we would expect .on() and .click() without delegation
1709 // bind one pair in one order
1710 jQuery("#body").on("click", "span#liveSpan1 a", function(){ lived++; return false; });
1711 jQuery("#body").on("click", "span#liveSpan1", function(){ livee++; });
1713 jQuery("span#liveSpan1 a").trigger("click");
1714 equal( lived, 1, "Verify that only one first handler occurred." );
1715 equal( livee, 0, "Verify that second handler doesn't." );
1717 // and one pair in inverse
1718 jQuery("#body").on("click", "span#liveSpan2", function(){ livee++; });
1719 jQuery("#body").on("click", "span#liveSpan2 a", function(){ lived++; return false; });
1723 jQuery("span#liveSpan2 a").trigger("click");
1724 equal( lived, 1, "Verify that only one first handler occurred." );
1725 equal( livee, 0, "Verify that second handler doesn't." );
1728 jQuery("#body").off("click", "**");
1730 // Test this, target and currentTarget are correct
1731 jQuery("#body").on("click", "span#liveSpan1", function( e ) {
1732 equal( this.id, "liveSpan1", "Check the this within a on handler" );
1733 equal( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a on handler" );
1734 equal( e.delegateTarget, document.body, "Check the event.delegateTarget within a on handler" );
1735 equal( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a on handler" );
1738 jQuery("span#liveSpan1 a").trigger("click");
1740 jQuery("#body").off("click", "span#liveSpan1");
1742 // Work with deep selectors
1745 function clickB() { livee++; }
1747 jQuery("#body").on("click", "#nothiddendiv div", function(){ livee++; });
1748 jQuery("#body").on("click", "#nothiddendiv div", clickB);
1749 jQuery("#body").on("mouseover", "#nothiddendiv div", function(){ livee++; });
1751 equal( livee, 0, "No clicks, deep selector." );
1754 jQuery("#nothiddendivchild").trigger("click");
1755 equal( livee, 2, "Click, deep selector." );
1758 jQuery("#nothiddendivchild").trigger("mouseover");
1759 equal( livee, 1, "Mouseover, deep selector." );
1761 jQuery("#body").off("mouseover", "#nothiddendiv div");
1764 jQuery("#nothiddendivchild").trigger("click");
1765 equal( livee, 2, "Click, deep selector." );
1768 jQuery("#nothiddendivchild").trigger("mouseover");
1769 equal( livee, 0, "Mouseover, deep selector." );
1771 jQuery("#body").off("click", "#nothiddendiv div", clickB);
1774 jQuery("#nothiddendivchild").trigger("click");
1775 equal( livee, 1, "Click, deep selector." );
1777 jQuery("#body").off("click", "#nothiddendiv div");
1780 test("jQuery.off using dispatched jQuery.Event", function() {
1783 var markup = jQuery("<p><a href='#'>target</a></p>"),
1786 .on( "click.name", "a", function( event ) {
1787 equal( ++count, 1, "event called once before removal" );
1788 jQuery().off( event );
1790 .find("a").trigger("click").trigger("click").end()
1794 test( "delegated event with delegateTarget-relative selector", function() {
1796 var markup = jQuery("<div><ul><li><a id=\"a0\"></a><ul id=\"ul0\"><li class=test><a id=\"a0_0\"></a></li><li><a id=\"a0_1\"></a></li></ul></li></ul></div>").appendTo("#qunit-fixture");
1798 // Non-positional selector (#12383)
1800 .on( "click", "div li a", function() {
1801 ok( false, "div is ABOVE the delegation point!" );
1803 .on( "click", "ul a", function() {
1804 ok( false, "ul IS the delegation point!" );
1806 .on( "click", "li.test a", function() {
1807 ok( true, "li.test is below the delegation point." );
1809 .find("#a0_0").trigger("click").end()
1812 // Positional selector (#11315)
1813 markup.find("ul").eq(0)
1814 .on( "click", ">li>a", function() {
1815 ok( this.id === "a0", "child li was clicked" );
1818 .on( "click", "li:first>a", function() {
1819 ok( this.id === "a0_0" , "first li under #u10 was clicked" );
1822 .find("a").trigger("click").end()
1823 .find("#ul0").off();
1828 test( "delegated event with selector matching Object.prototype property (#13203)", function() {
1833 jQuery("#foo").on( "click", "toString", function() {
1837 jQuery("#anchor2").trigger("click");
1839 equal( matched, 0, "Nothing matched 'toString'" );
1842 test("stopPropagation() stops directly-bound events on delegated target", function() {
1845 var markup = jQuery("<div><p><a href=\"#\">target</a></p></div>");
1847 .on( "click", function() {
1848 ok( false, "directly-bound event on delegate target was called" );
1850 .on( "click", "a", function( e ) {
1851 e.stopPropagation();
1852 ok( true, "delegated handler was called" );
1854 .find("a").trigger("click").end()
1858 test("off all bound delegated events", function(){
1863 div = jQuery("#body");
1865 div.on( "click submit", "div#nothiddendivchild", function(){ count++; } );
1866 div.on( "click", function(){ clicks++; } );
1867 div.off( undefined, "**" );
1869 jQuery("div#nothiddendivchild").trigger("click");
1870 jQuery("div#nothiddendivchild").trigger("submit");
1872 equal( count, 0, "Make sure no events were triggered." );
1874 div.trigger("click");
1875 equal( clicks, 2, "Make sure delegated and directly bound event occurred." );
1879 test("on with multiple delegated events", function(){
1883 div = jQuery("#body");
1885 div.on("click submit", "div#nothiddendivchild", function(){ count++; });
1887 jQuery("div#nothiddendivchild").trigger("click");
1888 jQuery("div#nothiddendivchild").trigger("submit");
1890 equal( count, 2, "Make sure both the click and submit were triggered." );
1892 jQuery("#body").off( undefined, "**" );
1895 test("delegated on with change", function(){
1898 var select, checkbox, checkboxFunction,
1899 text, textChange, oldTextVal,
1900 password, passwordChange, oldPasswordVal,
1904 select = jQuery("select[name='S1']");
1905 jQuery("#body").on("change", "select[name='S1']", function() {
1909 checkbox = jQuery("#check2");
1910 checkboxFunction = function(){
1913 jQuery("#body").on("change", "#check2", checkboxFunction);
1915 // test click on select
1917 // second click that changed it
1919 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1920 select.trigger("change");
1921 equal( selectChange, 1, "Change on click." );
1923 // test keys on select
1925 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1926 select.trigger("change");
1927 equal( selectChange, 1, "Change on keyup." );
1929 // test click on checkbox
1930 checkbox.trigger("change");
1931 equal( checkboxChange, 1, "Change on checkbox." );
1933 // test blur/focus on text
1934 text = jQuery("#name");
1936 oldTextVal = text.val();
1938 jQuery("#body").on("change", "#name", function() {
1942 text.val(oldTextVal+"foo");
1943 text.trigger("change");
1944 equal( textChange, 1, "Change on text input." );
1946 text.val(oldTextVal);
1947 jQuery("#body").off("change", "#name");
1949 // test blur/focus on password
1950 password = jQuery("#name");
1952 oldPasswordVal = password.val();
1953 jQuery("#body").on("change", "#name", function() {
1957 password.val(oldPasswordVal + "foo");
1958 password.trigger("change");
1959 equal( passwordChange, 1, "Change on password input." );
1961 password.val(oldPasswordVal);
1962 jQuery("#body").off("change", "#name");
1964 // make sure die works
1968 jQuery("#body").off("change", "select[name='S1']");
1969 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1970 select.trigger("change");
1971 equal( selectChange, 0, "Die on click works." );
1974 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1975 select.trigger("change");
1976 equal( selectChange, 0, "Die on keyup works." );
1978 // die specific checkbox
1979 jQuery("#body").off("change", "#check2", checkboxFunction);
1980 checkbox.trigger("change");
1981 equal( checkboxChange, 1, "Die on checkbox." );
1984 test("delegated on with submit", function() {
1987 var count1 = 0, count2 = 0;
1989 jQuery("#body").on("submit", "#testForm", function(ev) {
1991 ev.preventDefault();
1994 jQuery(document).on("submit", "body", function(ev) {
1996 ev.preventDefault();
1999 jQuery("#testForm input[name=sub1]").trigger("submit");
2000 equal( count1, 1, "Verify form submit." );
2001 equal( count2, 1, "Verify body submit." );
2003 jQuery("#body").off( undefined, "**" );
2004 jQuery(document).off( undefined, "**" );
2007 test("delegated off() with only namespaces", function() {
2010 var $delegate = jQuery("#liveHandlerOrder"),
2013 $delegate.on("click.ns", "a", function() {
2017 jQuery("a", $delegate).eq(0).trigger("click.ns");
2019 equal( count, 1, "delegated click.ns");
2021 $delegate.off( ".ns", "**" );
2023 jQuery("a", $delegate).eq(1).trigger("click.ns");
2025 equal( count, 1, "no more .ns after off");
2028 test("Non DOM element events", function() {
2033 jQuery(o).on("nonelementobj", function() {
2034 ok( true, "Event on non-DOM object triggered" );
2037 jQuery(o).trigger("nonelementobj").off("nonelementobj");
2040 test("inline handler returning false stops default", function() {
2043 var markup = jQuery("<div><a href=\"#\" onclick=\"return false\">x</a></div>");
2044 markup.on( "click", function(e) {
2045 ok( e.isDefaultPrevented(), "inline handler prevented default");
2048 markup.find("a").trigger("click");
2049 markup.off("click");
2052 test("window resize", function() {
2055 jQuery(window).off();
2057 jQuery(window).on( "resize", function(){
2058 ok( true, "Resize event fired." );
2059 }).trigger("resize").off("resize");
2061 ok( !jQuery._data(window, "events"), "Make sure all the events are gone." );
2064 test("focusin bubbles", function() {
2067 var input = jQuery( "<input type='text' />" ).prependTo( "body" ),
2070 // focus the element so DOM focus won't fire
2073 jQuery( "body" ).on( "focusin.focusinBubblesTest", function(){
2074 equal( 1, order++, "focusin on the body second" );
2077 input.on( "focusin.focusinBubblesTest", function(){
2078 equal( 0, order++, "focusin on the element first" );
2081 // Removed since DOM focus is unreliable on test swarm
2083 // input[0].focus();
2085 // To make the next focus test work, we need to take focus off the input.
2086 // This will fire another focusin event, so set order to reflect that.
2088 // jQuery("#text1")[0].focus();
2090 // jQuery trigger, which calls DOM focus
2092 input.trigger( "focus" );
2095 jQuery( "body" ).off( "focusin.focusinBubblesTest" );
2098 test("custom events with colons (#3533, #8272)", function() {
2101 var tab = jQuery("<table><tr><td>trigger</td></tr></table>").appendTo("body");
2103 tab.trigger("back:forth");
2104 ok( true, "colon events don't throw" );
2106 ok( false, "colon events die" );
2112 test(".on and .off", function() {
2114 var counter, mixfn, data,
2115 $onandoff = jQuery("<div id=\"onandoff\"><p>on<b>and</b>off</p><div>worked<em>or</em>borked?</div></div>").appendTo("body");
2118 jQuery( "#onandoff" )
2119 .on( "whip", function() {
2120 ok( true, "whipped it good" );
2125 // Direct events only
2127 jQuery( "#onandoff b" )
2128 .on( "click", 5, function( e, trig ) {
2129 counter += e.data + (trig || 9); // twice, 5+9+5+17=36
2131 .one( "click", 7, function( e, trig ) {
2132 counter += e.data + (trig || 11); // once, 7+11=18
2135 .trigger( "click", 17 )
2137 equal( counter, 54, "direct event bindings with data" );
2139 // Delegated events only
2141 jQuery( "#onandoff" )
2142 .on( "click", "em", 5, function( e, trig ) {
2143 counter += e.data + (trig || 9); // twice, 5+9+5+17=36
2145 .one( "click", "em", 7, function( e, trig ) {
2146 counter += e.data + (trig || 11); // once, 7+11=18
2150 .trigger( "click", 17 )
2152 .off( "click", "em" );
2153 equal( counter, 54, "delegated event bindings with data" );
2156 // Mixed event bindings and types
2158 mixfn = function(e, trig) {
2159 counter += (e.data || 0) + (trig || 1);
2161 jQuery( "#onandoff" )
2162 .on( " click clack cluck ", "em", 2, mixfn )
2163 .on( "cluck", "b", 7, mixfn )
2164 .on( "cluck", mixfn )
2167 equal( counter, 0, "nothing triggered yet" );
2170 .one( "cluck", 3, mixfn )
2171 .trigger( "cluck", 8 ) // 3+8 2+8 + 0+8 = 29
2173 .trigger( "cluck", 9 ) // 2+9 + 0+9 = 20
2176 equal( counter, 49, "after triggering em element" );
2178 .off( "cluck", function(){} ) // shouldn't remove anything
2179 .trigger( "cluck", 2 ) // 0+2 = 2
2181 equal( counter, 51, "after triggering #onandoff cluck" );
2184 .on( "click", 95, mixfn )
2185 .on( "clack", "p", 97, mixfn )
2186 .one( "cluck", 3, mixfn )
2187 .trigger( "quack", 19 ) // 0
2188 .off( "click clack cluck" )
2191 equal( counter, 51, "after triggering b" );
2193 .trigger( "cluck", 3 ) // 0+3 = 3
2194 .off( "clack", "em", mixfn )
2196 .trigger( "clack" ) // 0
2199 equal( counter, 54, "final triggers" );
2201 .off( "click cluck" );
2203 // We should have removed all the event handlers ... kinda hacky way to check this
2204 data = jQuery.data[ jQuery( "#onandoff" )[0].expando ] || {};
2205 equal( data["events"], undefined, "no events left" );
2210 test("special on name mapping", function() {
2213 jQuery.event.special["slap"] = {
2215 delegateType: "swing",
2216 handle: function( event ) {
2217 equal( event.handleObj.origType, "slap", "slapped your mammy, " + event.type );
2221 var comeback = function( event ) {
2222 ok( true, "event " + event.type + " triggered" );
2225 jQuery("<div><button id=\"mammy\">Are We Not Men?</button></div>")
2226 .on( "slap", "button", jQuery.noop )
2227 .on( "swing", "button", comeback )
2229 .on( "slap", jQuery.noop )
2230 .on( "click", comeback )
2231 .trigger( "click" ) // bindType-slap and click
2233 .trigger( "click" ) // click
2235 .trigger( "swing" ) // delegateType-slap and swing
2237 .off( "slap swing", "button" )
2238 .find( "button" ) // everything should be gone
2244 delete jQuery.event.special["slap"];
2246 jQuery.event.special["gutfeeling"] = {
2248 delegateType: "click",
2249 handle: function( event ) {
2250 equal( event.handleObj.origType, "gutfeeling", "got a gutfeeling" );
2251 // Need to call the handler since .one() uses it to unbind
2252 return event.handleObj.handler.call( this , event );
2256 // Ensure a special event isn't removed by its mapped type
2257 jQuery( "<p>Gut Feeling</p>" )
2258 .on( "click", jQuery.noop )
2259 .on( "gutfeeling", jQuery.noop )
2261 .trigger( "gutfeeling" )
2264 // Ensure special events are removed when only a namespace is provided
2265 jQuery( "<p>Gut Feeling</p>" )
2266 .on( "gutfeeling.Devo", jQuery.noop )
2268 .trigger( "gutfeeling" )
2271 // Ensure .one() events are removed after their maiden voyage
2272 jQuery( "<p>Gut Feeling</p>" )
2273 .one( "gutfeeling", jQuery.noop )
2274 .trigger( "gutfeeling" ) // This one should
2275 .trigger( "gutfeeling" ) // This one should not
2278 delete jQuery.event.special["gutfeeling"];
2281 test(".on and .off, selective mixed removal (#10705)", function() {
2284 var timingx = function( e ) {
2285 ok( true, "triggered " + e.type );
2288 jQuery( "<p>Strange Pursuit</p>" )
2289 .on( "click", timingx )
2290 .on( "click.duty", timingx )
2291 .on( "click.now", timingx )
2292 .on( "devo", timingx )
2293 .on( "future", timingx )
2294 .trigger( "click" ) // 3
2295 .trigger( "devo" ) // 1
2296 .off( ".duty devo " ) // trailing space
2297 .trigger( "future" ) // 1
2298 .trigger( "click" ) // 2
2299 .off( "future click" )
2300 .trigger( "click" ); // 0
2303 test(".on( event-map, null-selector, data ) #11130", function() {
2307 var $p = jQuery("<p>Strange Pursuit</p>"),
2310 "foo": function( event ) {
2311 equal( event.data, "bar", "event.data correctly relayed with null selector" );
2316 $p.on( map, null, data ).trigger("foo");
2319 test("clone() delegated events (#11076)", function() {
2322 var counter = { "center": 0, "fold": 0, "centerfold": 0 },
2323 clicked = function() {
2324 counter[ jQuery(this).text().replace(/\s+/, "") ]++;
2327 jQuery( "<table><tr><td>center</td><td>fold</td></tr></table>" )
2328 .on( "click", "tr", clicked )
2329 .on( "click", "td:first-child", clicked )
2330 .on( "click", "td:last-child", clicked ),
2331 clone = table.clone( true );
2333 clone.find("td").trigger("click");
2334 equal( counter["center"], 1, "first child" );
2335 equal( counter["fold"], 1, "last child" );
2336 equal( counter["centerfold"], 2, "all children" );
2342 test("checkbox state (#3827)", function() {
2345 var markup = jQuery("<div><input type=checkbox><div>").appendTo("#qunit-fixture"),
2346 cb = markup.find("input")[0];
2348 jQuery(cb).on( "click", function(){
2349 equal( this.checked, false, "just-clicked checkbox is not checked" );
2351 markup.on( "click", function(){
2352 equal( cb.checked, false, "checkbox is not checked in bubbled event" );
2357 equal( cb.checked, true, "native - checkbox is initially checked" );
2359 equal( cb.checked, false, "native - checkbox is no longer checked" );
2363 equal( cb.checked, true, "jQuery - checkbox is initially checked" );
2364 jQuery( cb ).trigger("click");
2365 equal( cb.checked, false, "jQuery - checkbox is no longer checked" );
2367 // Handlers only; checkbox state remains false
2368 jQuery( cb ).triggerHandler( "click" );
2371 test("hover event no longer special since 1.9", function() {
2374 jQuery("<div>craft</div>")
2375 .on( "hover", function( e ) {
2376 equal( e.type, "hover", "I am hovering!" );
2382 test( "event object properties on natively-triggered event", function() {
2385 var link = document.createElement( "a" ),
2386 $link = jQuery( link ),
2387 evt = document.createEvent( "MouseEvents" );
2389 // IE9+ requires element to be in the body before it will dispatch
2390 $link.appendTo( "body" ).on( "click", function( e ) {
2391 // Not trying to assert specific values here, just ensure the property exists
2392 equal( "detail" in e, true, "has .detail" );
2393 equal( "cancelable" in e, true, "has .cancelable" );
2394 equal( "bubbles" in e, true, "has .bubbles" );
2396 evt.initEvent( "click", true, true );
2397 link.dispatchEvent( evt );
2398 $link.off( "click" ).remove();
2401 test("fixHooks extensions", function() {
2404 // IE requires focusable elements to be visible, so append to body
2405 var $fixture = jQuery( "<input type='text' id='hook-fixture' />" ).appendTo( "body" ),
2406 saved = jQuery.event.fixHooks.click;
2408 // Ensure the property doesn't exist
2409 $fixture.on( "click", function( event ) {
2410 ok( !("blurrinessLevel" in event), "event.blurrinessLevel does not exist" );
2412 fireNative( $fixture[0], "click" );
2413 $fixture.off( "click" );
2415 jQuery.event.fixHooks.click = {
2416 filter: function( event ) {
2417 event.blurrinessLevel = 42;
2422 // Trigger a native click and ensure the property is set
2423 $fixture.on( "click", function( event ) {
2424 equal( event.blurrinessLevel, 42, "event.blurrinessLevel was set" );
2426 fireNative( $fixture[0], "click" );
2428 delete jQuery.event.fixHooks.click;
2429 $fixture.off( "click" ).remove();
2430 jQuery.event.fixHooks.click = saved;
2433 test( "drag/drop events copy mouse-related event properties (gh-1925, gh-2009)", function() {
2436 var $fixture = jQuery( "<div id='drag-fixture'></div>" ).appendTo( "body" );
2438 $fixture.on( "dragmove", function( evt ) {
2439 ok( "pageX" in evt, "checking for pageX property on dragmove" );
2440 ok( "pageY" in evt, "checking for pageY property on dragmove" );
2442 fireNative( $fixture[ 0 ], "dragmove" );
2444 $fixture.on( "drop", function( evt ) {
2445 ok( "pageX" in evt, "checking for pageX property on drop" );
2446 ok( "pageY" in evt, "checking for pageY property on drop" );
2449 fireNative( $fixture[ 0 ], "drop" );
2451 $fixture.unbind( "dragmove drop" ).remove();
2454 test( "focusin using non-element targets", function() {
2457 jQuery( document ).on( "focusin", function( e ) {
2458 ok( e.type === "focusin", "got a focusin event on a document" );
2459 }).trigger( "focusin" ).off( "focusin" );
2461 jQuery( window ).on( "focusin", function( e ) {
2462 ok( e.type === "focusin", "got a focusin event on a window" );
2463 }).trigger( "focusin" ).off( "focusin" );
2467 testIframeWithCallback( "focusin from an iframe", "event/focusinCrossFrame.html", function( frameDoc ) {
2470 var input = jQuery( frameDoc ).find( "#frame-input" );
2472 // Create a focusin handler on the parent; shouldn't affect the iframe's fate
2473 jQuery ( "body" ).on( "focusin.iframeTest", function() {
2474 ok( false, "fired a focusin event in the parent document" );
2477 input.on( "focusin", function() {
2478 ok( true, "fired a focusin event in the iframe" );
2481 // Avoid a native event; Chrome can't force focus to another frame
2482 input.trigger( "focusin" );
2484 // Must manually remove handler to avoid leaks in our data store
2487 // Be sure it was removed; nothing should happen
2488 input.trigger( "focusin" );
2490 // Remove body handler manually since it's outside the fixture
2491 jQuery( "body" ).off( "focusin.iframeTest" );
2494 testIframeWithCallback( "jQuery.ready promise", "event/promiseReady.html", function( isOk ) {
2496 ok( isOk, "$.when( $.ready ) works" );
2499 testIframeWithCallback( "Focusing iframe element", "event/focusElem.html", function( isOk ) {
2501 ok( isOk, "Focused an element in an iframe" );
2504 testIframeWithCallback( "triggerHandler(onbeforeunload)", "event/triggerunload.html", function( isOk ) {
2506 ok( isOk, "Triggered onbeforeunload without an error" );
2509 // need PHP here to make the incepted IFRAME hang
2511 testIframeWithCallback( "jQuery.ready synchronous load with long loading subresources", "event/syncReady.html", function( isOk ) {
2513 ok( isOk, "jQuery loaded synchronously fires ready when the DOM can truly be interacted with" );
2517 test("change handler should be detached from element", function() {
2520 var $fixture = jQuery( "<input type='text' id='change-ie-leak' />" ).appendTo( "body" ),
2521 originRemoveEvent = jQuery.removeEvent,
2522 wrapperRemoveEvent = function(elem, type, handle){
2523 equal("change", type, "Event handler for 'change' event should be removed");
2524 equal("change-ie-leak", jQuery(elem).attr("id"), "Event handler for 'change' event should be removed from appropriate element");
2525 originRemoveEvent(elem, type, handle);
2528 jQuery.removeEvent = wrapperRemoveEvent ;
2530 $fixture.on( "change", function() {});
2531 $fixture.off( "change" );
2535 jQuery.removeEvent = originRemoveEvent;
2538 asyncTest("trigger click on checkbox, fires change event", function() {
2541 var check = jQuery("#check2");
2543 check.on( "change", function() {
2545 check.off("change");
2546 ok( true, "Change event fired as a result of triggered click" );
2548 }).trigger("click");
2551 test( "Namespace preserved when passed an Event (#12739)", function() {
2554 var markup = jQuery(
2555 "<div id='parent'><div id='child'></div></div>"
2562 .on( "foo.bar", function( e ) {
2566 equal( e.namespace, "bar", "namespace is bar" );
2567 jQuery( e.target ).find("div").each(function() {
2568 jQuery( this ).triggerHandler( e );
2572 .on( "foo.bar2", function() {
2573 ok( false, "foo.bar2 called on trigger " + triggered + " id " + this.id );
2576 markup.trigger("foo.bar");
2577 markup.trigger( jQuery.Event("foo.bar") );
2578 fooEvent = jQuery.Event("foo");
2579 fooEvent.namespace = "bar";
2580 markup.trigger( fooEvent );
2583 equal( triggered, 3, "foo.bar triggered" );
2586 test( "make sure events cloned correctly", 18, function() {
2588 fixture = jQuery("#qunit-fixture"),
2589 checkbox = jQuery("#check1"),
2590 p = jQuery("#firstp");
2592 fixture.on( "click change", function( event, result ) {
2593 ok( result, event.type + " on original element is fired" );
2595 }).on( "click", "#firstp", function( event, result ) {
2596 ok( result, "Click on original child element though delegation is fired" );
2598 }).on( "change", "#check1", function( event, result ) {
2599 ok( result, "Change on original child element though delegation is fired" );
2602 p.on("click", function() {
2603 ok( true, "Click on original child element is fired" );
2606 checkbox.on("change", function() {
2607 ok( true, "Change on original child element is fired" );
2610 fixture.clone().trigger("click").trigger("change"); // 0 events should be fired
2612 clone = fixture.clone( true );
2614 clone.find("p").eq(0).trigger( "click", true ); // 3 events should fire
2615 clone.find("#check1").trigger( "change", true ); // 3 events should fire
2618 clone = fixture.clone( true, true );
2619 clone.find("p").eq(0).trigger( "click", true ); // 3 events should fire
2620 clone.find("#check1").trigger( "change", true ); // 3 events should fire
2626 p.trigger("click"); // 0 should be fired
2627 checkbox.trigger("change"); // 0 should be fired
2629 clone.find("p").eq(0).trigger( "click", true ); // 3 events should fire
2630 clone.find("#check1").trigger( "change", true ); // 3 events should fire
2633 clone.find("p").eq(0).trigger("click"); // 0 should be fired
2634 clone.find("#check1").trigger("change"); // 0 events should fire
2637 test( "String.prototype.namespace does not cause trigger() to throw (#13360)", function() {
2639 var errored = false;
2641 String.prototype.namespace = function() {};
2644 jQuery("<p>").trigger("foo.bar");
2648 equal( errored, false, "trigger() did not throw exception" );
2649 delete String.prototype.namespace;
2652 test( "Inline event result is returned (#13993)", function() {
2655 var result = jQuery("<p onclick='return 42'>hello</p>").triggerHandler("click");
2657 equal( result, 42, "inline handler returned value" );
2660 // This tests are unreliable in Firefox
2661 if ( !(/firefox/i.test( window.navigator.userAgent )) ) {
2662 test( "Check order of focusin/focusout events", 2, function() {
2664 input = jQuery( "#name" );
2666 input.on( "focus", function() {
2669 }).on( "focusin", function() {
2670 ok( !focus, "Focusin event should fire before focus does" );
2672 }).on( "blur", function() {
2675 }).on( "focusout", function() {
2676 ok( !blur, "Focusout event should fire before blur does" );
2680 input.trigger( "focus" );
2683 jQuery( "#search" ).trigger( "focus" );
2689 test("focus-blur order (#12868)", function() {
2693 $text = jQuery("#text1"),
2694 $radio = jQuery("#radio1").trigger("focus");
2696 // IE8-10 fire focus/blur events asynchronously; this is the resulting mess.
2697 // IE's browser window must be topmost for this to work properly!!
2701 setTimeout( function() {
2704 .on( "focus", function(){
2705 equal( order++, 1, "text focus" );
2707 .on( "blur", function(){
2708 equal( order++, 0, "text blur" );
2711 .on( "focus", function(){
2712 equal( order++, 1, "radio focus" );
2714 .on( "blur", function(){
2715 equal( order++, 0, "radio blur" );
2718 // Enabled input getting focus
2720 equal( document.activeElement, $radio[0], "radio has focus" );
2721 $text.trigger("focus");
2722 setTimeout( function() {
2723 equal( document.activeElement, $text[0], "text has focus" );
2725 // Run handlers without native method on an input
2727 $radio.triggerHandler( "focus" );