1 module("event", { teardown: moduleTeardown });
3 test("null or undefined handler", function() {
5 // Supports Fixes bug #7229
8 jQuery("#firstp").click(null);
10 ok(true, "Passing a null handler will not throw an exception");
16 jQuery("#firstp").click(undefined);
18 ok(true, "Passing an undefined handler will not throw an exception");
23 test("bind(), with data", function() {
25 var handler = function(event) {
26 ok( event.data, "bind() with data, check passed data exists" );
27 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
29 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
31 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
34 test("click(), with data", function() {
36 var handler = function(event) {
37 ok( event.data, "bind() with data, check passed data exists" );
38 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
40 jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
42 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
45 test("bind(), with data, trigger with data", function() {
47 var handler = function(event, data) {
48 ok( event.data, "check passed data exists" );
49 equals( event.data.foo, "bar", "Check value of passed data" );
50 ok( data, "Check trigger data" );
51 equals( data.bar, "foo", "Check value of trigger data" );
53 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
56 test("bind(), multiple events at once", function() {
60 var handler = function(event) {
61 if (event.type == "click")
63 else if (event.type == "mouseover")
64 mouseoverCounter += 1;
66 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
67 equals( clickCounter, 1, "bind() with multiple events at once" );
68 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
71 test("bind(), multiple events at once and namespaces", function() {
76 var div = jQuery("<div/>").bind("focusin.a", function(e) {
77 equals( e.type, cur, "Verify right single event was fired." );
81 div.trigger("focusin.a");
83 // manually clean up detached elements
86 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
87 equals( e.type, cur, "Verify right multi event was fired." );
88 equals( e.data, obj, "Make sure the data came in correctly." );
95 div.trigger("mouseover");
97 // manually clean up detached elements
100 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
101 equals( e.type, cur, "Verify right multi event was fired." );
105 div.trigger("focusin.a");
108 div.trigger("focusout.b");
110 // manually clean up detached elements
114 test("bind(), namespace with special add", function() {
117 var div = jQuery("<div/>").bind("test", function(e) {
118 ok( true, "Test event fired." );
123 jQuery.event.special.test = {
124 _default: function(e) {
125 equals( this, document, "Make sure we're at the top of the chain." );
126 equals( e.type, "test", "And that we're still dealing with a test event." );
127 equals( e.target, div[0], "And that the target is correct." );
130 teardown: function(){
131 ok(true, "Teardown called.");
133 add: function( handleObj ) {
134 var handler = handleObj.handler;
135 handleObj.handler = function(e) {
137 handler.apply( this, arguments );
141 ok(true, "Remove called.");
145 div.bind("test.a", {x: 1}, function(e) {
146 ok( !!e.xyz, "Make sure that the data is getting passed through." );
147 equals( e.data.x, 1, "Make sure data is attached properly." );
150 div.bind("test.b", {x: 2}, function(e) {
151 ok( !!e.xyz, "Make sure that the data is getting passed through." );
152 equals( e.data.x, 2, "Make sure data is attached properly." );
159 div.trigger("test.a");
162 div.trigger("test.b");
167 div = jQuery("<div/>").bind("test", function(e) {
168 ok( true, "Test event fired." );
172 div.appendTo("#main").remove();
174 delete jQuery.event.special.test;
177 test("bind(), no data", function() {
179 var handler = function(event) {
180 ok ( !event.data, "Check that no data is added to the event object" );
182 jQuery("#firstp").bind("click", handler).trigger("click");
185 test("bind/one/unbind(Object)", function(){
188 var clickCounter = 0, mouseoverCounter = 0;
189 function handler(event) {
190 if (event.type == "click")
192 else if (event.type == "mouseover")
196 function handlerWithData(event) {
197 if (event.type == "click")
198 clickCounter += event.data;
199 else if (event.type == "mouseover")
200 mouseoverCounter += event.data;
204 $elem.trigger("click").trigger("mouseover");
207 var $elem = jQuery("#firstp")
215 click:handlerWithData,
216 mouseover:handlerWithData
221 equals( clickCounter, 3, "bind(Object)" );
222 equals( mouseoverCounter, 3, "bind(Object)" );
225 equals( clickCounter, 4, "bind(Object)" );
226 equals( mouseoverCounter, 4, "bind(Object)" );
228 jQuery("#firstp").unbind({
234 equals( clickCounter, 4, "bind(Object)" );
235 equals( mouseoverCounter, 4, "bind(Object)" );
238 test("live/die(Object), delegate/undelegate(String, Object)", function() {
241 var clickCounter = 0, mouseoverCounter = 0,
242 $p = jQuery("#firstp"), $a = $p.find("a:first");
245 click: function( event ) {
246 clickCounter += ( event.data || 1 );
248 mouseover: function( event ) {
249 mouseoverCounter += ( event.data || 1 );
254 $a.trigger("click").trigger("mouseover");
258 $p.delegate( "a", events, 2 );
261 equals( clickCounter, 3, "live/delegate" );
262 equals( mouseoverCounter, 3, "live/delegate" );
264 $p.undelegate( "a", events );
267 equals( clickCounter, 4, "undelegate" );
268 equals( mouseoverCounter, 4, "undelegate" );
273 equals( clickCounter, 4, "die" );
274 equals( mouseoverCounter, 4, "die" );
277 test("live/delegate immediate propagation", function() {
280 var $p = jQuery("#firstp"), $a = $p.find("a:first"), lastClick;
283 $a.live( "click", function(e) {
284 lastClick = "click1";
285 e.stopImmediatePropagation();
287 $a.live( "click", function(e) {
288 lastClick = "click2";
290 $a.trigger( "click" );
291 equals( lastClick, "click1", "live stopImmediatePropagation" );
295 $p.delegate( "a", "click", function(e) {
296 lastClick = "click1";
297 e.stopImmediatePropagation();
299 $p.delegate( "a", "click", function(e) {
300 lastClick = "click2";
302 $a.trigger( "click" );
303 equals( lastClick, "click1", "delegate stopImmediatePropagation" );
304 $p.undelegate( "click" );
307 test("bind/delegate bubbling, isDefaultPrevented (Bug #7793)", function() {
309 var $anchor2 = jQuery( "#anchor2" ),
310 $main = jQuery( "#main" ),
311 fakeClick = function($jq) {
312 // Use a native click so we don't get jQuery simulated bubbling
313 if ( document.createEvent ) {
314 var e = document.createEvent( "MouseEvents" );
315 e.initEvent( "click", true, true );
316 $jq[0].dispatchEvent(e);
318 else if ( $jq[0].click ) {
319 $jq[0].click(); // IE
322 $anchor2.click(function(e) {
325 $main.delegate("#foo", "click", function(e) {
326 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
328 fakeClick( $anchor2 );
329 $anchor2.unbind( "click" );
330 $main.undelegate( "click" );
331 $anchor2.click(function(e) {
332 // Let the default action occur
334 $main.delegate("#foo", "click", function(e) {
335 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
337 fakeClick( $anchor2 );
338 $anchor2.unbind( "click" );
339 $main.undelegate( "click" );
342 test("bind(), iframes", function() {
343 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
344 var doc = jQuery("#loadediframe").contents();
346 jQuery("div", doc).bind("click", function() {
347 ok( true, "Binding to element inside iframe" );
348 }).click().unbind('click');
351 test("bind(), trigger change on select", function() {
354 function selectOnChange(event) {
355 equals( event.data, counter++, "Event.data is not a global event object" );
357 jQuery("#form select").each(function(i){
358 jQuery(this).bind('change', i, selectOnChange);
359 }).trigger('change');
362 test("bind(), namespaced events, cloned events", function() {
365 jQuery("#firstp").bind("custom.test",function(e){
366 ok(true, "Custom event triggered");
369 jQuery("#firstp").bind("click",function(e){
370 ok(true, "Normal click triggered");
373 jQuery("#firstp").bind("click.test",function(e){
374 ok(true, "Namespaced click triggered");
377 // Trigger both bound fn (2)
378 jQuery("#firstp").trigger("click");
380 // Trigger one bound fn (1)
381 jQuery("#firstp").trigger("click.test");
383 // Remove only the one fn
384 jQuery("#firstp").unbind("click.test");
386 // Trigger the remaining fn (1)
387 jQuery("#firstp").trigger("click");
389 // Remove the remaining fn
390 jQuery("#firstp").unbind(".test");
392 // Trigger the remaining fn (0)
393 jQuery("#firstp").trigger("custom");
395 // using contents will get comments regular, text, and comment nodes
396 jQuery("#nonnodes").contents().bind("tester", function () {
397 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
398 }).trigger("tester");
400 // Make sure events stick with appendTo'd elements (which are cloned) #2027
401 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
402 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
405 test("bind(), multi-namespaced events", function() {
417 function check(name, msg){
418 same(name, order.shift(), msg);
421 jQuery("#firstp").bind("custom.test",function(e){
422 check("custom.test", "Custom event triggered");
425 jQuery("#firstp").bind("custom.test2",function(e){
426 check("custom.test2", "Custom event triggered");
429 jQuery("#firstp").bind("click.test",function(e){
430 check("click.test", "Normal click triggered");
433 jQuery("#firstp").bind("click.test.abc",function(e){
434 check("click.test.abc", "Namespaced click triggered");
437 // Those would not trigger/unbind (#5303)
438 jQuery("#firstp").trigger("click.a.test");
439 jQuery("#firstp").unbind("click.a.test");
441 // Trigger both bound fn (1)
442 jQuery("#firstp").trigger("click.test.abc");
444 // Trigger one bound fn (1)
445 jQuery("#firstp").trigger("click.abc");
447 // Trigger two bound fn (2)
448 jQuery("#firstp").trigger("click.test");
450 // Remove only the one fn
451 jQuery("#firstp").unbind("click.abc");
453 // Trigger the remaining fn (1)
454 jQuery("#firstp").trigger("click");
456 // Remove the remaining fn
457 jQuery("#firstp").unbind(".test");
459 // Trigger the remaining fn (1)
460 jQuery("#firstp").trigger("custom");
463 test("bind(), with same function", function() {
466 var count = 0 , func = function(){
470 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
471 jQuery("#liveHandlerOrder").trigger("foo.bar");
473 equals(count, 1, "Verify binding function with multiple namespaces." );
475 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
476 jQuery("#liveHandlerOrder").trigger("foo.bar");
478 equals(count, 1, "Verify that removing events still work." );
481 test("bind(), make sure order is maintained", function() {
484 var elem = jQuery("#firstp"), log = [], check = [];
486 for ( var i = 0; i < 100; i++ ) (function(i){
487 elem.bind( "click", function(){
494 elem.trigger("click");
496 equals( log.join(","), check.join(","), "Make sure order was maintained." );
498 elem.unbind("click");
501 test("bind(), with different this object", function() {
503 var thisObject = { myThis: true },
504 data = { myData: true },
505 handler1 = function( event ) {
506 equals( this, thisObject, "bind() with different this object" );
508 handler2 = function( event ) {
509 equals( this, thisObject, "bind() with different this object and data" );
510 equals( event.data, data, "bind() with different this object and data" );
514 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
515 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
517 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
520 test("bind(name, false), unbind(name, false)", function() {
524 jQuery("#main").bind("click", function(e){ main++; });
525 jQuery("#ap").trigger("click");
526 equals( main, 1, "Verify that the trigger happened correctly." );
529 jQuery("#ap").bind("click", false);
530 jQuery("#ap").trigger("click");
531 equals( main, 0, "Verify that no bubble happened." );
534 jQuery("#ap").unbind("click", false);
535 jQuery("#ap").trigger("click");
536 equals( main, 1, "Verify that the trigger happened correctly." );
538 // manually clean up events from elements outside the fixture
539 jQuery("#main").unbind("click");
542 test("bind()/trigger()/unbind() on plain object", function() {
547 // Make sure it doesn't complain when no events are found
548 jQuery(obj).trigger("test");
550 // Make sure it doesn't complain when no events are found
551 jQuery(obj).unbind("test");
555 ok( true, "Custom event run." );
558 ok( true, "Custom submit event run." );
562 var events = jQuery._data(obj, "events");
563 ok( events, "Object has events bound." );
564 equals( obj.events, undefined, "Events object on plain objects is not events" );
565 equals( typeof events, "function", "'events' expando is a function on plain objects." );
566 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
567 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
570 jQuery(obj).trigger("test");
571 jQuery(obj).trigger("submit");
573 jQuery(obj).unbind("test");
574 jQuery(obj).unbind("submit");
577 jQuery(obj).trigger("test");
579 // Make sure it doesn't complain when no events are found
580 jQuery(obj).unbind("test");
582 equals( obj && obj[ jQuery.expando ] &&
583 obj[ jQuery.expando ][ jQuery.expando ] &&
584 obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
587 test("unbind(type)", function() {
590 var $elem = jQuery("#firstp"),
594 ok( false, message );
597 message = "unbind passing function";
598 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
600 message = "unbind all from event";
601 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
603 message = "unbind all";
604 $elem.bind('error1', error).unbind().triggerHandler('error1');
606 message = "unbind many with function";
607 $elem.bind('error1 error2',error)
608 .unbind('error1 error2', error )
609 .trigger('error1').triggerHandler('error2');
611 message = "unbind many"; // #3538
612 $elem.bind('error1 error2',error)
613 .unbind('error1 error2')
614 .trigger('error1').triggerHandler('error2');
616 message = "unbind without a type or handler";
617 $elem.bind("error1 error2.test",error)
619 .trigger("error1").triggerHandler("error2");
622 test("unbind(eventObject)", function() {
625 var $elem = jQuery("#firstp"),
628 function assert( expected ){
630 $elem.trigger('foo').triggerHandler('bar');
631 equals( num, expected, "Check the right handlers are triggered" );
635 // This handler shouldn't be unbound
636 .bind('foo', function(){
639 .bind('foo', function(e){
644 .bind('bar', function(){
658 test("hover()", function() {
660 handler1 = function( event ) { ++times; },
661 handler2 = function( event ) { ++times; };
664 .hover(handler1, handler2)
665 .mouseenter().mouseleave()
666 .unbind("mouseenter", handler1)
667 .unbind("mouseleave", handler2)
669 .mouseenter().mouseleave()
670 .unbind("mouseenter mouseleave", handler1)
671 .mouseenter().mouseleave();
673 equals( times, 4, "hover handlers fired" );
676 test("trigger() shortcuts", function() {
679 var elem = jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL');
680 elem.find('a').bind('click', function() {
681 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
682 equals( close.length, 0, "Context element does not exist, length must be zero" );
683 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
687 // manually clean up detached elements
690 jQuery("#check1").click(function() {
691 ok( true, "click event handler for checkbox gets fired twice, see #815" );
695 jQuery('#firstp')[0].onclick = function(event) {
698 jQuery('#firstp').click();
699 equals( counter, 1, "Check that click, triggers onclick event handler also" );
701 var clickCounter = 0;
702 jQuery('#simon1')[0].onclick = function(event) {
705 jQuery('#simon1').click();
706 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
708 elem = jQuery('<img />').load(function(){
709 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
712 // manually clean up detached elements
716 test("trigger() bubbling", function() {
719 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
721 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
722 jQuery("html").bind("click", function(e){ html++; });
723 jQuery("body").bind("click", function(e){ body++; });
724 jQuery("#main").bind("click", function(e){ main++; });
725 jQuery("#ap").bind("click", function(){ ap++; return false; });
727 jQuery("html").trigger("click");
728 equals( doc, 1, "HTML bubble" );
729 equals( html, 1, "HTML bubble" );
731 jQuery("body").trigger("click");
732 equals( doc, 2, "Body bubble" );
733 equals( html, 2, "Body bubble" );
734 equals( body, 1, "Body bubble" );
736 jQuery("#main").trigger("click");
737 equals( doc, 3, "Main bubble" );
738 equals( html, 3, "Main bubble" );
739 equals( body, 2, "Main bubble" );
740 equals( main, 1, "Main bubble" );
742 jQuery("#ap").trigger("click");
743 equals( doc, 3, "ap bubble" );
744 equals( html, 3, "ap bubble" );
745 equals( body, 2, "ap bubble" );
746 equals( main, 1, "ap bubble" );
747 equals( ap, 1, "ap bubble" );
749 // manually clean up events from elements outside the fixture
750 jQuery(document).unbind("click");
751 jQuery("html, body, #main").unbind("click");
754 test("trigger(type, [data], [fn])", function() {
757 var handler = function(event, a, b, c) {
758 equals( event.type, "click", "check passed data" );
759 equals( a, 1, "check passed data" );
760 equals( b, "2", "check passed data" );
761 equals( c, "abc", "check passed data" );
765 var $elem = jQuery("#firstp");
767 // Simulate a "native" click
768 $elem[0].click = function(){
769 ok( true, "Native call was triggered" );
772 // Triggers handlrs and native
774 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
776 // Simulate a "native" click
777 $elem[0].click = function(){
778 ok( false, "Native call was triggered" );
781 // Trigger only the handlers (no native)
783 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
787 jQuery('#form input:first').hide().trigger('focus');
791 ok( pass, "Trigger focus on hidden element" );
795 jQuery('#main table:first').bind('test:test', function(){}).trigger('test:test');
799 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
801 var form = jQuery("<form action=''></form>").appendTo("body");
803 // Make sure it can be prevented locally
804 form.submit(function(){
805 ok( true, "Local bind still works." );
810 form.trigger("submit");
812 form.unbind("submit");
814 jQuery(document).submit(function(){
815 ok( true, "Make sure bubble works up to document." );
820 form.trigger("submit");
822 jQuery(document).unbind("submit");
827 test("jQuery.Event.currentTarget", function(){
830 test("trigger(eventObject, [data], [fn])", function() {
833 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
834 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
836 var event = jQuery.Event("noNew");
837 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
838 equals( event.type, "noNew", "Verify its type" );
840 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
841 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
842 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
844 event.preventDefault();
845 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
846 event.stopPropagation();
847 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
849 event.isPropagationStopped = function(){ return false };
850 event.stopImmediatePropagation();
851 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
852 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
854 $parent.bind('foo',function(e){
856 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
857 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
858 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
859 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
862 // test with an event object
863 event = new jQuery.Event("foo");
864 event.secret = 'boo!';
865 $child.trigger(event);
867 // test with a literal object
868 $child.trigger({type:'foo', secret:'boo!'});
873 ok( false, "This assertion shouldn't be reached");
876 $parent.bind('foo', error );
878 $child.bind('foo',function(e, a, b, c ){
879 equals( arguments.length, 4, "Check arguments length");
880 equals( a, 1, "Check first custom argument");
881 equals( b, 2, "Check second custom argument");
882 equals( c, 3, "Check third custom argument");
884 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
885 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
886 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
889 e.stopImmediatePropagation();
894 // We should add this back in when we want to test the order
895 // in which event handlers are iterated.
896 //$child.bind('foo', error );
898 event = new jQuery.Event("foo");
899 $child.trigger( event, [1,2,3] ).unbind();
900 equals( event.result, "result", "Check event.result attribute");
902 // Will error if it bubbles
903 $child.triggerHandler('foo');
906 $parent.unbind().remove();
909 test("jQuery.Event.currentTarget", function(){
913 $elem = jQuery('<button>a</button>').click(function(e){
914 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
918 $elem.trigger('click');
924 test("toggle(Function, Function, ...)", function() {
928 fn1 = function(e) { count++; },
929 fn2 = function(e) { count--; },
930 preventDefault = function(e) { e.preventDefault() },
931 link = jQuery('#mark');
932 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
933 equals( count, 1, "Check for toggle(fn, fn)" );
935 jQuery("#firstp").toggle(function () {
936 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
937 }, function() {}).trigger("click", [ 1, 2, 3 ]);
940 jQuery("#simon1").one("click", function() {
941 ok( true, "Execute event only once" );
942 jQuery(this).toggle(function() {
943 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
945 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
948 }).click().click().click();
963 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
965 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
967 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
969 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
971 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
973 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
975 $div.unbind('click',fns[0]);
976 var data = jQuery._data( $div[0], 'events' );
977 ok( !data, "Unbinding one function from toggle unbinds them all");
979 // manually clean up detached elements
982 // Test Multi-Toggles
984 $div = jQuery("<div/>");
985 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
987 same( a, [1], "Check that a click worked." );
989 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
991 same( a, [1,2], "Check that a click worked with a second toggle." );
992 same( b, [1], "Check that a click worked with a second toggle." );
995 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
996 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
998 // manually clean up detached elements
1002 test(".live()/.die()", function() {
1005 var submit = 0, div = 0, livea = 0, liveb = 0;
1007 jQuery("div").live("submit", function(){ submit++; return false; });
1008 jQuery("div").live("click", function(){ div++; });
1009 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
1010 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
1012 // Nothing should trigger on the body
1013 jQuery("body").trigger("click");
1014 equals( submit, 0, "Click on body" );
1015 equals( div, 0, "Click on body" );
1016 equals( livea, 0, "Click on body" );
1017 equals( liveb, 0, "Click on body" );
1019 // This should trigger two events
1020 submit = 0, div = 0, livea = 0, liveb = 0;
1021 jQuery("div#nothiddendiv").trigger("click");
1022 equals( submit, 0, "Click on div" );
1023 equals( div, 1, "Click on div" );
1024 equals( livea, 1, "Click on div" );
1025 equals( liveb, 0, "Click on div" );
1027 // This should trigger three events (w/ bubbling)
1028 submit = 0, div = 0, livea = 0, liveb = 0;
1029 jQuery("div#nothiddendivchild").trigger("click");
1030 equals( submit, 0, "Click on inner div" );
1031 equals( div, 2, "Click on inner div" );
1032 equals( livea, 1, "Click on inner div" );
1033 equals( liveb, 1, "Click on inner div" );
1035 // This should trigger one submit
1036 submit = 0, div = 0, livea = 0, liveb = 0;
1037 jQuery("div#nothiddendivchild").trigger("submit");
1038 equals( submit, 1, "Submit on div" );
1039 equals( div, 0, "Submit on div" );
1040 equals( livea, 0, "Submit on div" );
1041 equals( liveb, 0, "Submit on div" );
1043 // Make sure no other events were removed in the process
1044 submit = 0, div = 0, livea = 0, liveb = 0;
1045 jQuery("div#nothiddendivchild").trigger("click");
1046 equals( submit, 0, "die Click on inner div" );
1047 equals( div, 2, "die Click on inner div" );
1048 equals( livea, 1, "die Click on inner div" );
1049 equals( liveb, 1, "die Click on inner div" );
1051 // Now make sure that the removal works
1052 submit = 0, div = 0, livea = 0, liveb = 0;
1053 jQuery("div#nothiddendivchild").die("click");
1054 jQuery("div#nothiddendivchild").trigger("click");
1055 equals( submit, 0, "die Click on inner div" );
1056 equals( div, 2, "die Click on inner div" );
1057 equals( livea, 1, "die Click on inner div" );
1058 equals( liveb, 0, "die Click on inner div" );
1060 // Make sure that the click wasn't removed too early
1061 submit = 0, div = 0, livea = 0, liveb = 0;
1062 jQuery("div#nothiddendiv").trigger("click");
1063 equals( submit, 0, "die Click on inner div" );
1064 equals( div, 1, "die Click on inner div" );
1065 equals( livea, 1, "die Click on inner div" );
1066 equals( liveb, 0, "die Click on inner div" );
1068 // Make sure that stopPropgation doesn't stop live events
1069 submit = 0, div = 0, livea = 0, liveb = 0;
1070 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1071 jQuery("div#nothiddendivchild").trigger("click");
1072 equals( submit, 0, "stopPropagation Click on inner div" );
1073 equals( div, 1, "stopPropagation Click on inner div" );
1074 equals( livea, 0, "stopPropagation Click on inner div" );
1075 equals( liveb, 1, "stopPropagation Click on inner div" );
1077 // Make sure click events only fire with primary click
1078 submit = 0, div = 0, livea = 0, liveb = 0;
1079 var event = jQuery.Event("click");
1081 jQuery("div#nothiddendiv").trigger(event);
1083 equals( livea, 0, "live secondary click" );
1085 jQuery("div#nothiddendivchild").die("click");
1086 jQuery("div#nothiddendiv").die("click");
1087 jQuery("div").die("click");
1088 jQuery("div").die("submit");
1090 // Test binding with a different context
1091 var clicked = 0, container = jQuery('#main')[0];
1092 jQuery("#foo", container).live("click", function(e){ clicked++; });
1093 jQuery("div").trigger('click');
1094 jQuery("#foo").trigger('click');
1095 jQuery("#main").trigger('click');
1096 jQuery("body").trigger('click');
1097 equals( clicked, 2, "live with a context" );
1099 // Make sure the event is actually stored on the context
1100 ok( jQuery._data(container, "events").live, "live with a context" );
1102 // Test unbinding with a different context
1103 jQuery("#foo", container).die("click");
1104 jQuery("#foo").trigger('click');
1105 equals( clicked, 2, "die with a context");
1107 // Test binding with event data
1108 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1109 jQuery("#foo").trigger("click").die("click");
1111 // Test binding with trigger data
1112 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1113 jQuery("#foo").trigger("click", true).die("click");
1115 // Test binding with different this object
1116 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1117 jQuery("#foo").trigger("click").die("click");
1119 // Test binding with different this object, event data, and trigger data
1120 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1121 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1122 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1123 equals( data, true, "live with with different this object, event data, and trigger data")
1124 }, { foo: "bar" }));
1125 jQuery("#foo").trigger("click", true).die("click");
1127 // Verify that return false prevents default action
1128 jQuery("#anchor2").live("click", function(){ return false; });
1129 var hash = window.location.hash;
1130 jQuery("#anchor2").trigger("click");
1131 equals( window.location.hash, hash, "return false worked" );
1132 jQuery("#anchor2").die("click");
1134 // Verify that .preventDefault() prevents default action
1135 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1136 var hash = window.location.hash;
1137 jQuery("#anchor2").trigger("click");
1138 equals( window.location.hash, hash, "e.preventDefault() worked" );
1139 jQuery("#anchor2").die("click");
1141 // Test binding the same handler to multiple points
1143 function callback(){ called++; return false; }
1145 jQuery("#nothiddendiv").live("click", callback);
1146 jQuery("#anchor2").live("click", callback);
1148 jQuery("#nothiddendiv").trigger("click");
1149 equals( called, 1, "Verify that only one click occurred." );
1152 jQuery("#anchor2").trigger("click");
1153 equals( called, 1, "Verify that only one click occurred." );
1155 // Make sure that only one callback is removed
1156 jQuery("#anchor2").die("click", callback);
1159 jQuery("#nothiddendiv").trigger("click");
1160 equals( called, 1, "Verify that only one click occurred." );
1163 jQuery("#anchor2").trigger("click");
1164 equals( called, 0, "Verify that no click occurred." );
1166 // Make sure that it still works if the selector is the same,
1167 // but the event type is different
1168 jQuery("#nothiddendiv").live("foo", callback);
1171 jQuery("#nothiddendiv").die("click", callback);
1174 jQuery("#nothiddendiv").trigger("click");
1175 equals( called, 0, "Verify that no click occurred." );
1178 jQuery("#nothiddendiv").trigger("foo");
1179 equals( called, 1, "Verify that one foo occurred." );
1182 jQuery("#nothiddendiv").die("foo", callback);
1184 // Make sure we don't loose the target by DOM modifications
1185 // after the bubble already reached the liveHandler
1186 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1188 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1189 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1191 jQuery("#nothiddendiv span").click();
1192 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1193 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1196 jQuery("#nothiddendivchild").die("click");
1198 // Verify that .live() ocurs and cancel buble in the same order as
1199 // we would expect .bind() and .click() without delegation
1200 var lived = 0, livee = 0;
1202 // bind one pair in one order
1203 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1204 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1206 jQuery('span#liveSpan1 a').click();
1207 equals( lived, 1, "Verify that only one first handler occurred." );
1208 equals( livee, 0, "Verify that second handler doesn't." );
1210 // and one pair in inverse
1211 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1212 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1216 jQuery('span#liveSpan2 a').click();
1217 equals( lived, 1, "Verify that only one first handler occurred." );
1218 equals( livee, 0, "Verify that second handler doesn't." );
1221 jQuery("span#liveSpan1 a").die("click")
1222 jQuery("span#liveSpan1").die("click");
1223 jQuery("span#liveSpan2 a").die("click");
1224 jQuery("span#liveSpan2").die("click");
1226 // Test this, target and currentTarget are correct
1227 jQuery('span#liveSpan1').live('click', function(e){
1228 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1229 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1230 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1233 jQuery('span#liveSpan1 a').click();
1235 jQuery('span#liveSpan1').die('click');
1237 // Work with deep selectors
1240 function clickB(){ livee++; }
1242 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1243 jQuery("#nothiddendiv div").live("click", clickB);
1244 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1246 equals( livee, 0, "No clicks, deep selector." );
1249 jQuery("#nothiddendivchild").trigger("click");
1250 equals( livee, 2, "Click, deep selector." );
1253 jQuery("#nothiddendivchild").trigger("mouseover");
1254 equals( livee, 1, "Mouseover, deep selector." );
1256 jQuery("#nothiddendiv div").die("mouseover");
1259 jQuery("#nothiddendivchild").trigger("click");
1260 equals( livee, 2, "Click, deep selector." );
1263 jQuery("#nothiddendivchild").trigger("mouseover");
1264 equals( livee, 0, "Mouseover, deep selector." );
1266 jQuery("#nothiddendiv div").die("click", clickB);
1269 jQuery("#nothiddendivchild").trigger("click");
1270 equals( livee, 1, "Click, deep selector." );
1272 jQuery("#nothiddendiv div").die("click");
1274 jQuery("#nothiddendiv div").live("blur", function(){
1275 ok( true, "Live div trigger blur." );
1278 jQuery("#nothiddendiv div").trigger("blur");
1280 jQuery("#nothiddendiv div").die("blur");
1283 test("die all bound events", function(){
1287 var div = jQuery("div#nothiddendivchild");
1289 div.live("click submit", function(){ count++; });
1292 div.trigger("click");
1293 div.trigger("submit");
1295 equals( count, 0, "Make sure no events were triggered." );
1298 test("live with multiple events", function(){
1302 var div = jQuery("div#nothiddendivchild");
1304 div.live("click submit", function(){ count++; });
1306 div.trigger("click");
1307 div.trigger("submit");
1309 equals( count, 2, "Make sure both the click and submit were triggered." );
1311 // manually clean up events from elements outside the fixture
1315 test("live with namespaces", function(){
1318 var count1 = 0, count2 = 0;
1320 jQuery("#liveSpan1").live("foo.bar", function(e){
1324 jQuery("#liveSpan1").live("foo.zed", function(e){
1328 jQuery("#liveSpan1").trigger("foo.bar");
1329 equals( count1, 1, "Got live foo.bar" );
1330 equals( count2, 0, "Got live foo.bar" );
1332 count1 = 0, count2 = 0;
1334 jQuery("#liveSpan1").trigger("foo.zed");
1335 equals( count1, 0, "Got live foo.zed" );
1336 equals( count2, 1, "Got live foo.zed" );
1339 count1 = 0, count2 = 0;
1341 jQuery("#liveSpan1").die("foo.zed");
1342 jQuery("#liveSpan1").trigger("foo.bar");
1344 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1345 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1347 count1 = 0, count2 = 0;
1349 jQuery("#liveSpan1").trigger("foo.zed");
1350 equals( count1, 0, "Got live foo.zed" );
1351 equals( count2, 0, "Got live foo.zed" );
1354 jQuery("#liveSpan1").die("foo.bar");
1356 count1 = 0, count2 = 0;
1358 jQuery("#liveSpan1").trigger("foo.bar");
1359 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1360 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1362 jQuery("#liveSpan1").trigger("foo.zed");
1363 equals( count1, 0, "Did not trigger foo.zed again" );
1364 equals( count2, 0, "Did not trigger foo.zed again" );
1367 test("live with change", function(){
1370 var selectChange = 0, checkboxChange = 0;
1372 var select = jQuery("select[name='S1']")
1373 select.live("change", function() {
1377 var checkbox = jQuery("#check2"),
1378 checkboxFunction = function(){
1381 checkbox.live("change", checkboxFunction);
1383 // test click on select
1385 // second click that changed it
1387 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1388 select.trigger("change");
1389 equals( selectChange, 1, "Change on click." );
1391 // test keys on select
1393 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1394 select.trigger("change");
1395 equals( selectChange, 1, "Change on keyup." );
1397 // test click on checkbox
1398 checkbox.trigger("change");
1399 equals( checkboxChange, 1, "Change on checkbox." );
1401 // test blur/focus on text
1402 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1403 text.live("change", function() {
1407 text.val(oldTextVal+"foo");
1408 text.trigger("change");
1409 equals( textChange, 1, "Change on text input." );
1411 text.val(oldTextVal);
1414 // test blur/focus on password
1415 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1416 password.live("change", function() {
1420 password.val(oldPasswordVal + "foo");
1421 password.trigger("change");
1422 equals( passwordChange, 1, "Change on password input." );
1424 password.val(oldPasswordVal);
1425 password.die("change");
1427 // make sure die works
1431 select.die("change");
1432 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1433 select.trigger("change");
1434 equals( selectChange, 0, "Die on click works." );
1437 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1438 select.trigger("change");
1439 equals( selectChange, 0, "Die on keyup works." );
1441 // die specific checkbox
1442 checkbox.die("change", checkboxFunction);
1443 checkbox.trigger("change");
1444 equals( checkboxChange, 1, "Die on checkbox." );
1447 test("live with submit", function() {
1448 var count1 = 0, count2 = 0;
1450 jQuery("#testForm").live("submit", function(ev) {
1452 ev.preventDefault();
1455 jQuery("body").live("submit", function(ev) {
1457 ev.preventDefault();
1460 jQuery("#testForm input[name=sub1]").submit();
1461 equals( count1, 1, "Verify form submit." );
1462 equals( count2, 1, "Verify body submit." );
1464 jQuery("#testForm").die("submit");
1465 jQuery("body").die("submit");
1468 test("live with special events", function() {
1471 jQuery.event.special.foo = {
1472 setup: function( data, namespaces, handler ) {
1473 ok( true, "Setup run." );
1475 teardown: function( namespaces ) {
1476 ok( true, "Teardown run." );
1478 add: function( handleObj ) {
1479 ok( true, "Add run." );
1481 remove: function( handleObj ) {
1482 ok( true, "Remove run." );
1484 _default: function( event ) {
1485 ok( true, "Default run." );
1490 jQuery("#liveSpan1").live("foo.a", function(e){
1491 ok( true, "Handler 1 run." );
1495 jQuery("#liveSpan1").live("foo.b", function(e){
1496 ok( true, "Handler 2 run." );
1499 // Run: Handler 1, Handler 2, Default
1500 jQuery("#liveSpan1").trigger("foo");
1502 // Run: Handler 1, Default
1503 // TODO: Namespace doesn't trigger default (?)
1504 jQuery("#liveSpan1").trigger("foo.a");
1507 jQuery("#liveSpan1").die("foo.a");
1509 // Run: Handler 2, Default
1510 jQuery("#liveSpan1").trigger("foo");
1512 // Run: remove, teardown
1513 jQuery("#liveSpan1").die("foo");
1515 delete jQuery.event.special.foo;
1518 test(".delegate()/.undelegate()", function() {
1521 var submit = 0, div = 0, livea = 0, liveb = 0;
1523 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1524 jQuery("#body").delegate("div", "click", function(){ div++; });
1525 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1526 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1528 // Nothing should trigger on the body
1529 jQuery("body").trigger("click");
1530 equals( submit, 0, "Click on body" );
1531 equals( div, 0, "Click on body" );
1532 equals( livea, 0, "Click on body" );
1533 equals( liveb, 0, "Click on body" );
1535 // This should trigger two events
1536 submit = 0, div = 0, livea = 0, liveb = 0;
1537 jQuery("div#nothiddendiv").trigger("click");
1538 equals( submit, 0, "Click on div" );
1539 equals( div, 1, "Click on div" );
1540 equals( livea, 1, "Click on div" );
1541 equals( liveb, 0, "Click on div" );
1543 // This should trigger three events (w/ bubbling)
1544 submit = 0, div = 0, livea = 0, liveb = 0;
1545 jQuery("div#nothiddendivchild").trigger("click");
1546 equals( submit, 0, "Click on inner div" );
1547 equals( div, 2, "Click on inner div" );
1548 equals( livea, 1, "Click on inner div" );
1549 equals( liveb, 1, "Click on inner div" );
1551 // This should trigger one submit
1552 submit = 0, div = 0, livea = 0, liveb = 0;
1553 jQuery("div#nothiddendivchild").trigger("submit");
1554 equals( submit, 1, "Submit on div" );
1555 equals( div, 0, "Submit on div" );
1556 equals( livea, 0, "Submit on div" );
1557 equals( liveb, 0, "Submit on div" );
1559 // Make sure no other events were removed in the process
1560 submit = 0, div = 0, livea = 0, liveb = 0;
1561 jQuery("div#nothiddendivchild").trigger("click");
1562 equals( submit, 0, "undelegate Click on inner div" );
1563 equals( div, 2, "undelegate Click on inner div" );
1564 equals( livea, 1, "undelegate Click on inner div" );
1565 equals( liveb, 1, "undelegate Click on inner div" );
1567 // Now make sure that the removal works
1568 submit = 0, div = 0, livea = 0, liveb = 0;
1569 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1570 jQuery("div#nothiddendivchild").trigger("click");
1571 equals( submit, 0, "undelegate Click on inner div" );
1572 equals( div, 2, "undelegate Click on inner div" );
1573 equals( livea, 1, "undelegate Click on inner div" );
1574 equals( liveb, 0, "undelegate Click on inner div" );
1576 // Make sure that the click wasn't removed too early
1577 submit = 0, div = 0, livea = 0, liveb = 0;
1578 jQuery("div#nothiddendiv").trigger("click");
1579 equals( submit, 0, "undelegate Click on inner div" );
1580 equals( div, 1, "undelegate Click on inner div" );
1581 equals( livea, 1, "undelegate Click on inner div" );
1582 equals( liveb, 0, "undelegate Click on inner div" );
1584 // Make sure that stopPropgation doesn't stop live events
1585 submit = 0, div = 0, livea = 0, liveb = 0;
1586 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1587 jQuery("div#nothiddendivchild").trigger("click");
1588 equals( submit, 0, "stopPropagation Click on inner div" );
1589 equals( div, 1, "stopPropagation Click on inner div" );
1590 equals( livea, 0, "stopPropagation Click on inner div" );
1591 equals( liveb, 1, "stopPropagation Click on inner div" );
1593 // Make sure click events only fire with primary click
1594 submit = 0, div = 0, livea = 0, liveb = 0;
1595 var event = jQuery.Event("click");
1597 jQuery("div#nothiddendiv").trigger(event);
1599 equals( livea, 0, "delegate secondary click" );
1601 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1602 jQuery("#body").undelegate("div#nothiddendiv", "click");
1603 jQuery("#body").undelegate("div", "click");
1604 jQuery("#body").undelegate("div", "submit");
1606 // Test binding with a different context
1607 var clicked = 0, container = jQuery('#main')[0];
1608 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1609 jQuery("div").trigger('click');
1610 jQuery("#foo").trigger('click');
1611 jQuery("#main").trigger('click');
1612 jQuery("body").trigger('click');
1613 equals( clicked, 2, "delegate with a context" );
1615 // Make sure the event is actually stored on the context
1616 ok( jQuery._data(container, "events").live, "delegate with a context" );
1618 // Test unbinding with a different context
1619 jQuery("#main").undelegate("#foo", "click");
1620 jQuery("#foo").trigger('click');
1621 equals( clicked, 2, "undelegate with a context");
1623 // Test binding with event data
1624 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1625 jQuery("#foo").trigger("click");
1626 jQuery("#body").undelegate("#foo", "click");
1628 // Test binding with trigger data
1629 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1630 jQuery("#foo").trigger("click", true);
1631 jQuery("#body").undelegate("#foo", "click");
1633 // Test binding with different this object
1634 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1635 jQuery("#foo").trigger("click");
1636 jQuery("#body").undelegate("#foo", "click");
1638 // Test binding with different this object, event data, and trigger data
1639 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1640 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1641 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1642 equals( data, true, "delegate with with different this object, event data, and trigger data")
1643 }, { foo: "bar" }));
1644 jQuery("#foo").trigger("click", true);
1645 jQuery("#body").undelegate("#foo", "click");
1647 // Verify that return false prevents default action
1648 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1649 var hash = window.location.hash;
1650 jQuery("#anchor2").trigger("click");
1651 equals( window.location.hash, hash, "return false worked" );
1652 jQuery("#body").undelegate("#anchor2", "click");
1654 // Verify that .preventDefault() prevents default action
1655 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1656 var hash = window.location.hash;
1657 jQuery("#anchor2").trigger("click");
1658 equals( window.location.hash, hash, "e.preventDefault() worked" );
1659 jQuery("#body").undelegate("#anchor2", "click");
1661 // Test binding the same handler to multiple points
1663 function callback(){ called++; return false; }
1665 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1666 jQuery("#body").delegate("#anchor2", "click", callback);
1668 jQuery("#nothiddendiv").trigger("click");
1669 equals( called, 1, "Verify that only one click occurred." );
1672 jQuery("#anchor2").trigger("click");
1673 equals( called, 1, "Verify that only one click occurred." );
1675 // Make sure that only one callback is removed
1676 jQuery("#body").undelegate("#anchor2", "click", callback);
1679 jQuery("#nothiddendiv").trigger("click");
1680 equals( called, 1, "Verify that only one click occurred." );
1683 jQuery("#anchor2").trigger("click");
1684 equals( called, 0, "Verify that no click occurred." );
1686 // Make sure that it still works if the selector is the same,
1687 // but the event type is different
1688 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1691 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1694 jQuery("#nothiddendiv").trigger("click");
1695 equals( called, 0, "Verify that no click occurred." );
1698 jQuery("#nothiddendiv").trigger("foo");
1699 equals( called, 1, "Verify that one foo occurred." );
1702 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1704 // Make sure we don't loose the target by DOM modifications
1705 // after the bubble already reached the liveHandler
1706 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1708 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1709 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1711 jQuery("#nothiddendiv span").click();
1712 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1713 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1716 jQuery("#body").undelegate("#nothiddendivchild", "click");
1718 // Verify that .live() ocurs and cancel buble in the same order as
1719 // we would expect .bind() and .click() without delegation
1720 var lived = 0, livee = 0;
1722 // bind one pair in one order
1723 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1724 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1726 jQuery('span#liveSpan1 a').click();
1727 equals( lived, 1, "Verify that only one first handler occurred." );
1728 equals( livee, 0, "Verify that second handler doesn't." );
1730 // and one pair in inverse
1731 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1732 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1736 jQuery('span#liveSpan2 a').click();
1737 equals( lived, 1, "Verify that only one first handler occurred." );
1738 equals( livee, 0, "Verify that second handler doesn't." );
1741 jQuery("#body").undelegate("click");
1743 // Test this, target and currentTarget are correct
1744 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1745 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1746 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1747 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1750 jQuery('span#liveSpan1 a').click();
1752 jQuery("#body").undelegate('span#liveSpan1', 'click');
1754 // Work with deep selectors
1757 function clickB(){ livee++; }
1759 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1760 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1761 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1763 equals( livee, 0, "No clicks, deep selector." );
1766 jQuery("#nothiddendivchild").trigger("click");
1767 equals( livee, 2, "Click, deep selector." );
1770 jQuery("#nothiddendivchild").trigger("mouseover");
1771 equals( livee, 1, "Mouseover, deep selector." );
1773 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1776 jQuery("#nothiddendivchild").trigger("click");
1777 equals( livee, 2, "Click, deep selector." );
1780 jQuery("#nothiddendivchild").trigger("mouseover");
1781 equals( livee, 0, "Mouseover, deep selector." );
1783 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1786 jQuery("#nothiddendivchild").trigger("click");
1787 equals( livee, 1, "Click, deep selector." );
1789 jQuery("#body").undelegate("#nothiddendiv div", "click");
1792 test("undelegate all bound events", function(){
1796 var div = jQuery("#body");
1798 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1801 jQuery("div#nothiddendivchild").trigger("click");
1802 jQuery("div#nothiddendivchild").trigger("submit");
1804 equals( count, 0, "Make sure no events were triggered." );
1807 test("delegate with multiple events", function(){
1811 var div = jQuery("#body");
1813 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1815 jQuery("div#nothiddendivchild").trigger("click");
1816 jQuery("div#nothiddendivchild").trigger("submit");
1818 equals( count, 2, "Make sure both the click and submit were triggered." );
1820 jQuery("#body").undelegate();
1823 test("delegate with change", function(){
1826 var selectChange = 0, checkboxChange = 0;
1828 var select = jQuery("select[name='S1']");
1829 jQuery("#body").delegate("select[name='S1']", "change", function() {
1833 var checkbox = jQuery("#check2"),
1834 checkboxFunction = function(){
1837 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1839 // test click on select
1841 // second click that changed it
1843 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1844 select.trigger("change");
1845 equals( selectChange, 1, "Change on click." );
1847 // test keys on select
1849 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1850 select.trigger("change");
1851 equals( selectChange, 1, "Change on keyup." );
1853 // test click on checkbox
1854 checkbox.trigger("change");
1855 equals( checkboxChange, 1, "Change on checkbox." );
1857 // test blur/focus on text
1858 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1859 jQuery("#body").delegate("#name", "change", function() {
1863 text.val(oldTextVal+"foo");
1864 text.trigger("change");
1865 equals( textChange, 1, "Change on text input." );
1867 text.val(oldTextVal);
1868 jQuery("#body").die("change");
1870 // test blur/focus on password
1871 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1872 jQuery("#body").delegate("#name", "change", function() {
1876 password.val(oldPasswordVal + "foo");
1877 password.trigger("change");
1878 equals( passwordChange, 1, "Change on password input." );
1880 password.val(oldPasswordVal);
1881 jQuery("#body").undelegate("#name", "change");
1883 // make sure die works
1887 jQuery("#body").undelegate("select[name='S1']", "change");
1888 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1889 select.trigger("change");
1890 equals( selectChange, 0, "Die on click works." );
1893 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1894 select.trigger("change");
1895 equals( selectChange, 0, "Die on keyup works." );
1897 // die specific checkbox
1898 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1899 checkbox.trigger("change");
1900 equals( checkboxChange, 1, "Die on checkbox." );
1903 test("delegate with submit", function() {
1904 var count1 = 0, count2 = 0;
1906 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1908 ev.preventDefault();
1911 jQuery(document).delegate("body", "submit", function(ev) {
1913 ev.preventDefault();
1916 jQuery("#testForm input[name=sub1]").submit();
1917 equals( count1, 1, "Verify form submit." );
1918 equals( count2, 1, "Verify body submit." );
1920 jQuery("#body").undelegate();
1921 jQuery(document).undelegate();
1924 test("Non DOM element events", function() {
1929 jQuery(o).bind('nonelementobj', function(e) {
1930 ok( true, "Event on non-DOM object triggered" );
1933 jQuery(o).trigger('nonelementobj');
1936 test("window resize", function() {
1939 jQuery(window).unbind();
1941 jQuery(window).bind("resize", function(){
1942 ok( true, "Resize event fired." );
1943 }).resize().unbind("resize");
1945 ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
1948 test("focusin bubbles", function() {
1949 //create an input and focusin on it
1950 var input = jQuery("<input/>"), order = 0;
1952 input.prependTo("body");
1954 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1955 equals(1,order++,"focusin on the body second")
1958 input.bind("focusin.focusinBubblesTest",function(){
1959 equals(0,order++,"focusin on the element first")
1965 jQuery("body").unbind("focusin.focusinBubblesTest");
1969 test("jQuery(function($) {})", function() {
1971 jQuery(function($) {
1972 equals(jQuery, $, "ready doesn't provide an event object, instead it provides a reference to the jQuery function, see http://docs.jquery.com/Events/ready#fn");
1977 test("event properties", function() {
1979 jQuery("#simon1").click(function(event) {
1980 ok( event.timeStamp, "assert event.timeStamp is present" );