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( typeof events, "function", "'events' expando is a function on plain objects." );
574 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
575 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
578 jQuery(obj).trigger("test");
579 jQuery(obj).trigger("submit");
581 jQuery(obj).unbind("test");
582 jQuery(obj).unbind("submit");
585 jQuery(obj).trigger("test");
587 // Make sure it doesn't complain when no events are found
588 jQuery(obj).unbind("test");
590 equals( obj && obj[ jQuery.expando ] &&
591 obj[ jQuery.expando ][ jQuery.expando ] &&
592 obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
595 test("unbind(type)", function() {
598 var $elem = jQuery("#firstp"),
602 ok( false, message );
605 message = "unbind passing function";
606 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
608 message = "unbind all from event";
609 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
611 message = "unbind all";
612 $elem.bind('error1', error).unbind().triggerHandler('error1');
614 message = "unbind many with function";
615 $elem.bind('error1 error2',error)
616 .unbind('error1 error2', error )
617 .trigger('error1').triggerHandler('error2');
619 message = "unbind many"; // #3538
620 $elem.bind('error1 error2',error)
621 .unbind('error1 error2')
622 .trigger('error1').triggerHandler('error2');
624 message = "unbind without a type or handler";
625 $elem.bind("error1 error2.test",error)
627 .trigger("error1").triggerHandler("error2");
630 test("unbind(eventObject)", function() {
633 var $elem = jQuery("#firstp"),
636 function assert( expected ){
638 $elem.trigger('foo').triggerHandler('bar');
639 equals( num, expected, "Check the right handlers are triggered" );
643 // This handler shouldn't be unbound
644 .bind('foo', function(){
647 .bind('foo', function(e){
652 .bind('bar', function(){
666 test("hover()", function() {
668 handler1 = function( event ) { ++times; },
669 handler2 = function( event ) { ++times; };
672 .hover(handler1, handler2)
673 .mouseenter().mouseleave()
674 .unbind("mouseenter", handler1)
675 .unbind("mouseleave", handler2)
677 .mouseenter().mouseleave()
678 .unbind("mouseenter mouseleave", handler1)
679 .mouseenter().mouseleave();
681 equals( times, 4, "hover handlers fired" );
684 test("trigger() shortcuts", function() {
687 var elem = jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL');
688 elem.find('a').bind('click', function() {
689 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
690 equals( close.length, 0, "Context element does not exist, length must be zero" );
691 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
695 // manually clean up detached elements
698 jQuery("#check1").click(function() {
699 ok( true, "click event handler for checkbox gets fired twice, see #815" );
703 jQuery('#firstp')[0].onclick = function(event) {
706 jQuery('#firstp').click();
707 equals( counter, 1, "Check that click, triggers onclick event handler also" );
709 var clickCounter = 0;
710 jQuery('#simon1')[0].onclick = function(event) {
713 jQuery('#simon1').click();
714 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
716 elem = jQuery('<img />').load(function(){
717 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
720 // manually clean up detached elements
724 test("trigger() bubbling", function() {
727 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
729 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
730 jQuery("html").bind("click", function(e){ html++; });
731 jQuery("body").bind("click", function(e){ body++; });
732 jQuery("#main").bind("click", function(e){ main++; });
733 jQuery("#ap").bind("click", function(){ ap++; return false; });
735 jQuery("html").trigger("click");
736 equals( doc, 1, "HTML bubble" );
737 equals( html, 1, "HTML bubble" );
739 jQuery("body").trigger("click");
740 equals( doc, 2, "Body bubble" );
741 equals( html, 2, "Body bubble" );
742 equals( body, 1, "Body bubble" );
744 jQuery("#main").trigger("click");
745 equals( doc, 3, "Main bubble" );
746 equals( html, 3, "Main bubble" );
747 equals( body, 2, "Main bubble" );
748 equals( main, 1, "Main bubble" );
750 jQuery("#ap").trigger("click");
751 equals( doc, 3, "ap bubble" );
752 equals( html, 3, "ap bubble" );
753 equals( body, 2, "ap bubble" );
754 equals( main, 1, "ap bubble" );
755 equals( ap, 1, "ap bubble" );
757 // manually clean up events from elements outside the fixture
758 jQuery(document).unbind("click");
759 jQuery("html, body, #main").unbind("click");
762 test("trigger(type, [data], [fn])", function() {
765 var handler = function(event, a, b, c) {
766 equals( event.type, "click", "check passed data" );
767 equals( a, 1, "check passed data" );
768 equals( b, "2", "check passed data" );
769 equals( c, "abc", "check passed data" );
773 var $elem = jQuery("#firstp");
775 // Simulate a "native" click
776 $elem[0].click = function(){
777 ok( true, "Native call was triggered" );
780 // Triggers handlrs and native
782 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
784 // Simulate a "native" click
785 $elem[0].click = function(){
786 ok( false, "Native call was triggered" );
789 // Trigger only the handlers (no native)
791 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
795 jQuery('#form input:first').hide().trigger('focus');
799 ok( pass, "Trigger focus on hidden element" );
803 jQuery('#main table:first').bind('test:test', function(){}).trigger('test:test');
807 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
809 var form = jQuery("<form action=''></form>").appendTo("body");
811 // Make sure it can be prevented locally
812 form.submit(function(){
813 ok( true, "Local bind still works." );
818 form.trigger("submit");
820 form.unbind("submit");
822 jQuery(document).submit(function(){
823 ok( true, "Make sure bubble works up to document." );
828 form.trigger("submit");
830 jQuery(document).unbind("submit");
835 test("jQuery.Event.currentTarget", function(){
838 test("trigger(eventObject, [data], [fn])", function() {
841 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
842 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
844 var event = jQuery.Event("noNew");
845 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
846 equals( event.type, "noNew", "Verify its type" );
848 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
849 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
850 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
852 event.preventDefault();
853 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
854 event.stopPropagation();
855 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
857 event.isPropagationStopped = function(){ return false };
858 event.stopImmediatePropagation();
859 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
860 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
862 $parent.bind('foo',function(e){
864 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
865 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
866 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
867 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
870 // test with an event object
871 event = new jQuery.Event("foo");
872 event.secret = 'boo!';
873 $child.trigger(event);
875 // test with a literal object
876 $child.trigger({type:'foo', secret:'boo!'});
881 ok( false, "This assertion shouldn't be reached");
884 $parent.bind('foo', error );
886 $child.bind('foo',function(e, a, b, c ){
887 equals( arguments.length, 4, "Check arguments length");
888 equals( a, 1, "Check first custom argument");
889 equals( b, 2, "Check second custom argument");
890 equals( c, 3, "Check third custom argument");
892 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
893 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
894 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
897 e.stopImmediatePropagation();
902 // We should add this back in when we want to test the order
903 // in which event handlers are iterated.
904 //$child.bind('foo', error );
906 event = new jQuery.Event("foo");
907 $child.trigger( event, [1,2,3] ).unbind();
908 equals( event.result, "result", "Check event.result attribute");
910 // Will error if it bubbles
911 $child.triggerHandler('foo');
914 $parent.unbind().remove();
917 test("jQuery.Event.currentTarget", function(){
921 $elem = jQuery('<button>a</button>').click(function(e){
922 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
926 $elem.trigger('click');
932 test("toggle(Function, Function, ...)", function() {
936 fn1 = function(e) { count++; },
937 fn2 = function(e) { count--; },
938 preventDefault = function(e) { e.preventDefault() },
939 link = jQuery('#mark');
940 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
941 equals( count, 1, "Check for toggle(fn, fn)" );
943 jQuery("#firstp").toggle(function () {
944 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
945 }, function() {}).trigger("click", [ 1, 2, 3 ]);
948 jQuery("#simon1").one("click", function() {
949 ok( true, "Execute event only once" );
950 jQuery(this).toggle(function() {
951 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
953 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
956 }).click().click().click();
971 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
973 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
975 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
977 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
979 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
981 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
983 $div.unbind('click',fns[0]);
984 var data = jQuery._data( $div[0], 'events' );
985 ok( !data, "Unbinding one function from toggle unbinds them all");
987 // manually clean up detached elements
990 // Test Multi-Toggles
992 $div = jQuery("<div/>");
993 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
995 same( a, [1], "Check that a click worked." );
997 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
999 same( a, [1,2], "Check that a click worked with a second toggle." );
1000 same( b, [1], "Check that a click worked with a second toggle." );
1003 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
1004 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
1006 // manually clean up detached elements
1010 test(".live()/.die()", function() {
1013 var submit = 0, div = 0, livea = 0, liveb = 0;
1015 jQuery("div").live("submit", function(){ submit++; return false; });
1016 jQuery("div").live("click", function(){ div++; });
1017 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
1018 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
1020 // Nothing should trigger on the body
1021 jQuery("body").trigger("click");
1022 equals( submit, 0, "Click on body" );
1023 equals( div, 0, "Click on body" );
1024 equals( livea, 0, "Click on body" );
1025 equals( liveb, 0, "Click on body" );
1027 // This should trigger two events
1028 submit = 0, div = 0, livea = 0, liveb = 0;
1029 jQuery("div#nothiddendiv").trigger("click");
1030 equals( submit, 0, "Click on div" );
1031 equals( div, 1, "Click on div" );
1032 equals( livea, 1, "Click on div" );
1033 equals( liveb, 0, "Click on div" );
1035 // This should trigger three events (w/ bubbling)
1036 submit = 0, div = 0, livea = 0, liveb = 0;
1037 jQuery("div#nothiddendivchild").trigger("click");
1038 equals( submit, 0, "Click on inner div" );
1039 equals( div, 2, "Click on inner div" );
1040 equals( livea, 1, "Click on inner div" );
1041 equals( liveb, 1, "Click on inner div" );
1043 // This should trigger one submit
1044 submit = 0, div = 0, livea = 0, liveb = 0;
1045 jQuery("div#nothiddendivchild").trigger("submit");
1046 equals( submit, 1, "Submit on div" );
1047 equals( div, 0, "Submit on div" );
1048 equals( livea, 0, "Submit on div" );
1049 equals( liveb, 0, "Submit on div" );
1051 // Make sure no other events were removed in the process
1052 submit = 0, div = 0, livea = 0, liveb = 0;
1053 jQuery("div#nothiddendivchild").trigger("click");
1054 equals( submit, 0, "die Click on inner div" );
1055 equals( div, 2, "die Click on inner div" );
1056 equals( livea, 1, "die Click on inner div" );
1057 equals( liveb, 1, "die Click on inner div" );
1059 // Now make sure that the removal works
1060 submit = 0, div = 0, livea = 0, liveb = 0;
1061 jQuery("div#nothiddendivchild").die("click");
1062 jQuery("div#nothiddendivchild").trigger("click");
1063 equals( submit, 0, "die Click on inner div" );
1064 equals( div, 2, "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 the click wasn't removed too early
1069 submit = 0, div = 0, livea = 0, liveb = 0;
1070 jQuery("div#nothiddendiv").trigger("click");
1071 equals( submit, 0, "die Click on inner div" );
1072 equals( div, 1, "die Click on inner div" );
1073 equals( livea, 1, "die Click on inner div" );
1074 equals( liveb, 0, "die Click on inner div" );
1076 // Make sure that stopPropgation doesn't stop live events
1077 submit = 0, div = 0, livea = 0, liveb = 0;
1078 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1079 jQuery("div#nothiddendivchild").trigger("click");
1080 equals( submit, 0, "stopPropagation Click on inner div" );
1081 equals( div, 1, "stopPropagation Click on inner div" );
1082 equals( livea, 0, "stopPropagation Click on inner div" );
1083 equals( liveb, 1, "stopPropagation Click on inner div" );
1085 // Make sure click events only fire with primary click
1086 submit = 0, div = 0, livea = 0, liveb = 0;
1087 var event = jQuery.Event("click");
1089 jQuery("div#nothiddendiv").trigger(event);
1091 equals( livea, 0, "live secondary click" );
1093 jQuery("div#nothiddendivchild").die("click");
1094 jQuery("div#nothiddendiv").die("click");
1095 jQuery("div").die("click");
1096 jQuery("div").die("submit");
1098 // Test binding with a different context
1099 var clicked = 0, container = jQuery('#main')[0];
1100 jQuery("#foo", container).live("click", function(e){ clicked++; });
1101 jQuery("div").trigger('click');
1102 jQuery("#foo").trigger('click');
1103 jQuery("#main").trigger('click');
1104 jQuery("body").trigger('click');
1105 equals( clicked, 2, "live with a context" );
1107 // Make sure the event is actually stored on the context
1108 ok( jQuery._data(container, "events").live, "live with a context" );
1110 // Test unbinding with a different context
1111 jQuery("#foo", container).die("click");
1112 jQuery("#foo").trigger('click');
1113 equals( clicked, 2, "die with a context");
1115 // Test binding with event data
1116 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1117 jQuery("#foo").trigger("click").die("click");
1119 // Test binding with trigger data
1120 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1121 jQuery("#foo").trigger("click", true).die("click");
1123 // Test binding with different this object
1124 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1125 jQuery("#foo").trigger("click").die("click");
1127 // Test binding with different this object, event data, and trigger data
1128 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1129 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1130 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1131 equals( data, true, "live with with different this object, event data, and trigger data")
1132 }, { foo: "bar" }));
1133 jQuery("#foo").trigger("click", true).die("click");
1135 // Verify that return false prevents default action
1136 jQuery("#anchor2").live("click", function(){ return false; });
1137 var hash = window.location.hash;
1138 jQuery("#anchor2").trigger("click");
1139 equals( window.location.hash, hash, "return false worked" );
1140 jQuery("#anchor2").die("click");
1142 // Verify that .preventDefault() prevents default action
1143 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1144 var hash = window.location.hash;
1145 jQuery("#anchor2").trigger("click");
1146 equals( window.location.hash, hash, "e.preventDefault() worked" );
1147 jQuery("#anchor2").die("click");
1149 // Test binding the same handler to multiple points
1151 function callback(){ called++; return false; }
1153 jQuery("#nothiddendiv").live("click", callback);
1154 jQuery("#anchor2").live("click", callback);
1156 jQuery("#nothiddendiv").trigger("click");
1157 equals( called, 1, "Verify that only one click occurred." );
1160 jQuery("#anchor2").trigger("click");
1161 equals( called, 1, "Verify that only one click occurred." );
1163 // Make sure that only one callback is removed
1164 jQuery("#anchor2").die("click", callback);
1167 jQuery("#nothiddendiv").trigger("click");
1168 equals( called, 1, "Verify that only one click occurred." );
1171 jQuery("#anchor2").trigger("click");
1172 equals( called, 0, "Verify that no click occurred." );
1174 // Make sure that it still works if the selector is the same,
1175 // but the event type is different
1176 jQuery("#nothiddendiv").live("foo", callback);
1179 jQuery("#nothiddendiv").die("click", callback);
1182 jQuery("#nothiddendiv").trigger("click");
1183 equals( called, 0, "Verify that no click occurred." );
1186 jQuery("#nothiddendiv").trigger("foo");
1187 equals( called, 1, "Verify that one foo occurred." );
1190 jQuery("#nothiddendiv").die("foo", callback);
1192 // Make sure we don't loose the target by DOM modifications
1193 // after the bubble already reached the liveHandler
1194 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1196 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1197 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1199 jQuery("#nothiddendiv span").click();
1200 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1201 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1204 jQuery("#nothiddendivchild").die("click");
1206 // Verify that .live() ocurs and cancel buble in the same order as
1207 // we would expect .bind() and .click() without delegation
1208 var lived = 0, livee = 0;
1210 // bind one pair in one order
1211 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1212 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1214 jQuery('span#liveSpan1 a').click();
1215 equals( lived, 1, "Verify that only one first handler occurred." );
1216 equals( livee, 0, "Verify that second handler doesn't." );
1218 // and one pair in inverse
1219 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1220 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1224 jQuery('span#liveSpan2 a').click();
1225 equals( lived, 1, "Verify that only one first handler occurred." );
1226 equals( livee, 0, "Verify that second handler doesn't." );
1229 jQuery("span#liveSpan1 a").die("click")
1230 jQuery("span#liveSpan1").die("click");
1231 jQuery("span#liveSpan2 a").die("click");
1232 jQuery("span#liveSpan2").die("click");
1234 // Test this, target and currentTarget are correct
1235 jQuery('span#liveSpan1').live('click', function(e){
1236 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1237 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1238 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1241 jQuery('span#liveSpan1 a').click();
1243 jQuery('span#liveSpan1').die('click');
1245 // Work with deep selectors
1248 function clickB(){ livee++; }
1250 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1251 jQuery("#nothiddendiv div").live("click", clickB);
1252 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1254 equals( livee, 0, "No clicks, deep selector." );
1257 jQuery("#nothiddendivchild").trigger("click");
1258 equals( livee, 2, "Click, deep selector." );
1261 jQuery("#nothiddendivchild").trigger("mouseover");
1262 equals( livee, 1, "Mouseover, deep selector." );
1264 jQuery("#nothiddendiv div").die("mouseover");
1267 jQuery("#nothiddendivchild").trigger("click");
1268 equals( livee, 2, "Click, deep selector." );
1271 jQuery("#nothiddendivchild").trigger("mouseover");
1272 equals( livee, 0, "Mouseover, deep selector." );
1274 jQuery("#nothiddendiv div").die("click", clickB);
1277 jQuery("#nothiddendivchild").trigger("click");
1278 equals( livee, 1, "Click, deep selector." );
1280 jQuery("#nothiddendiv div").die("click");
1282 jQuery("#nothiddendiv div").live("blur", function(){
1283 ok( true, "Live div trigger blur." );
1286 jQuery("#nothiddendiv div").trigger("blur");
1288 jQuery("#nothiddendiv div").die("blur");
1291 test("die all bound events", function(){
1295 var div = jQuery("div#nothiddendivchild");
1297 div.live("click submit", function(){ count++; });
1300 div.trigger("click");
1301 div.trigger("submit");
1303 equals( count, 0, "Make sure no events were triggered." );
1306 test("live with multiple events", function(){
1310 var div = jQuery("div#nothiddendivchild");
1312 div.live("click submit", function(){ count++; });
1314 div.trigger("click");
1315 div.trigger("submit");
1317 equals( count, 2, "Make sure both the click and submit were triggered." );
1319 // manually clean up events from elements outside the fixture
1323 test("live with namespaces", function(){
1326 var count1 = 0, count2 = 0;
1328 jQuery("#liveSpan1").live("foo.bar", function(e){
1332 jQuery("#liveSpan1").live("foo.zed", function(e){
1336 jQuery("#liveSpan1").trigger("foo.bar");
1337 equals( count1, 1, "Got live foo.bar" );
1338 equals( count2, 0, "Got live foo.bar" );
1340 count1 = 0, count2 = 0;
1342 jQuery("#liveSpan1").trigger("foo.zed");
1343 equals( count1, 0, "Got live foo.zed" );
1344 equals( count2, 1, "Got live foo.zed" );
1347 count1 = 0, count2 = 0;
1349 jQuery("#liveSpan1").die("foo.zed");
1350 jQuery("#liveSpan1").trigger("foo.bar");
1352 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1353 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1355 count1 = 0, count2 = 0;
1357 jQuery("#liveSpan1").trigger("foo.zed");
1358 equals( count1, 0, "Got live foo.zed" );
1359 equals( count2, 0, "Got live foo.zed" );
1362 jQuery("#liveSpan1").die("foo.bar");
1364 count1 = 0, count2 = 0;
1366 jQuery("#liveSpan1").trigger("foo.bar");
1367 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1368 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1370 jQuery("#liveSpan1").trigger("foo.zed");
1371 equals( count1, 0, "Did not trigger foo.zed again" );
1372 equals( count2, 0, "Did not trigger foo.zed again" );
1375 test("live with change", function(){
1378 var selectChange = 0, checkboxChange = 0;
1380 var select = jQuery("select[name='S1']")
1381 select.live("change", function() {
1385 var checkbox = jQuery("#check2"),
1386 checkboxFunction = function(){
1389 checkbox.live("change", checkboxFunction);
1391 // test click on select
1393 // second click that changed it
1395 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1396 select.trigger("change");
1397 equals( selectChange, 1, "Change on click." );
1399 // test keys on select
1401 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1402 select.trigger("change");
1403 equals( selectChange, 1, "Change on keyup." );
1405 // test click on checkbox
1406 checkbox.trigger("change");
1407 equals( checkboxChange, 1, "Change on checkbox." );
1409 // test blur/focus on text
1410 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1411 text.live("change", function() {
1415 text.val(oldTextVal+"foo");
1416 text.trigger("change");
1417 equals( textChange, 1, "Change on text input." );
1419 text.val(oldTextVal);
1422 // test blur/focus on password
1423 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1424 password.live("change", function() {
1428 password.val(oldPasswordVal + "foo");
1429 password.trigger("change");
1430 equals( passwordChange, 1, "Change on password input." );
1432 password.val(oldPasswordVal);
1433 password.die("change");
1435 // make sure die works
1439 select.die("change");
1440 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1441 select.trigger("change");
1442 equals( selectChange, 0, "Die on click works." );
1445 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1446 select.trigger("change");
1447 equals( selectChange, 0, "Die on keyup works." );
1449 // die specific checkbox
1450 checkbox.die("change", checkboxFunction);
1451 checkbox.trigger("change");
1452 equals( checkboxChange, 1, "Die on checkbox." );
1455 test("live with submit", function() {
1456 var count1 = 0, count2 = 0;
1458 jQuery("#testForm").live("submit", function(ev) {
1460 ev.preventDefault();
1463 jQuery("body").live("submit", function(ev) {
1465 ev.preventDefault();
1468 jQuery("#testForm input[name=sub1]").submit();
1469 equals( count1, 1, "Verify form submit." );
1470 equals( count2, 1, "Verify body submit." );
1472 jQuery("#testForm").die("submit");
1473 jQuery("body").die("submit");
1476 test("live with special events", function() {
1479 jQuery.event.special.foo = {
1480 setup: function( data, namespaces, handler ) {
1481 ok( true, "Setup run." );
1483 teardown: function( namespaces ) {
1484 ok( true, "Teardown run." );
1486 add: function( handleObj ) {
1487 ok( true, "Add run." );
1489 remove: function( handleObj ) {
1490 ok( true, "Remove run." );
1492 _default: function( event ) {
1493 ok( true, "Default run." );
1498 jQuery("#liveSpan1").live("foo.a", function(e){
1499 ok( true, "Handler 1 run." );
1503 jQuery("#liveSpan1").live("foo.b", function(e){
1504 ok( true, "Handler 2 run." );
1507 // Run: Handler 1, Handler 2, Default
1508 jQuery("#liveSpan1").trigger("foo");
1510 // Run: Handler 1, Default
1511 // TODO: Namespace doesn't trigger default (?)
1512 jQuery("#liveSpan1").trigger("foo.a");
1515 jQuery("#liveSpan1").die("foo.a");
1517 // Run: Handler 2, Default
1518 jQuery("#liveSpan1").trigger("foo");
1520 // Run: remove, teardown
1521 jQuery("#liveSpan1").die("foo");
1523 delete jQuery.event.special.foo;
1526 test(".delegate()/.undelegate()", function() {
1529 var submit = 0, div = 0, livea = 0, liveb = 0;
1531 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1532 jQuery("#body").delegate("div", "click", function(){ div++; });
1533 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1534 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1536 // Nothing should trigger on the body
1537 jQuery("body").trigger("click");
1538 equals( submit, 0, "Click on body" );
1539 equals( div, 0, "Click on body" );
1540 equals( livea, 0, "Click on body" );
1541 equals( liveb, 0, "Click on body" );
1543 // This should trigger two events
1544 submit = 0, div = 0, livea = 0, liveb = 0;
1545 jQuery("div#nothiddendiv").trigger("click");
1546 equals( submit, 0, "Click on div" );
1547 equals( div, 1, "Click on div" );
1548 equals( livea, 1, "Click on div" );
1549 equals( liveb, 0, "Click on div" );
1551 // This should trigger three events (w/ bubbling)
1552 submit = 0, div = 0, livea = 0, liveb = 0;
1553 jQuery("div#nothiddendivchild").trigger("click");
1554 equals( submit, 0, "Click on inner div" );
1555 equals( div, 2, "Click on inner div" );
1556 equals( livea, 1, "Click on inner div" );
1557 equals( liveb, 1, "Click on inner div" );
1559 // This should trigger one submit
1560 submit = 0, div = 0, livea = 0, liveb = 0;
1561 jQuery("div#nothiddendivchild").trigger("submit");
1562 equals( submit, 1, "Submit on div" );
1563 equals( div, 0, "Submit on div" );
1564 equals( livea, 0, "Submit on div" );
1565 equals( liveb, 0, "Submit on div" );
1567 // Make sure no other events were removed in the process
1568 submit = 0, div = 0, livea = 0, liveb = 0;
1569 jQuery("div#nothiddendivchild").trigger("click");
1570 equals( submit, 0, "undelegate Click on inner div" );
1571 equals( div, 2, "undelegate Click on inner div" );
1572 equals( livea, 1, "undelegate Click on inner div" );
1573 equals( liveb, 1, "undelegate Click on inner div" );
1575 // Now make sure that the removal works
1576 submit = 0, div = 0, livea = 0, liveb = 0;
1577 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1578 jQuery("div#nothiddendivchild").trigger("click");
1579 equals( submit, 0, "undelegate Click on inner div" );
1580 equals( div, 2, "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 the click wasn't removed too early
1585 submit = 0, div = 0, livea = 0, liveb = 0;
1586 jQuery("div#nothiddendiv").trigger("click");
1587 equals( submit, 0, "undelegate Click on inner div" );
1588 equals( div, 1, "undelegate Click on inner div" );
1589 equals( livea, 1, "undelegate Click on inner div" );
1590 equals( liveb, 0, "undelegate Click on inner div" );
1592 // Make sure that stopPropgation doesn't stop live events
1593 submit = 0, div = 0, livea = 0, liveb = 0;
1594 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1595 jQuery("div#nothiddendivchild").trigger("click");
1596 equals( submit, 0, "stopPropagation Click on inner div" );
1597 equals( div, 1, "stopPropagation Click on inner div" );
1598 equals( livea, 0, "stopPropagation Click on inner div" );
1599 equals( liveb, 1, "stopPropagation Click on inner div" );
1601 // Make sure click events only fire with primary click
1602 submit = 0, div = 0, livea = 0, liveb = 0;
1603 var event = jQuery.Event("click");
1605 jQuery("div#nothiddendiv").trigger(event);
1607 equals( livea, 0, "delegate secondary click" );
1609 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1610 jQuery("#body").undelegate("div#nothiddendiv", "click");
1611 jQuery("#body").undelegate("div", "click");
1612 jQuery("#body").undelegate("div", "submit");
1614 // Test binding with a different context
1615 var clicked = 0, container = jQuery('#main')[0];
1616 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1617 jQuery("div").trigger('click');
1618 jQuery("#foo").trigger('click');
1619 jQuery("#main").trigger('click');
1620 jQuery("body").trigger('click');
1621 equals( clicked, 2, "delegate with a context" );
1623 // Make sure the event is actually stored on the context
1624 ok( jQuery._data(container, "events").live, "delegate with a context" );
1626 // Test unbinding with a different context
1627 jQuery("#main").undelegate("#foo", "click");
1628 jQuery("#foo").trigger('click');
1629 equals( clicked, 2, "undelegate with a context");
1631 // Test binding with event data
1632 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1633 jQuery("#foo").trigger("click");
1634 jQuery("#body").undelegate("#foo", "click");
1636 // Test binding with trigger data
1637 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1638 jQuery("#foo").trigger("click", true);
1639 jQuery("#body").undelegate("#foo", "click");
1641 // Test binding with different this object
1642 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1643 jQuery("#foo").trigger("click");
1644 jQuery("#body").undelegate("#foo", "click");
1646 // Test binding with different this object, event data, and trigger data
1647 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1648 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1649 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1650 equals( data, true, "delegate with with different this object, event data, and trigger data")
1651 }, { foo: "bar" }));
1652 jQuery("#foo").trigger("click", true);
1653 jQuery("#body").undelegate("#foo", "click");
1655 // Verify that return false prevents default action
1656 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1657 var hash = window.location.hash;
1658 jQuery("#anchor2").trigger("click");
1659 equals( window.location.hash, hash, "return false worked" );
1660 jQuery("#body").undelegate("#anchor2", "click");
1662 // Verify that .preventDefault() prevents default action
1663 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1664 var hash = window.location.hash;
1665 jQuery("#anchor2").trigger("click");
1666 equals( window.location.hash, hash, "e.preventDefault() worked" );
1667 jQuery("#body").undelegate("#anchor2", "click");
1669 // Test binding the same handler to multiple points
1671 function callback(){ called++; return false; }
1673 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1674 jQuery("#body").delegate("#anchor2", "click", callback);
1676 jQuery("#nothiddendiv").trigger("click");
1677 equals( called, 1, "Verify that only one click occurred." );
1680 jQuery("#anchor2").trigger("click");
1681 equals( called, 1, "Verify that only one click occurred." );
1683 // Make sure that only one callback is removed
1684 jQuery("#body").undelegate("#anchor2", "click", callback);
1687 jQuery("#nothiddendiv").trigger("click");
1688 equals( called, 1, "Verify that only one click occurred." );
1691 jQuery("#anchor2").trigger("click");
1692 equals( called, 0, "Verify that no click occurred." );
1694 // Make sure that it still works if the selector is the same,
1695 // but the event type is different
1696 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1699 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1702 jQuery("#nothiddendiv").trigger("click");
1703 equals( called, 0, "Verify that no click occurred." );
1706 jQuery("#nothiddendiv").trigger("foo");
1707 equals( called, 1, "Verify that one foo occurred." );
1710 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1712 // Make sure we don't loose the target by DOM modifications
1713 // after the bubble already reached the liveHandler
1714 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1716 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1717 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1719 jQuery("#nothiddendiv span").click();
1720 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1721 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1724 jQuery("#body").undelegate("#nothiddendivchild", "click");
1726 // Verify that .live() ocurs and cancel buble in the same order as
1727 // we would expect .bind() and .click() without delegation
1728 var lived = 0, livee = 0;
1730 // bind one pair in one order
1731 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1732 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1734 jQuery('span#liveSpan1 a').click();
1735 equals( lived, 1, "Verify that only one first handler occurred." );
1736 equals( livee, 0, "Verify that second handler doesn't." );
1738 // and one pair in inverse
1739 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1740 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1744 jQuery('span#liveSpan2 a').click();
1745 equals( lived, 1, "Verify that only one first handler occurred." );
1746 equals( livee, 0, "Verify that second handler doesn't." );
1749 jQuery("#body").undelegate("click");
1751 // Test this, target and currentTarget are correct
1752 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1753 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1754 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1755 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1758 jQuery('span#liveSpan1 a').click();
1760 jQuery("#body").undelegate('span#liveSpan1', 'click');
1762 // Work with deep selectors
1765 function clickB(){ livee++; }
1767 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1768 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1769 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1771 equals( livee, 0, "No clicks, deep selector." );
1774 jQuery("#nothiddendivchild").trigger("click");
1775 equals( livee, 2, "Click, deep selector." );
1778 jQuery("#nothiddendivchild").trigger("mouseover");
1779 equals( livee, 1, "Mouseover, deep selector." );
1781 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1784 jQuery("#nothiddendivchild").trigger("click");
1785 equals( livee, 2, "Click, deep selector." );
1788 jQuery("#nothiddendivchild").trigger("mouseover");
1789 equals( livee, 0, "Mouseover, deep selector." );
1791 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1794 jQuery("#nothiddendivchild").trigger("click");
1795 equals( livee, 1, "Click, deep selector." );
1797 jQuery("#body").undelegate("#nothiddendiv div", "click");
1800 test("undelegate all bound events", function(){
1804 var div = jQuery("#body");
1806 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1809 jQuery("div#nothiddendivchild").trigger("click");
1810 jQuery("div#nothiddendivchild").trigger("submit");
1812 equals( count, 0, "Make sure no events were triggered." );
1815 test("delegate with multiple events", function(){
1819 var div = jQuery("#body");
1821 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1823 jQuery("div#nothiddendivchild").trigger("click");
1824 jQuery("div#nothiddendivchild").trigger("submit");
1826 equals( count, 2, "Make sure both the click and submit were triggered." );
1828 jQuery("#body").undelegate();
1831 test("delegate with change", function(){
1834 var selectChange = 0, checkboxChange = 0;
1836 var select = jQuery("select[name='S1']");
1837 jQuery("#body").delegate("select[name='S1']", "change", function() {
1841 var checkbox = jQuery("#check2"),
1842 checkboxFunction = function(){
1845 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1847 // test click on select
1849 // second click that changed it
1851 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1852 select.trigger("change");
1853 equals( selectChange, 1, "Change on click." );
1855 // test keys on select
1857 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1858 select.trigger("change");
1859 equals( selectChange, 1, "Change on keyup." );
1861 // test click on checkbox
1862 checkbox.trigger("change");
1863 equals( checkboxChange, 1, "Change on checkbox." );
1865 // test blur/focus on text
1866 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1867 jQuery("#body").delegate("#name", "change", function() {
1871 text.val(oldTextVal+"foo");
1872 text.trigger("change");
1873 equals( textChange, 1, "Change on text input." );
1875 text.val(oldTextVal);
1876 jQuery("#body").die("change");
1878 // test blur/focus on password
1879 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1880 jQuery("#body").delegate("#name", "change", function() {
1884 password.val(oldPasswordVal + "foo");
1885 password.trigger("change");
1886 equals( passwordChange, 1, "Change on password input." );
1888 password.val(oldPasswordVal);
1889 jQuery("#body").undelegate("#name", "change");
1891 // make sure die works
1895 jQuery("#body").undelegate("select[name='S1']", "change");
1896 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1897 select.trigger("change");
1898 equals( selectChange, 0, "Die on click works." );
1901 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1902 select.trigger("change");
1903 equals( selectChange, 0, "Die on keyup works." );
1905 // die specific checkbox
1906 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1907 checkbox.trigger("change");
1908 equals( checkboxChange, 1, "Die on checkbox." );
1911 test("delegate with submit", function() {
1912 var count1 = 0, count2 = 0;
1914 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1916 ev.preventDefault();
1919 jQuery(document).delegate("body", "submit", function(ev) {
1921 ev.preventDefault();
1924 jQuery("#testForm input[name=sub1]").submit();
1925 equals( count1, 1, "Verify form submit." );
1926 equals( count2, 1, "Verify body submit." );
1928 jQuery("#body").undelegate();
1929 jQuery(document).undelegate();
1932 test("Non DOM element events", function() {
1937 jQuery(o).bind('nonelementobj', function(e) {
1938 ok( true, "Event on non-DOM object triggered" );
1941 jQuery(o).trigger('nonelementobj');
1944 test("window resize", function() {
1947 jQuery(window).unbind();
1949 jQuery(window).bind("resize", function(){
1950 ok( true, "Resize event fired." );
1951 }).resize().unbind("resize");
1953 ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
1956 test("focusin bubbles", function() {
1957 //create an input and focusin on it
1958 var input = jQuery("<input/>"), order = 0;
1960 input.prependTo("body");
1962 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1963 equals(1,order++,"focusin on the body second")
1966 input.bind("focusin.focusinBubblesTest",function(){
1967 equals(0,order++,"focusin on the element first")
1973 jQuery("body").unbind("focusin.focusinBubblesTest");
1977 test("jQuery(function($) {})", function() {
1979 jQuery(function($) {
1980 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");
1985 test("event properties", function() {
1987 jQuery("#simon1").click(function(event) {
1988 ok( event.timeStamp, "assert event.timeStamp is present" );