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", 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 var orig = e.originalEvent;
328 if ( typeof(orig.defaultPrevented) === "boolean" || typeof(orig.returnValue) === "boolean" || orig.getPreventDefault ) {
329 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
332 // Opera < 11 doesn't implement any interface we can use, so give it a pass
333 ok( true, "isDefaultPrevented not supported by this browser, test skipped" );
336 fakeClick( $anchor2 );
337 $anchor2.unbind( "click" );
338 $main.undelegate( "click" );
339 $anchor2.click(function(e) {
340 // Let the default action occur
342 $main.delegate("#foo", "click", function(e) {
343 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
345 fakeClick( $anchor2 );
346 $anchor2.unbind( "click" );
347 $main.undelegate( "click" );
350 test("bind(), iframes", function() {
351 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
352 var doc = jQuery("#loadediframe").contents();
354 jQuery("div", doc).bind("click", function() {
355 ok( true, "Binding to element inside iframe" );
356 }).click().unbind('click');
359 test("bind(), trigger change on select", function() {
362 function selectOnChange(event) {
363 equals( event.data, counter++, "Event.data is not a global event object" );
365 jQuery("#form select").each(function(i){
366 jQuery(this).bind('change', i, selectOnChange);
367 }).trigger('change');
370 test("bind(), namespaced events, cloned events", function() {
373 jQuery("#firstp").bind("custom.test",function(e){
374 ok(true, "Custom event triggered");
377 jQuery("#firstp").bind("click",function(e){
378 ok(true, "Normal click triggered");
381 jQuery("#firstp").bind("click.test",function(e){
382 ok(true, "Namespaced click triggered");
385 // Trigger both bound fn (2)
386 jQuery("#firstp").trigger("click");
388 // Trigger one bound fn (1)
389 jQuery("#firstp").trigger("click.test");
391 // Remove only the one fn
392 jQuery("#firstp").unbind("click.test");
394 // Trigger the remaining fn (1)
395 jQuery("#firstp").trigger("click");
397 // Remove the remaining fn
398 jQuery("#firstp").unbind(".test");
400 // Trigger the remaining fn (0)
401 jQuery("#firstp").trigger("custom");
403 // using contents will get comments regular, text, and comment nodes
404 jQuery("#nonnodes").contents().bind("tester", function () {
405 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
406 }).trigger("tester");
408 // Make sure events stick with appendTo'd elements (which are cloned) #2027
409 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
410 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
413 test("bind(), multi-namespaced events", function() {
425 function check(name, msg){
426 same(name, order.shift(), msg);
429 jQuery("#firstp").bind("custom.test",function(e){
430 check("custom.test", "Custom event triggered");
433 jQuery("#firstp").bind("custom.test2",function(e){
434 check("custom.test2", "Custom event triggered");
437 jQuery("#firstp").bind("click.test",function(e){
438 check("click.test", "Normal click triggered");
441 jQuery("#firstp").bind("click.test.abc",function(e){
442 check("click.test.abc", "Namespaced click triggered");
445 // Those would not trigger/unbind (#5303)
446 jQuery("#firstp").trigger("click.a.test");
447 jQuery("#firstp").unbind("click.a.test");
449 // Trigger both bound fn (1)
450 jQuery("#firstp").trigger("click.test.abc");
452 // Trigger one bound fn (1)
453 jQuery("#firstp").trigger("click.abc");
455 // Trigger two bound fn (2)
456 jQuery("#firstp").trigger("click.test");
458 // Remove only the one fn
459 jQuery("#firstp").unbind("click.abc");
461 // Trigger the remaining fn (1)
462 jQuery("#firstp").trigger("click");
464 // Remove the remaining fn
465 jQuery("#firstp").unbind(".test");
467 // Trigger the remaining fn (1)
468 jQuery("#firstp").trigger("custom");
471 test("bind(), with same function", function() {
474 var count = 0 , func = function(){
478 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
479 jQuery("#liveHandlerOrder").trigger("foo.bar");
481 equals(count, 1, "Verify binding function with multiple namespaces." );
483 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
484 jQuery("#liveHandlerOrder").trigger("foo.bar");
486 equals(count, 1, "Verify that removing events still work." );
489 test("bind(), make sure order is maintained", function() {
492 var elem = jQuery("#firstp"), log = [], check = [];
494 for ( var i = 0; i < 100; i++ ) (function(i){
495 elem.bind( "click", function(){
502 elem.trigger("click");
504 equals( log.join(","), check.join(","), "Make sure order was maintained." );
506 elem.unbind("click");
509 test("bind(), with different this object", function() {
511 var thisObject = { myThis: true },
512 data = { myData: true },
513 handler1 = function( event ) {
514 equals( this, thisObject, "bind() with different this object" );
516 handler2 = function( event ) {
517 equals( this, thisObject, "bind() with different this object and data" );
518 equals( event.data, data, "bind() with different this object and data" );
522 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
523 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
525 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
528 test("bind(name, false), unbind(name, false)", function() {
532 jQuery("#main").bind("click", function(e){ main++; });
533 jQuery("#ap").trigger("click");
534 equals( main, 1, "Verify that the trigger happened correctly." );
537 jQuery("#ap").bind("click", false);
538 jQuery("#ap").trigger("click");
539 equals( main, 0, "Verify that no bubble happened." );
542 jQuery("#ap").unbind("click", false);
543 jQuery("#ap").trigger("click");
544 equals( main, 1, "Verify that the trigger happened correctly." );
546 // manually clean up events from elements outside the fixture
547 jQuery("#main").unbind("click");
550 test("bind()/trigger()/unbind() on plain object", function() {
555 // Make sure it doesn't complain when no events are found
556 jQuery(obj).trigger("test");
558 // Make sure it doesn't complain when no events are found
559 jQuery(obj).unbind("test");
563 ok( true, "Custom event run." );
566 ok( true, "Custom submit event run." );
570 var events = jQuery._data(obj, "events");
571 ok( events, "Object has events bound." );
572 equals( obj.events, undefined, "Events object on plain objects is not events" );
573 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
574 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
577 jQuery(obj).trigger("test");
578 jQuery(obj).trigger("submit");
580 jQuery(obj).unbind("test");
581 jQuery(obj).unbind("submit");
584 jQuery(obj).trigger("test");
586 // Make sure it doesn't complain when no events are found
587 jQuery(obj).unbind("test");
589 equals( obj && obj[ jQuery.expando ] &&
590 obj[ jQuery.expando ][ jQuery.expando ] &&
591 obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
594 test("unbind(type)", function() {
597 var $elem = jQuery("#firstp"),
601 ok( false, message );
604 message = "unbind passing function";
605 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
607 message = "unbind all from event";
608 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
610 message = "unbind all";
611 $elem.bind('error1', error).unbind().triggerHandler('error1');
613 message = "unbind many with function";
614 $elem.bind('error1 error2',error)
615 .unbind('error1 error2', error )
616 .trigger('error1').triggerHandler('error2');
618 message = "unbind many"; // #3538
619 $elem.bind('error1 error2',error)
620 .unbind('error1 error2')
621 .trigger('error1').triggerHandler('error2');
623 message = "unbind without a type or handler";
624 $elem.bind("error1 error2.test",error)
626 .trigger("error1").triggerHandler("error2");
629 test("unbind(eventObject)", function() {
632 var $elem = jQuery("#firstp"),
635 function assert( expected ){
637 $elem.trigger('foo').triggerHandler('bar');
638 equals( num, expected, "Check the right handlers are triggered" );
642 // This handler shouldn't be unbound
643 .bind('foo', function(){
646 .bind('foo', function(e){
651 .bind('bar', function(){
665 test("hover()", function() {
667 handler1 = function( event ) { ++times; },
668 handler2 = function( event ) { ++times; };
671 .hover(handler1, handler2)
672 .mouseenter().mouseleave()
673 .unbind("mouseenter", handler1)
674 .unbind("mouseleave", handler2)
676 .mouseenter().mouseleave()
677 .unbind("mouseenter mouseleave", handler1)
678 .mouseenter().mouseleave();
680 equals( times, 4, "hover handlers fired" );
683 test("trigger() shortcuts", function() {
686 var elem = jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL');
687 elem.find('a').bind('click', function() {
688 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
689 equals( close.length, 0, "Context element does not exist, length must be zero" );
690 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
694 // manually clean up detached elements
697 jQuery("#check1").click(function() {
698 ok( true, "click event handler for checkbox gets fired twice, see #815" );
702 jQuery('#firstp')[0].onclick = function(event) {
705 jQuery('#firstp').click();
706 equals( counter, 1, "Check that click, triggers onclick event handler also" );
708 var clickCounter = 0;
709 jQuery('#simon1')[0].onclick = function(event) {
712 jQuery('#simon1').click();
713 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
715 elem = jQuery('<img />').load(function(){
716 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
719 // manually clean up detached elements
723 test("trigger() bubbling", function() {
726 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
728 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
729 jQuery("html").bind("click", function(e){ html++; });
730 jQuery("body").bind("click", function(e){ body++; });
731 jQuery("#main").bind("click", function(e){ main++; });
732 jQuery("#ap").bind("click", function(){ ap++; return false; });
734 jQuery("html").trigger("click");
735 equals( doc, 1, "HTML bubble" );
736 equals( html, 1, "HTML bubble" );
738 jQuery("body").trigger("click");
739 equals( doc, 2, "Body bubble" );
740 equals( html, 2, "Body bubble" );
741 equals( body, 1, "Body bubble" );
743 jQuery("#main").trigger("click");
744 equals( doc, 3, "Main bubble" );
745 equals( html, 3, "Main bubble" );
746 equals( body, 2, "Main bubble" );
747 equals( main, 1, "Main bubble" );
749 jQuery("#ap").trigger("click");
750 equals( doc, 3, "ap bubble" );
751 equals( html, 3, "ap bubble" );
752 equals( body, 2, "ap bubble" );
753 equals( main, 1, "ap bubble" );
754 equals( ap, 1, "ap bubble" );
756 // manually clean up events from elements outside the fixture
757 jQuery(document).unbind("click");
758 jQuery("html, body, #main").unbind("click");
761 test("trigger(type, [data], [fn])", function() {
764 var handler = function(event, a, b, c) {
765 equals( event.type, "click", "check passed data" );
766 equals( a, 1, "check passed data" );
767 equals( b, "2", "check passed data" );
768 equals( c, "abc", "check passed data" );
772 var $elem = jQuery("#firstp");
774 // Simulate a "native" click
775 $elem[0].click = function(){
776 ok( true, "Native call was triggered" );
779 // Triggers handlrs and native
781 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
783 // Simulate a "native" click
784 $elem[0].click = function(){
785 ok( false, "Native call was triggered" );
788 // Trigger only the handlers (no native)
790 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
794 jQuery('#form input:first').hide().trigger('focus');
798 ok( pass, "Trigger focus on hidden element" );
802 jQuery('#main table:first').bind('test:test', function(){}).trigger('test:test');
806 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
808 var form = jQuery("<form action=''></form>").appendTo("body");
810 // Make sure it can be prevented locally
811 form.submit(function(){
812 ok( true, "Local bind still works." );
817 form.trigger("submit");
819 form.unbind("submit");
821 jQuery(document).submit(function(){
822 ok( true, "Make sure bubble works up to document." );
827 form.trigger("submit");
829 jQuery(document).unbind("submit");
834 test("jQuery.Event.currentTarget", function(){
837 test("trigger(eventObject, [data], [fn])", function() {
840 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
841 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
843 var event = jQuery.Event("noNew");
844 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
845 equals( event.type, "noNew", "Verify its type" );
847 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
848 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
849 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
851 event.preventDefault();
852 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
853 event.stopPropagation();
854 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
856 event.isPropagationStopped = function(){ return false };
857 event.stopImmediatePropagation();
858 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
859 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
861 $parent.bind('foo',function(e){
863 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
864 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
865 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
866 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
869 // test with an event object
870 event = new jQuery.Event("foo");
871 event.secret = 'boo!';
872 $child.trigger(event);
874 // test with a literal object
875 $child.trigger({type:'foo', secret:'boo!'});
880 ok( false, "This assertion shouldn't be reached");
883 $parent.bind('foo', error );
885 $child.bind('foo',function(e, a, b, c ){
886 equals( arguments.length, 4, "Check arguments length");
887 equals( a, 1, "Check first custom argument");
888 equals( b, 2, "Check second custom argument");
889 equals( c, 3, "Check third custom argument");
891 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
892 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
893 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
896 e.stopImmediatePropagation();
901 // We should add this back in when we want to test the order
902 // in which event handlers are iterated.
903 //$child.bind('foo', error );
905 event = new jQuery.Event("foo");
906 $child.trigger( event, [1,2,3] ).unbind();
907 equals( event.result, "result", "Check event.result attribute");
909 // Will error if it bubbles
910 $child.triggerHandler('foo');
913 $parent.unbind().remove();
916 test("jQuery.Event.currentTarget", function(){
920 $elem = jQuery('<button>a</button>').click(function(e){
921 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
925 $elem.trigger('click');
931 test("toggle(Function, Function, ...)", function() {
935 fn1 = function(e) { count++; },
936 fn2 = function(e) { count--; },
937 preventDefault = function(e) { e.preventDefault() },
938 link = jQuery('#mark');
939 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
940 equals( count, 1, "Check for toggle(fn, fn)" );
942 jQuery("#firstp").toggle(function () {
943 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
944 }, function() {}).trigger("click", [ 1, 2, 3 ]);
947 jQuery("#simon1").one("click", function() {
948 ok( true, "Execute event only once" );
949 jQuery(this).toggle(function() {
950 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
952 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
955 }).click().click().click();
970 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
972 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
974 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
976 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
978 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
980 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
982 $div.unbind('click',fns[0]);
983 var data = jQuery._data( $div[0], 'events' );
984 ok( !data, "Unbinding one function from toggle unbinds them all");
986 // manually clean up detached elements
989 // Test Multi-Toggles
991 $div = jQuery("<div/>");
992 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
994 same( a, [1], "Check that a click worked." );
996 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
998 same( a, [1,2], "Check that a click worked with a second toggle." );
999 same( b, [1], "Check that a click worked with a second toggle." );
1002 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
1003 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
1005 // manually clean up detached elements
1009 test(".live()/.die()", function() {
1012 var submit = 0, div = 0, livea = 0, liveb = 0;
1014 jQuery("div").live("submit", function(){ submit++; return false; });
1015 jQuery("div").live("click", function(){ div++; });
1016 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
1017 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
1019 // Nothing should trigger on the body
1020 jQuery("body").trigger("click");
1021 equals( submit, 0, "Click on body" );
1022 equals( div, 0, "Click on body" );
1023 equals( livea, 0, "Click on body" );
1024 equals( liveb, 0, "Click on body" );
1026 // This should trigger two events
1027 submit = 0, div = 0, livea = 0, liveb = 0;
1028 jQuery("div#nothiddendiv").trigger("click");
1029 equals( submit, 0, "Click on div" );
1030 equals( div, 1, "Click on div" );
1031 equals( livea, 1, "Click on div" );
1032 equals( liveb, 0, "Click on div" );
1034 // This should trigger three events (w/ bubbling)
1035 submit = 0, div = 0, livea = 0, liveb = 0;
1036 jQuery("div#nothiddendivchild").trigger("click");
1037 equals( submit, 0, "Click on inner div" );
1038 equals( div, 2, "Click on inner div" );
1039 equals( livea, 1, "Click on inner div" );
1040 equals( liveb, 1, "Click on inner div" );
1042 // This should trigger one submit
1043 submit = 0, div = 0, livea = 0, liveb = 0;
1044 jQuery("div#nothiddendivchild").trigger("submit");
1045 equals( submit, 1, "Submit on div" );
1046 equals( div, 0, "Submit on div" );
1047 equals( livea, 0, "Submit on div" );
1048 equals( liveb, 0, "Submit on div" );
1050 // Make sure no other events were removed in the process
1051 submit = 0, div = 0, livea = 0, liveb = 0;
1052 jQuery("div#nothiddendivchild").trigger("click");
1053 equals( submit, 0, "die Click on inner div" );
1054 equals( div, 2, "die Click on inner div" );
1055 equals( livea, 1, "die Click on inner div" );
1056 equals( liveb, 1, "die Click on inner div" );
1058 // Now make sure that the removal works
1059 submit = 0, div = 0, livea = 0, liveb = 0;
1060 jQuery("div#nothiddendivchild").die("click");
1061 jQuery("div#nothiddendivchild").trigger("click");
1062 equals( submit, 0, "die Click on inner div" );
1063 equals( div, 2, "die Click on inner div" );
1064 equals( livea, 1, "die Click on inner div" );
1065 equals( liveb, 0, "die Click on inner div" );
1067 // Make sure that the click wasn't removed too early
1068 submit = 0, div = 0, livea = 0, liveb = 0;
1069 jQuery("div#nothiddendiv").trigger("click");
1070 equals( submit, 0, "die Click on inner div" );
1071 equals( div, 1, "die Click on inner div" );
1072 equals( livea, 1, "die Click on inner div" );
1073 equals( liveb, 0, "die Click on inner div" );
1075 // Make sure that stopPropgation doesn't stop live events
1076 submit = 0, div = 0, livea = 0, liveb = 0;
1077 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1078 jQuery("div#nothiddendivchild").trigger("click");
1079 equals( submit, 0, "stopPropagation Click on inner div" );
1080 equals( div, 1, "stopPropagation Click on inner div" );
1081 equals( livea, 0, "stopPropagation Click on inner div" );
1082 equals( liveb, 1, "stopPropagation Click on inner div" );
1084 // Make sure click events only fire with primary click
1085 submit = 0, div = 0, livea = 0, liveb = 0;
1086 var event = jQuery.Event("click");
1088 jQuery("div#nothiddendiv").trigger(event);
1090 equals( livea, 0, "live secondary click" );
1092 jQuery("div#nothiddendivchild").die("click");
1093 jQuery("div#nothiddendiv").die("click");
1094 jQuery("div").die("click");
1095 jQuery("div").die("submit");
1097 // Test binding with a different context
1098 var clicked = 0, container = jQuery('#main')[0];
1099 jQuery("#foo", container).live("click", function(e){ clicked++; });
1100 jQuery("div").trigger('click');
1101 jQuery("#foo").trigger('click');
1102 jQuery("#main").trigger('click');
1103 jQuery("body").trigger('click');
1104 equals( clicked, 2, "live with a context" );
1106 // Make sure the event is actually stored on the context
1107 ok( jQuery._data(container, "events").live, "live with a context" );
1109 // Test unbinding with a different context
1110 jQuery("#foo", container).die("click");
1111 jQuery("#foo").trigger('click');
1112 equals( clicked, 2, "die with a context");
1114 // Test binding with event data
1115 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1116 jQuery("#foo").trigger("click").die("click");
1118 // Test binding with trigger data
1119 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1120 jQuery("#foo").trigger("click", true).die("click");
1122 // Test binding with different this object
1123 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1124 jQuery("#foo").trigger("click").die("click");
1126 // Test binding with different this object, event data, and trigger data
1127 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1128 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1129 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1130 equals( data, true, "live with with different this object, event data, and trigger data")
1131 }, { foo: "bar" }));
1132 jQuery("#foo").trigger("click", true).die("click");
1134 // Verify that return false prevents default action
1135 jQuery("#anchor2").live("click", function(){ return false; });
1136 var hash = window.location.hash;
1137 jQuery("#anchor2").trigger("click");
1138 equals( window.location.hash, hash, "return false worked" );
1139 jQuery("#anchor2").die("click");
1141 // Verify that .preventDefault() prevents default action
1142 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1143 var hash = window.location.hash;
1144 jQuery("#anchor2").trigger("click");
1145 equals( window.location.hash, hash, "e.preventDefault() worked" );
1146 jQuery("#anchor2").die("click");
1148 // Test binding the same handler to multiple points
1150 function callback(){ called++; return false; }
1152 jQuery("#nothiddendiv").live("click", callback);
1153 jQuery("#anchor2").live("click", callback);
1155 jQuery("#nothiddendiv").trigger("click");
1156 equals( called, 1, "Verify that only one click occurred." );
1159 jQuery("#anchor2").trigger("click");
1160 equals( called, 1, "Verify that only one click occurred." );
1162 // Make sure that only one callback is removed
1163 jQuery("#anchor2").die("click", callback);
1166 jQuery("#nothiddendiv").trigger("click");
1167 equals( called, 1, "Verify that only one click occurred." );
1170 jQuery("#anchor2").trigger("click");
1171 equals( called, 0, "Verify that no click occurred." );
1173 // Make sure that it still works if the selector is the same,
1174 // but the event type is different
1175 jQuery("#nothiddendiv").live("foo", callback);
1178 jQuery("#nothiddendiv").die("click", callback);
1181 jQuery("#nothiddendiv").trigger("click");
1182 equals( called, 0, "Verify that no click occurred." );
1185 jQuery("#nothiddendiv").trigger("foo");
1186 equals( called, 1, "Verify that one foo occurred." );
1189 jQuery("#nothiddendiv").die("foo", callback);
1191 // Make sure we don't loose the target by DOM modifications
1192 // after the bubble already reached the liveHandler
1193 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1195 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1196 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1198 jQuery("#nothiddendiv span").click();
1199 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1200 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1203 jQuery("#nothiddendivchild").die("click");
1205 // Verify that .live() ocurs and cancel buble in the same order as
1206 // we would expect .bind() and .click() without delegation
1207 var lived = 0, livee = 0;
1209 // bind one pair in one order
1210 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1211 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1213 jQuery('span#liveSpan1 a').click();
1214 equals( lived, 1, "Verify that only one first handler occurred." );
1215 equals( livee, 0, "Verify that second handler doesn't." );
1217 // and one pair in inverse
1218 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1219 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1223 jQuery('span#liveSpan2 a').click();
1224 equals( lived, 1, "Verify that only one first handler occurred." );
1225 equals( livee, 0, "Verify that second handler doesn't." );
1228 jQuery("span#liveSpan1 a").die("click")
1229 jQuery("span#liveSpan1").die("click");
1230 jQuery("span#liveSpan2 a").die("click");
1231 jQuery("span#liveSpan2").die("click");
1233 // Test this, target and currentTarget are correct
1234 jQuery('span#liveSpan1').live('click', function(e){
1235 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1236 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1237 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1240 jQuery('span#liveSpan1 a').click();
1242 jQuery('span#liveSpan1').die('click');
1244 // Work with deep selectors
1247 function clickB(){ livee++; }
1249 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1250 jQuery("#nothiddendiv div").live("click", clickB);
1251 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1253 equals( livee, 0, "No clicks, deep selector." );
1256 jQuery("#nothiddendivchild").trigger("click");
1257 equals( livee, 2, "Click, deep selector." );
1260 jQuery("#nothiddendivchild").trigger("mouseover");
1261 equals( livee, 1, "Mouseover, deep selector." );
1263 jQuery("#nothiddendiv div").die("mouseover");
1266 jQuery("#nothiddendivchild").trigger("click");
1267 equals( livee, 2, "Click, deep selector." );
1270 jQuery("#nothiddendivchild").trigger("mouseover");
1271 equals( livee, 0, "Mouseover, deep selector." );
1273 jQuery("#nothiddendiv div").die("click", clickB);
1276 jQuery("#nothiddendivchild").trigger("click");
1277 equals( livee, 1, "Click, deep selector." );
1279 jQuery("#nothiddendiv div").die("click");
1281 jQuery("#nothiddendiv div").live("blur", function(){
1282 ok( true, "Live div trigger blur." );
1285 jQuery("#nothiddendiv div").trigger("blur");
1287 jQuery("#nothiddendiv div").die("blur");
1290 test("die all bound events", function(){
1294 var div = jQuery("div#nothiddendivchild");
1296 div.live("click submit", function(){ count++; });
1299 div.trigger("click");
1300 div.trigger("submit");
1302 equals( count, 0, "Make sure no events were triggered." );
1305 test("live with multiple events", function(){
1309 var div = jQuery("div#nothiddendivchild");
1311 div.live("click submit", function(){ count++; });
1313 div.trigger("click");
1314 div.trigger("submit");
1316 equals( count, 2, "Make sure both the click and submit were triggered." );
1318 // manually clean up events from elements outside the fixture
1322 test("live with namespaces", function(){
1325 var count1 = 0, count2 = 0;
1327 jQuery("#liveSpan1").live("foo.bar", function(e){
1331 jQuery("#liveSpan1").live("foo.zed", function(e){
1335 jQuery("#liveSpan1").trigger("foo.bar");
1336 equals( count1, 1, "Got live foo.bar" );
1337 equals( count2, 0, "Got live foo.bar" );
1339 count1 = 0, count2 = 0;
1341 jQuery("#liveSpan1").trigger("foo.zed");
1342 equals( count1, 0, "Got live foo.zed" );
1343 equals( count2, 1, "Got live foo.zed" );
1346 count1 = 0, count2 = 0;
1348 jQuery("#liveSpan1").die("foo.zed");
1349 jQuery("#liveSpan1").trigger("foo.bar");
1351 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1352 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1354 count1 = 0, count2 = 0;
1356 jQuery("#liveSpan1").trigger("foo.zed");
1357 equals( count1, 0, "Got live foo.zed" );
1358 equals( count2, 0, "Got live foo.zed" );
1361 jQuery("#liveSpan1").die("foo.bar");
1363 count1 = 0, count2 = 0;
1365 jQuery("#liveSpan1").trigger("foo.bar");
1366 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1367 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1369 jQuery("#liveSpan1").trigger("foo.zed");
1370 equals( count1, 0, "Did not trigger foo.zed again" );
1371 equals( count2, 0, "Did not trigger foo.zed again" );
1374 test("live with change", function(){
1377 var selectChange = 0, checkboxChange = 0;
1379 var select = jQuery("select[name='S1']")
1380 select.live("change", function() {
1384 var checkbox = jQuery("#check2"),
1385 checkboxFunction = function(){
1388 checkbox.live("change", checkboxFunction);
1390 // test click on select
1392 // second click that changed it
1394 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1395 select.trigger("change");
1396 equals( selectChange, 1, "Change on click." );
1398 // test keys on select
1400 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1401 select.trigger("change");
1402 equals( selectChange, 1, "Change on keyup." );
1404 // test click on checkbox
1405 checkbox.trigger("change");
1406 equals( checkboxChange, 1, "Change on checkbox." );
1408 // test blur/focus on text
1409 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1410 text.live("change", function() {
1414 text.val(oldTextVal+"foo");
1415 text.trigger("change");
1416 equals( textChange, 1, "Change on text input." );
1418 text.val(oldTextVal);
1421 // test blur/focus on password
1422 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1423 password.live("change", function() {
1427 password.val(oldPasswordVal + "foo");
1428 password.trigger("change");
1429 equals( passwordChange, 1, "Change on password input." );
1431 password.val(oldPasswordVal);
1432 password.die("change");
1434 // make sure die works
1438 select.die("change");
1439 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1440 select.trigger("change");
1441 equals( selectChange, 0, "Die on click works." );
1444 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1445 select.trigger("change");
1446 equals( selectChange, 0, "Die on keyup works." );
1448 // die specific checkbox
1449 checkbox.die("change", checkboxFunction);
1450 checkbox.trigger("change");
1451 equals( checkboxChange, 1, "Die on checkbox." );
1454 test("live with submit", function() {
1455 var count1 = 0, count2 = 0;
1457 jQuery("#testForm").live("submit", function(ev) {
1459 ev.preventDefault();
1462 jQuery("body").live("submit", function(ev) {
1464 ev.preventDefault();
1467 jQuery("#testForm input[name=sub1]").submit();
1468 equals( count1, 1, "Verify form submit." );
1469 equals( count2, 1, "Verify body submit." );
1471 jQuery("#testForm").die("submit");
1472 jQuery("body").die("submit");
1475 test("live with special events", function() {
1478 jQuery.event.special.foo = {
1479 setup: function( data, namespaces, handler ) {
1480 ok( true, "Setup run." );
1482 teardown: function( namespaces ) {
1483 ok( true, "Teardown run." );
1485 add: function( handleObj ) {
1486 ok( true, "Add run." );
1488 remove: function( handleObj ) {
1489 ok( true, "Remove run." );
1491 _default: function( event ) {
1492 ok( true, "Default run." );
1497 jQuery("#liveSpan1").live("foo.a", function(e){
1498 ok( true, "Handler 1 run." );
1502 jQuery("#liveSpan1").live("foo.b", function(e){
1503 ok( true, "Handler 2 run." );
1506 // Run: Handler 1, Handler 2, Default
1507 jQuery("#liveSpan1").trigger("foo");
1509 // Run: Handler 1, Default
1510 // TODO: Namespace doesn't trigger default (?)
1511 jQuery("#liveSpan1").trigger("foo.a");
1514 jQuery("#liveSpan1").die("foo.a");
1516 // Run: Handler 2, Default
1517 jQuery("#liveSpan1").trigger("foo");
1519 // Run: remove, teardown
1520 jQuery("#liveSpan1").die("foo");
1522 delete jQuery.event.special.foo;
1525 test(".delegate()/.undelegate()", function() {
1528 var submit = 0, div = 0, livea = 0, liveb = 0;
1530 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1531 jQuery("#body").delegate("div", "click", function(){ div++; });
1532 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1533 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1535 // Nothing should trigger on the body
1536 jQuery("body").trigger("click");
1537 equals( submit, 0, "Click on body" );
1538 equals( div, 0, "Click on body" );
1539 equals( livea, 0, "Click on body" );
1540 equals( liveb, 0, "Click on body" );
1542 // This should trigger two events
1543 submit = 0, div = 0, livea = 0, liveb = 0;
1544 jQuery("div#nothiddendiv").trigger("click");
1545 equals( submit, 0, "Click on div" );
1546 equals( div, 1, "Click on div" );
1547 equals( livea, 1, "Click on div" );
1548 equals( liveb, 0, "Click on div" );
1550 // This should trigger three events (w/ bubbling)
1551 submit = 0, div = 0, livea = 0, liveb = 0;
1552 jQuery("div#nothiddendivchild").trigger("click");
1553 equals( submit, 0, "Click on inner div" );
1554 equals( div, 2, "Click on inner div" );
1555 equals( livea, 1, "Click on inner div" );
1556 equals( liveb, 1, "Click on inner div" );
1558 // This should trigger one submit
1559 submit = 0, div = 0, livea = 0, liveb = 0;
1560 jQuery("div#nothiddendivchild").trigger("submit");
1561 equals( submit, 1, "Submit on div" );
1562 equals( div, 0, "Submit on div" );
1563 equals( livea, 0, "Submit on div" );
1564 equals( liveb, 0, "Submit on div" );
1566 // Make sure no other events were removed in the process
1567 submit = 0, div = 0, livea = 0, liveb = 0;
1568 jQuery("div#nothiddendivchild").trigger("click");
1569 equals( submit, 0, "undelegate Click on inner div" );
1570 equals( div, 2, "undelegate Click on inner div" );
1571 equals( livea, 1, "undelegate Click on inner div" );
1572 equals( liveb, 1, "undelegate Click on inner div" );
1574 // Now make sure that the removal works
1575 submit = 0, div = 0, livea = 0, liveb = 0;
1576 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1577 jQuery("div#nothiddendivchild").trigger("click");
1578 equals( submit, 0, "undelegate Click on inner div" );
1579 equals( div, 2, "undelegate Click on inner div" );
1580 equals( livea, 1, "undelegate Click on inner div" );
1581 equals( liveb, 0, "undelegate Click on inner div" );
1583 // Make sure that the click wasn't removed too early
1584 submit = 0, div = 0, livea = 0, liveb = 0;
1585 jQuery("div#nothiddendiv").trigger("click");
1586 equals( submit, 0, "undelegate Click on inner div" );
1587 equals( div, 1, "undelegate Click on inner div" );
1588 equals( livea, 1, "undelegate Click on inner div" );
1589 equals( liveb, 0, "undelegate Click on inner div" );
1591 // Make sure that stopPropgation doesn't stop live events
1592 submit = 0, div = 0, livea = 0, liveb = 0;
1593 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1594 jQuery("div#nothiddendivchild").trigger("click");
1595 equals( submit, 0, "stopPropagation Click on inner div" );
1596 equals( div, 1, "stopPropagation Click on inner div" );
1597 equals( livea, 0, "stopPropagation Click on inner div" );
1598 equals( liveb, 1, "stopPropagation Click on inner div" );
1600 // Make sure click events only fire with primary click
1601 submit = 0, div = 0, livea = 0, liveb = 0;
1602 var event = jQuery.Event("click");
1604 jQuery("div#nothiddendiv").trigger(event);
1606 equals( livea, 0, "delegate secondary click" );
1608 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1609 jQuery("#body").undelegate("div#nothiddendiv", "click");
1610 jQuery("#body").undelegate("div", "click");
1611 jQuery("#body").undelegate("div", "submit");
1613 // Test binding with a different context
1614 var clicked = 0, container = jQuery('#main')[0];
1615 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1616 jQuery("div").trigger('click');
1617 jQuery("#foo").trigger('click');
1618 jQuery("#main").trigger('click');
1619 jQuery("body").trigger('click');
1620 equals( clicked, 2, "delegate with a context" );
1622 // Make sure the event is actually stored on the context
1623 ok( jQuery._data(container, "events").live, "delegate with a context" );
1625 // Test unbinding with a different context
1626 jQuery("#main").undelegate("#foo", "click");
1627 jQuery("#foo").trigger('click');
1628 equals( clicked, 2, "undelegate with a context");
1630 // Test binding with event data
1631 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1632 jQuery("#foo").trigger("click");
1633 jQuery("#body").undelegate("#foo", "click");
1635 // Test binding with trigger data
1636 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1637 jQuery("#foo").trigger("click", true);
1638 jQuery("#body").undelegate("#foo", "click");
1640 // Test binding with different this object
1641 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1642 jQuery("#foo").trigger("click");
1643 jQuery("#body").undelegate("#foo", "click");
1645 // Test binding with different this object, event data, and trigger data
1646 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1647 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1648 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1649 equals( data, true, "delegate with with different this object, event data, and trigger data")
1650 }, { foo: "bar" }));
1651 jQuery("#foo").trigger("click", true);
1652 jQuery("#body").undelegate("#foo", "click");
1654 // Verify that return false prevents default action
1655 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1656 var hash = window.location.hash;
1657 jQuery("#anchor2").trigger("click");
1658 equals( window.location.hash, hash, "return false worked" );
1659 jQuery("#body").undelegate("#anchor2", "click");
1661 // Verify that .preventDefault() prevents default action
1662 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1663 var hash = window.location.hash;
1664 jQuery("#anchor2").trigger("click");
1665 equals( window.location.hash, hash, "e.preventDefault() worked" );
1666 jQuery("#body").undelegate("#anchor2", "click");
1668 // Test binding the same handler to multiple points
1670 function callback(){ called++; return false; }
1672 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1673 jQuery("#body").delegate("#anchor2", "click", callback);
1675 jQuery("#nothiddendiv").trigger("click");
1676 equals( called, 1, "Verify that only one click occurred." );
1679 jQuery("#anchor2").trigger("click");
1680 equals( called, 1, "Verify that only one click occurred." );
1682 // Make sure that only one callback is removed
1683 jQuery("#body").undelegate("#anchor2", "click", callback);
1686 jQuery("#nothiddendiv").trigger("click");
1687 equals( called, 1, "Verify that only one click occurred." );
1690 jQuery("#anchor2").trigger("click");
1691 equals( called, 0, "Verify that no click occurred." );
1693 // Make sure that it still works if the selector is the same,
1694 // but the event type is different
1695 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1698 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1701 jQuery("#nothiddendiv").trigger("click");
1702 equals( called, 0, "Verify that no click occurred." );
1705 jQuery("#nothiddendiv").trigger("foo");
1706 equals( called, 1, "Verify that one foo occurred." );
1709 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1711 // Make sure we don't loose the target by DOM modifications
1712 // after the bubble already reached the liveHandler
1713 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1715 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1716 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1718 jQuery("#nothiddendiv span").click();
1719 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1720 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1723 jQuery("#body").undelegate("#nothiddendivchild", "click");
1725 // Verify that .live() ocurs and cancel buble in the same order as
1726 // we would expect .bind() and .click() without delegation
1727 var lived = 0, livee = 0;
1729 // bind one pair in one order
1730 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1731 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1733 jQuery('span#liveSpan1 a').click();
1734 equals( lived, 1, "Verify that only one first handler occurred." );
1735 equals( livee, 0, "Verify that second handler doesn't." );
1737 // and one pair in inverse
1738 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1739 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1743 jQuery('span#liveSpan2 a').click();
1744 equals( lived, 1, "Verify that only one first handler occurred." );
1745 equals( livee, 0, "Verify that second handler doesn't." );
1748 jQuery("#body").undelegate("click");
1750 // Test this, target and currentTarget are correct
1751 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1752 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1753 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1754 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1757 jQuery('span#liveSpan1 a').click();
1759 jQuery("#body").undelegate('span#liveSpan1', 'click');
1761 // Work with deep selectors
1764 function clickB(){ livee++; }
1766 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1767 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1768 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1770 equals( livee, 0, "No clicks, deep selector." );
1773 jQuery("#nothiddendivchild").trigger("click");
1774 equals( livee, 2, "Click, deep selector." );
1777 jQuery("#nothiddendivchild").trigger("mouseover");
1778 equals( livee, 1, "Mouseover, deep selector." );
1780 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1783 jQuery("#nothiddendivchild").trigger("click");
1784 equals( livee, 2, "Click, deep selector." );
1787 jQuery("#nothiddendivchild").trigger("mouseover");
1788 equals( livee, 0, "Mouseover, deep selector." );
1790 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1793 jQuery("#nothiddendivchild").trigger("click");
1794 equals( livee, 1, "Click, deep selector." );
1796 jQuery("#body").undelegate("#nothiddendiv div", "click");
1799 test("undelegate all bound events", function(){
1803 var div = jQuery("#body");
1805 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1808 jQuery("div#nothiddendivchild").trigger("click");
1809 jQuery("div#nothiddendivchild").trigger("submit");
1811 equals( count, 0, "Make sure no events were triggered." );
1814 test("delegate with multiple events", function(){
1818 var div = jQuery("#body");
1820 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1822 jQuery("div#nothiddendivchild").trigger("click");
1823 jQuery("div#nothiddendivchild").trigger("submit");
1825 equals( count, 2, "Make sure both the click and submit were triggered." );
1827 jQuery("#body").undelegate();
1830 test("delegate with change", function(){
1833 var selectChange = 0, checkboxChange = 0;
1835 var select = jQuery("select[name='S1']");
1836 jQuery("#body").delegate("select[name='S1']", "change", function() {
1840 var checkbox = jQuery("#check2"),
1841 checkboxFunction = function(){
1844 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1846 // test click on select
1848 // second click that changed it
1850 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1851 select.trigger("change");
1852 equals( selectChange, 1, "Change on click." );
1854 // test keys on select
1856 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1857 select.trigger("change");
1858 equals( selectChange, 1, "Change on keyup." );
1860 // test click on checkbox
1861 checkbox.trigger("change");
1862 equals( checkboxChange, 1, "Change on checkbox." );
1864 // test blur/focus on text
1865 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1866 jQuery("#body").delegate("#name", "change", function() {
1870 text.val(oldTextVal+"foo");
1871 text.trigger("change");
1872 equals( textChange, 1, "Change on text input." );
1874 text.val(oldTextVal);
1875 jQuery("#body").die("change");
1877 // test blur/focus on password
1878 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1879 jQuery("#body").delegate("#name", "change", function() {
1883 password.val(oldPasswordVal + "foo");
1884 password.trigger("change");
1885 equals( passwordChange, 1, "Change on password input." );
1887 password.val(oldPasswordVal);
1888 jQuery("#body").undelegate("#name", "change");
1890 // make sure die works
1894 jQuery("#body").undelegate("select[name='S1']", "change");
1895 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1896 select.trigger("change");
1897 equals( selectChange, 0, "Die on click works." );
1900 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1901 select.trigger("change");
1902 equals( selectChange, 0, "Die on keyup works." );
1904 // die specific checkbox
1905 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1906 checkbox.trigger("change");
1907 equals( checkboxChange, 1, "Die on checkbox." );
1910 test("delegate with submit", function() {
1911 var count1 = 0, count2 = 0;
1913 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1915 ev.preventDefault();
1918 jQuery(document).delegate("body", "submit", function(ev) {
1920 ev.preventDefault();
1923 jQuery("#testForm input[name=sub1]").submit();
1924 equals( count1, 1, "Verify form submit." );
1925 equals( count2, 1, "Verify body submit." );
1927 jQuery("#body").undelegate();
1928 jQuery(document).undelegate();
1931 test("Non DOM element events", function() {
1936 jQuery(o).bind('nonelementobj', function(e) {
1937 ok( true, "Event on non-DOM object triggered" );
1940 jQuery(o).trigger('nonelementobj');
1943 test("window resize", function() {
1946 jQuery(window).unbind();
1948 jQuery(window).bind("resize", function(){
1949 ok( true, "Resize event fired." );
1950 }).resize().unbind("resize");
1952 ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
1956 test("jQuery(function($) {})", function() {
1958 jQuery(function($) {
1959 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");
1964 test("event properties", function() {
1966 jQuery("#simon1").click(function(event) {
1967 ok( event.timeStamp, "assert event.timeStamp is present" );