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 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
84 equals( e.type, cur, "Verify right multi event was fired." );
85 equals( e.data, obj, "Make sure the data came in correctly." );
92 div.trigger("mouseover");
94 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
95 equals( e.type, cur, "Verify right multi event was fired." );
99 div.trigger("focusin.a");
102 div.trigger("focusout.b");
105 test("bind(), namespace with special add", function() {
108 var div = jQuery("<div/>").bind("test", function(e) {
109 ok( true, "Test event fired." );
114 jQuery.event.special.test = {
115 _default: function(e) {
116 equals( this, document, "Make sure we're at the top of the chain." );
117 equals( e.type, "test", "And that we're still dealing with a test event." );
118 equals( e.target, div[0], "And that the target is correct." );
121 teardown: function(){
122 ok(true, "Teardown called.");
124 add: function( handleObj ) {
125 var handler = handleObj.handler;
126 handleObj.handler = function(e) {
128 handler.apply( this, arguments );
132 ok(true, "Remove called.");
136 div.bind("test.a", {x: 1}, function(e) {
137 ok( !!e.xyz, "Make sure that the data is getting passed through." );
138 equals( e.data.x, 1, "Make sure data is attached properly." );
141 div.bind("test.b", {x: 2}, function(e) {
142 ok( !!e.xyz, "Make sure that the data is getting passed through." );
143 equals( e.data.x, 2, "Make sure data is attached properly." );
150 div.trigger("test.a");
153 div.trigger("test.b");
158 div = jQuery("<div/>").bind("test", function(e) {
159 ok( true, "Test event fired." );
163 div.appendTo("#main").remove();
165 delete jQuery.event.special.test;
168 test("bind(), no data", function() {
170 var handler = function(event) {
171 ok ( !event.data, "Check that no data is added to the event object" );
173 jQuery("#firstp").bind("click", handler).trigger("click");
176 test("bind/one/unbind(Object)", function(){
179 var clickCounter = 0, mouseoverCounter = 0;
180 function handler(event) {
181 if (event.type == "click")
183 else if (event.type == "mouseover")
187 function handlerWithData(event) {
188 if (event.type == "click")
189 clickCounter += event.data;
190 else if (event.type == "mouseover")
191 mouseoverCounter += event.data;
195 $elem.trigger("click").trigger("mouseover");
198 var $elem = jQuery("#firstp")
206 click:handlerWithData,
207 mouseover:handlerWithData
212 equals( clickCounter, 3, "bind(Object)" );
213 equals( mouseoverCounter, 3, "bind(Object)" );
216 equals( clickCounter, 4, "bind(Object)" );
217 equals( mouseoverCounter, 4, "bind(Object)" );
219 jQuery("#firstp").unbind({
225 equals( clickCounter, 4, "bind(Object)" );
226 equals( mouseoverCounter, 4, "bind(Object)" );
229 test("live/die(Object), delegate/undelegate(String, Object)", function() {
232 var clickCounter = 0, mouseoverCounter = 0,
233 $p = jQuery("#firstp"), $a = $p.find("a:first");
236 click: function( event ) {
237 clickCounter += ( event.data || 1 );
239 mouseover: function( event ) {
240 mouseoverCounter += ( event.data || 1 );
245 $a.trigger("click").trigger("mouseover");
249 $p.delegate( "a", events, 2 );
252 equals( clickCounter, 3, "live/delegate" );
253 equals( mouseoverCounter, 3, "live/delegate" );
255 $p.undelegate( "a", events );
258 equals( clickCounter, 4, "undelegate" );
259 equals( mouseoverCounter, 4, "undelegate" );
264 equals( clickCounter, 4, "die" );
265 equals( mouseoverCounter, 4, "die" );
268 test("live/delegate immediate propagation", function() {
271 var $p = jQuery("#firstp"), $a = $p.find("a:first"), lastClick;
274 $a.live( "click", function(e) {
275 lastClick = "click1";
276 e.stopImmediatePropagation();
278 $a.live( "click", function(e) {
279 lastClick = "click2";
281 $a.trigger( "click" );
282 equals( lastClick, "click1", "live stopImmediatePropagation" );
286 $p.delegate( "a", "click", function(e) {
287 lastClick = "click1";
288 e.stopImmediatePropagation();
290 $p.delegate( "a", "click", function(e) {
291 lastClick = "click2";
293 $a.trigger( "click" );
294 equals( lastClick, "click1", "delegate stopImmediatePropagation" );
295 $p.undelegate( "click" );
298 test("bind/delegate bubbling, isDefaultPrevented (Bug #7793)", function() {
300 var $anchor2 = jQuery( "#anchor2" ),
301 $main = jQuery( "#main" ),
302 fakeClick = function($jq) {
303 // Use a native click so we don't get jQuery simulated bubbling
304 if ( document.createEvent ) {
305 var e = document.createEvent( "MouseEvents" );
306 e.initEvent( "click", true, true );
307 $jq[0].dispatchEvent(e);
309 else if ( $jq[0].click ) {
310 $jq[0].click(); // IE
313 $anchor2.click(function(e) {
316 $main.delegate("#foo", "click", function(e) {
317 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
319 fakeClick( $anchor2 );
320 $anchor2.unbind( "click" );
321 $main.undelegate( "click" );
322 $anchor2.click(function(e) {
323 // Let the default action occur
325 $main.delegate("#foo", "click", function(e) {
326 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
328 fakeClick( $anchor2 );
329 $anchor2.unbind( "click" );
330 $main.undelegate( "click" );
333 test("bind(), iframes", function() {
334 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
335 var doc = jQuery("#loadediframe").contents();
337 jQuery("div", doc).bind("click", function() {
338 ok( true, "Binding to element inside iframe" );
339 }).click().unbind('click');
342 test("bind(), trigger change on select", function() {
345 function selectOnChange(event) {
346 equals( event.data, counter++, "Event.data is not a global event object" );
348 jQuery("#form select").each(function(i){
349 jQuery(this).bind('change', i, selectOnChange);
350 }).trigger('change');
353 test("bind(), namespaced events, cloned events", function() {
356 jQuery("#firstp").bind("custom.test",function(e){
357 ok(true, "Custom event triggered");
360 jQuery("#firstp").bind("click",function(e){
361 ok(true, "Normal click triggered");
364 jQuery("#firstp").bind("click.test",function(e){
365 ok(true, "Namespaced click triggered");
368 // Trigger both bound fn (2)
369 jQuery("#firstp").trigger("click");
371 // Trigger one bound fn (1)
372 jQuery("#firstp").trigger("click.test");
374 // Remove only the one fn
375 jQuery("#firstp").unbind("click.test");
377 // Trigger the remaining fn (1)
378 jQuery("#firstp").trigger("click");
380 // Remove the remaining fn
381 jQuery("#firstp").unbind(".test");
383 // Trigger the remaining fn (0)
384 jQuery("#firstp").trigger("custom");
386 // using contents will get comments regular, text, and comment nodes
387 jQuery("#nonnodes").contents().bind("tester", function () {
388 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
389 }).trigger("tester");
391 // Make sure events stick with appendTo'd elements (which are cloned) #2027
392 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
393 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
396 test("bind(), multi-namespaced events", function() {
408 function check(name, msg){
409 same(name, order.shift(), msg);
412 jQuery("#firstp").bind("custom.test",function(e){
413 check("custom.test", "Custom event triggered");
416 jQuery("#firstp").bind("custom.test2",function(e){
417 check("custom.test2", "Custom event triggered");
420 jQuery("#firstp").bind("click.test",function(e){
421 check("click.test", "Normal click triggered");
424 jQuery("#firstp").bind("click.test.abc",function(e){
425 check("click.test.abc", "Namespaced click triggered");
428 // Those would not trigger/unbind (#5303)
429 jQuery("#firstp").trigger("click.a.test");
430 jQuery("#firstp").unbind("click.a.test");
432 // Trigger both bound fn (1)
433 jQuery("#firstp").trigger("click.test.abc");
435 // Trigger one bound fn (1)
436 jQuery("#firstp").trigger("click.abc");
438 // Trigger two bound fn (2)
439 jQuery("#firstp").trigger("click.test");
441 // Remove only the one fn
442 jQuery("#firstp").unbind("click.abc");
444 // Trigger the remaining fn (1)
445 jQuery("#firstp").trigger("click");
447 // Remove the remaining fn
448 jQuery("#firstp").unbind(".test");
450 // Trigger the remaining fn (1)
451 jQuery("#firstp").trigger("custom");
454 test("bind(), with same function", function() {
457 var count = 0 , func = function(){
461 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
462 jQuery("#liveHandlerOrder").trigger("foo.bar");
464 equals(count, 1, "Verify binding function with multiple namespaces." );
466 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
467 jQuery("#liveHandlerOrder").trigger("foo.bar");
469 equals(count, 1, "Verify that removing events still work." );
472 test("bind(), make sure order is maintained", function() {
475 var elem = jQuery("#firstp"), log = [], check = [];
477 for ( var i = 0; i < 100; i++ ) (function(i){
478 elem.bind( "click", function(){
485 elem.trigger("click");
487 equals( log.join(","), check.join(","), "Make sure order was maintained." );
489 elem.unbind("click");
492 test("bind(), with different this object", function() {
494 var thisObject = { myThis: true },
495 data = { myData: true },
496 handler1 = function( event ) {
497 equals( this, thisObject, "bind() with different this object" );
499 handler2 = function( event ) {
500 equals( this, thisObject, "bind() with different this object and data" );
501 equals( event.data, data, "bind() with different this object and data" );
505 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
506 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
508 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
511 test("bind(name, false), unbind(name, false)", function() {
515 jQuery("#main").bind("click", function(e){ main++; });
516 jQuery("#ap").trigger("click");
517 equals( main, 1, "Verify that the trigger happened correctly." );
520 jQuery("#ap").bind("click", false);
521 jQuery("#ap").trigger("click");
522 equals( main, 0, "Verify that no bubble happened." );
525 jQuery("#ap").unbind("click", false);
526 jQuery("#ap").trigger("click");
527 equals( main, 1, "Verify that the trigger happened correctly." );
530 test("bind()/trigger()/unbind() on plain object", function() {
535 // Make sure it doesn't complain when no events are found
536 jQuery(obj).trigger("test");
538 // Make sure it doesn't complain when no events are found
539 jQuery(obj).unbind("test");
543 ok( true, "Custom event run." );
546 ok( true, "Custom submit event run." );
550 var events = jQuery(obj).data("__events__");
551 ok( events, "Object has events bound." );
552 equals( obj.events, undefined, "Events object on plain objects is not events" );
553 equals( typeof events, "function", "'events' expando is a function on plain objects." );
554 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
555 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
558 jQuery(obj).trigger("test");
559 jQuery(obj).trigger("submit");
561 jQuery(obj).unbind("test");
562 jQuery(obj).unbind("submit");
565 jQuery(obj).trigger("test");
567 // Make sure it doesn't complain when no events are found
568 jQuery(obj).unbind("test");
570 equals( obj.__events__, undefined, "Make sure events object is removed" );
573 test("unbind(type)", function() {
576 var $elem = jQuery("#firstp"),
580 ok( false, message );
583 message = "unbind passing function";
584 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
586 message = "unbind all from event";
587 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
589 message = "unbind all";
590 $elem.bind('error1', error).unbind().triggerHandler('error1');
592 message = "unbind many with function";
593 $elem.bind('error1 error2',error)
594 .unbind('error1 error2', error )
595 .trigger('error1').triggerHandler('error2');
597 message = "unbind many"; // #3538
598 $elem.bind('error1 error2',error)
599 .unbind('error1 error2')
600 .trigger('error1').triggerHandler('error2');
602 message = "unbind without a type or handler";
603 $elem.bind("error1 error2.test",error)
605 .trigger("error1").triggerHandler("error2");
608 test("unbind(eventObject)", function() {
611 var $elem = jQuery("#firstp"),
614 function assert( expected ){
616 $elem.trigger('foo').triggerHandler('bar');
617 equals( num, expected, "Check the right handlers are triggered" );
621 // This handler shouldn't be unbound
622 .bind('foo', function(){
625 .bind('foo', function(e){
630 .bind('bar', function(){
644 test("hover()", function() {
646 handler1 = function( event ) { ++times; },
647 handler2 = function( event ) { ++times; };
650 .hover(handler1, handler2)
651 .mouseenter().mouseleave()
652 .unbind("mouseenter", handler1)
653 .unbind("mouseleave", handler2)
655 .mouseenter().mouseleave()
656 .unbind("mouseenter mouseleave", handler1)
657 .mouseenter().mouseleave();
659 equals( times, 4, "hover handlers fired" );
662 test("trigger() shortcuts", function() {
664 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
665 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
666 equals( close.length, 0, "Context element does not exist, length must be zero" );
667 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
671 jQuery("#check1").click(function() {
672 ok( true, "click event handler for checkbox gets fired twice, see #815" );
676 jQuery('#firstp')[0].onclick = function(event) {
679 jQuery('#firstp').click();
680 equals( counter, 1, "Check that click, triggers onclick event handler also" );
682 var clickCounter = 0;
683 jQuery('#simon1')[0].onclick = function(event) {
686 jQuery('#simon1').click();
687 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
689 jQuery('<img />').load(function(){
690 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
694 test("trigger() bubbling", function() {
697 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
699 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
700 jQuery("html").bind("click", function(e){ html++; });
701 jQuery("body").bind("click", function(e){ body++; });
702 jQuery("#main").bind("click", function(e){ main++; });
703 jQuery("#ap").bind("click", function(){ ap++; return false; });
705 jQuery("html").trigger("click");
706 equals( doc, 1, "HTML bubble" );
707 equals( html, 1, "HTML bubble" );
709 jQuery("body").trigger("click");
710 equals( doc, 2, "Body bubble" );
711 equals( html, 2, "Body bubble" );
712 equals( body, 1, "Body bubble" );
714 jQuery("#main").trigger("click");
715 equals( doc, 3, "Main bubble" );
716 equals( html, 3, "Main bubble" );
717 equals( body, 2, "Main bubble" );
718 equals( main, 1, "Main bubble" );
720 jQuery("#ap").trigger("click");
721 equals( doc, 3, "ap bubble" );
722 equals( html, 3, "ap bubble" );
723 equals( body, 2, "ap bubble" );
724 equals( main, 1, "ap bubble" );
725 equals( ap, 1, "ap bubble" );
728 test("trigger(type, [data], [fn])", function() {
731 var handler = function(event, a, b, c) {
732 equals( event.type, "click", "check passed data" );
733 equals( a, 1, "check passed data" );
734 equals( b, "2", "check passed data" );
735 equals( c, "abc", "check passed data" );
739 var $elem = jQuery("#firstp");
741 // Simulate a "native" click
742 $elem[0].click = function(){
743 ok( true, "Native call was triggered" );
746 // Triggers handlrs and native
748 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
750 // Simulate a "native" click
751 $elem[0].click = function(){
752 ok( false, "Native call was triggered" );
755 // Trigger only the handlers (no native)
757 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
761 jQuery('#form input:first').hide().trigger('focus');
765 ok( pass, "Trigger focus on hidden element" );
769 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
773 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
775 var form = jQuery("<form action=''></form>").appendTo("body");
777 // Make sure it can be prevented locally
778 form.submit(function(){
779 ok( true, "Local bind still works." );
784 form.trigger("submit");
786 form.unbind("submit");
788 jQuery(document).submit(function(){
789 ok( true, "Make sure bubble works up to document." );
794 form.trigger("submit");
796 jQuery(document).unbind("submit");
801 test("jQuery.Event.currentTarget", function(){
804 test("trigger(eventObject, [data], [fn])", function() {
807 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
808 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
810 var event = jQuery.Event("noNew");
811 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
812 equals( event.type, "noNew", "Verify its type" );
814 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
815 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
816 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
818 event.preventDefault();
819 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
820 event.stopPropagation();
821 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
823 event.isPropagationStopped = function(){ return false };
824 event.stopImmediatePropagation();
825 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
826 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
828 $parent.bind('foo',function(e){
830 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
831 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
832 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
833 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
836 // test with an event object
837 event = new jQuery.Event("foo");
838 event.secret = 'boo!';
839 $child.trigger(event);
841 // test with a literal object
842 $child.trigger({type:'foo', secret:'boo!'});
847 ok( false, "This assertion shouldn't be reached");
850 $parent.bind('foo', error );
852 $child.bind('foo',function(e, a, b, c ){
853 equals( arguments.length, 4, "Check arguments length");
854 equals( a, 1, "Check first custom argument");
855 equals( b, 2, "Check second custom argument");
856 equals( c, 3, "Check third custom argument");
858 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
859 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
860 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
863 e.stopImmediatePropagation();
868 // We should add this back in when we want to test the order
869 // in which event handlers are iterated.
870 //$child.bind('foo', error );
872 event = new jQuery.Event("foo");
873 $child.trigger( event, [1,2,3] ).unbind();
874 equals( event.result, "result", "Check event.result attribute");
876 // Will error if it bubbles
877 $child.triggerHandler('foo');
880 $parent.unbind().remove();
883 test("jQuery.Event.currentTarget", function(){
887 $elem = jQuery('<button>a</button>').click(function(e){
888 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
892 $elem.trigger('click');
898 test("toggle(Function, Function, ...)", function() {
902 fn1 = function(e) { count++; },
903 fn2 = function(e) { count--; },
904 preventDefault = function(e) { e.preventDefault() },
905 link = jQuery('#mark');
906 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
907 equals( count, 1, "Check for toggle(fn, fn)" );
909 jQuery("#firstp").toggle(function () {
910 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
911 }, function() {}).trigger("click", [ 1, 2, 3 ]);
914 jQuery("#simon1").one("click", function() {
915 ok( true, "Execute event only once" );
916 jQuery(this).toggle(function() {
917 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
919 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
922 }).click().click().click();
937 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
939 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
941 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
943 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
945 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
947 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
949 $div.unbind('click',fns[0]);
950 var data = jQuery.data( $div[0], 'events' );
951 ok( !data, "Unbinding one function from toggle unbinds them all");
953 // Test Multi-Toggles
955 $div = jQuery("<div/>");
956 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
958 same( a, [1], "Check that a click worked." );
960 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
962 same( a, [1,2], "Check that a click worked with a second toggle." );
963 same( b, [1], "Check that a click worked with a second toggle." );
966 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
967 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
970 test(".live()/.die()", function() {
973 var submit = 0, div = 0, livea = 0, liveb = 0;
975 jQuery("div").live("submit", function(){ submit++; return false; });
976 jQuery("div").live("click", function(){ div++; });
977 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
978 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
980 // Nothing should trigger on the body
981 jQuery("body").trigger("click");
982 equals( submit, 0, "Click on body" );
983 equals( div, 0, "Click on body" );
984 equals( livea, 0, "Click on body" );
985 equals( liveb, 0, "Click on body" );
987 // This should trigger two events
988 submit = 0, div = 0, livea = 0, liveb = 0;
989 jQuery("div#nothiddendiv").trigger("click");
990 equals( submit, 0, "Click on div" );
991 equals( div, 1, "Click on div" );
992 equals( livea, 1, "Click on div" );
993 equals( liveb, 0, "Click on div" );
995 // This should trigger three events (w/ bubbling)
996 submit = 0, div = 0, livea = 0, liveb = 0;
997 jQuery("div#nothiddendivchild").trigger("click");
998 equals( submit, 0, "Click on inner div" );
999 equals( div, 2, "Click on inner div" );
1000 equals( livea, 1, "Click on inner div" );
1001 equals( liveb, 1, "Click on inner div" );
1003 // This should trigger one submit
1004 submit = 0, div = 0, livea = 0, liveb = 0;
1005 jQuery("div#nothiddendivchild").trigger("submit");
1006 equals( submit, 1, "Submit on div" );
1007 equals( div, 0, "Submit on div" );
1008 equals( livea, 0, "Submit on div" );
1009 equals( liveb, 0, "Submit on div" );
1011 // Make sure no other events were removed in the process
1012 submit = 0, div = 0, livea = 0, liveb = 0;
1013 jQuery("div#nothiddendivchild").trigger("click");
1014 equals( submit, 0, "die Click on inner div" );
1015 equals( div, 2, "die Click on inner div" );
1016 equals( livea, 1, "die Click on inner div" );
1017 equals( liveb, 1, "die Click on inner div" );
1019 // Now make sure that the removal works
1020 submit = 0, div = 0, livea = 0, liveb = 0;
1021 jQuery("div#nothiddendivchild").die("click");
1022 jQuery("div#nothiddendivchild").trigger("click");
1023 equals( submit, 0, "die Click on inner div" );
1024 equals( div, 2, "die Click on inner div" );
1025 equals( livea, 1, "die Click on inner div" );
1026 equals( liveb, 0, "die Click on inner div" );
1028 // Make sure that the click wasn't removed too early
1029 submit = 0, div = 0, livea = 0, liveb = 0;
1030 jQuery("div#nothiddendiv").trigger("click");
1031 equals( submit, 0, "die Click on inner div" );
1032 equals( div, 1, "die Click on inner div" );
1033 equals( livea, 1, "die Click on inner div" );
1034 equals( liveb, 0, "die Click on inner div" );
1036 // Make sure that stopPropgation doesn't stop live events
1037 submit = 0, div = 0, livea = 0, liveb = 0;
1038 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1039 jQuery("div#nothiddendivchild").trigger("click");
1040 equals( submit, 0, "stopPropagation Click on inner div" );
1041 equals( div, 1, "stopPropagation Click on inner div" );
1042 equals( livea, 0, "stopPropagation Click on inner div" );
1043 equals( liveb, 1, "stopPropagation Click on inner div" );
1045 // Make sure click events only fire with primary click
1046 submit = 0, div = 0, livea = 0, liveb = 0;
1047 var event = jQuery.Event("click");
1049 jQuery("div#nothiddendiv").trigger(event);
1051 equals( livea, 0, "live secondary click" );
1053 jQuery("div#nothiddendivchild").die("click");
1054 jQuery("div#nothiddendiv").die("click");
1055 jQuery("div").die("click");
1056 jQuery("div").die("submit");
1058 // Test binding with a different context
1059 var clicked = 0, container = jQuery('#main')[0];
1060 jQuery("#foo", container).live("click", function(e){ clicked++; });
1061 jQuery("div").trigger('click');
1062 jQuery("#foo").trigger('click');
1063 jQuery("#main").trigger('click');
1064 jQuery("body").trigger('click');
1065 equals( clicked, 2, "live with a context" );
1067 // Make sure the event is actually stored on the context
1068 ok( jQuery.data(container, "events").live, "live with a context" );
1070 // Test unbinding with a different context
1071 jQuery("#foo", container).die("click");
1072 jQuery("#foo").trigger('click');
1073 equals( clicked, 2, "die with a context");
1075 // Test binding with event data
1076 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1077 jQuery("#foo").trigger("click").die("click");
1079 // Test binding with trigger data
1080 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1081 jQuery("#foo").trigger("click", true).die("click");
1083 // Test binding with different this object
1084 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1085 jQuery("#foo").trigger("click").die("click");
1087 // Test binding with different this object, event data, and trigger data
1088 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1089 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1090 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1091 equals( data, true, "live with with different this object, event data, and trigger data")
1092 }, { foo: "bar" }));
1093 jQuery("#foo").trigger("click", true).die("click");
1095 // Verify that return false prevents default action
1096 jQuery("#anchor2").live("click", function(){ return false; });
1097 var hash = window.location.hash;
1098 jQuery("#anchor2").trigger("click");
1099 equals( window.location.hash, hash, "return false worked" );
1100 jQuery("#anchor2").die("click");
1102 // Verify that .preventDefault() prevents default action
1103 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1104 var hash = window.location.hash;
1105 jQuery("#anchor2").trigger("click");
1106 equals( window.location.hash, hash, "e.preventDefault() worked" );
1107 jQuery("#anchor2").die("click");
1109 // Test binding the same handler to multiple points
1111 function callback(){ called++; return false; }
1113 jQuery("#nothiddendiv").live("click", callback);
1114 jQuery("#anchor2").live("click", callback);
1116 jQuery("#nothiddendiv").trigger("click");
1117 equals( called, 1, "Verify that only one click occurred." );
1120 jQuery("#anchor2").trigger("click");
1121 equals( called, 1, "Verify that only one click occurred." );
1123 // Make sure that only one callback is removed
1124 jQuery("#anchor2").die("click", callback);
1127 jQuery("#nothiddendiv").trigger("click");
1128 equals( called, 1, "Verify that only one click occurred." );
1131 jQuery("#anchor2").trigger("click");
1132 equals( called, 0, "Verify that no click occurred." );
1134 // Make sure that it still works if the selector is the same,
1135 // but the event type is different
1136 jQuery("#nothiddendiv").live("foo", callback);
1139 jQuery("#nothiddendiv").die("click", callback);
1142 jQuery("#nothiddendiv").trigger("click");
1143 equals( called, 0, "Verify that no click occurred." );
1146 jQuery("#nothiddendiv").trigger("foo");
1147 equals( called, 1, "Verify that one foo occurred." );
1150 jQuery("#nothiddendiv").die("foo", callback);
1152 // Make sure we don't loose the target by DOM modifications
1153 // after the bubble already reached the liveHandler
1154 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1156 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1157 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1159 jQuery("#nothiddendiv span").click();
1160 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1161 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1164 jQuery("#nothiddendivchild").die("click");
1166 // Verify that .live() ocurs and cancel buble in the same order as
1167 // we would expect .bind() and .click() without delegation
1168 var lived = 0, livee = 0;
1170 // bind one pair in one order
1171 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1172 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1174 jQuery('span#liveSpan1 a').click();
1175 equals( lived, 1, "Verify that only one first handler occurred." );
1176 equals( livee, 0, "Verify that second handler doesn't." );
1178 // and one pair in inverse
1179 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1180 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1184 jQuery('span#liveSpan2 a').click();
1185 equals( lived, 1, "Verify that only one first handler occurred." );
1186 equals( livee, 0, "Verify that second handler doesn't." );
1189 jQuery("span#liveSpan1 a").die("click")
1190 jQuery("span#liveSpan1").die("click");
1191 jQuery("span#liveSpan2 a").die("click");
1192 jQuery("span#liveSpan2").die("click");
1194 // Test this, target and currentTarget are correct
1195 jQuery('span#liveSpan1').live('click', function(e){
1196 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1197 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1198 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1201 jQuery('span#liveSpan1 a').click();
1203 jQuery('span#liveSpan1').die('click');
1205 // Work with deep selectors
1208 function clickB(){ livee++; }
1210 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1211 jQuery("#nothiddendiv div").live("click", clickB);
1212 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1214 equals( livee, 0, "No clicks, deep selector." );
1217 jQuery("#nothiddendivchild").trigger("click");
1218 equals( livee, 2, "Click, deep selector." );
1221 jQuery("#nothiddendivchild").trigger("mouseover");
1222 equals( livee, 1, "Mouseover, deep selector." );
1224 jQuery("#nothiddendiv div").die("mouseover");
1227 jQuery("#nothiddendivchild").trigger("click");
1228 equals( livee, 2, "Click, deep selector." );
1231 jQuery("#nothiddendivchild").trigger("mouseover");
1232 equals( livee, 0, "Mouseover, deep selector." );
1234 jQuery("#nothiddendiv div").die("click", clickB);
1237 jQuery("#nothiddendivchild").trigger("click");
1238 equals( livee, 1, "Click, deep selector." );
1240 jQuery("#nothiddendiv div").die("click");
1242 jQuery("#nothiddendiv div").live("blur", function(){
1243 ok( true, "Live div trigger blur." );
1246 jQuery("#nothiddendiv div").trigger("blur");
1248 jQuery("#nothiddendiv div").die("blur");
1251 test("die all bound events", function(){
1255 var div = jQuery("div#nothiddendivchild");
1257 div.live("click submit", function(){ count++; });
1260 div.trigger("click");
1261 div.trigger("submit");
1263 equals( count, 0, "Make sure no events were triggered." );
1266 test("live with multiple events", function(){
1270 var div = jQuery("div#nothiddendivchild");
1272 div.live("click submit", function(){ count++; });
1274 div.trigger("click");
1275 div.trigger("submit");
1277 equals( count, 2, "Make sure both the click and submit were triggered." );
1280 test("live with namespaces", function(){
1283 var count1 = 0, count2 = 0;
1285 jQuery("#liveSpan1").live("foo.bar", function(e){
1289 jQuery("#liveSpan1").live("foo.zed", function(e){
1293 jQuery("#liveSpan1").trigger("foo.bar");
1294 equals( count1, 1, "Got live foo.bar" );
1295 equals( count2, 0, "Got live foo.bar" );
1297 count1 = 0, count2 = 0;
1299 jQuery("#liveSpan1").trigger("foo.zed");
1300 equals( count1, 0, "Got live foo.zed" );
1301 equals( count2, 1, "Got live foo.zed" );
1304 count1 = 0, count2 = 0;
1306 jQuery("#liveSpan1").die("foo.zed");
1307 jQuery("#liveSpan1").trigger("foo.bar");
1309 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1310 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1312 count1 = 0, count2 = 0;
1314 jQuery("#liveSpan1").trigger("foo.zed");
1315 equals( count1, 0, "Got live foo.zed" );
1316 equals( count2, 0, "Got live foo.zed" );
1319 jQuery("#liveSpan1").die("foo.bar");
1321 count1 = 0, count2 = 0;
1323 jQuery("#liveSpan1").trigger("foo.bar");
1324 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1325 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1327 jQuery("#liveSpan1").trigger("foo.zed");
1328 equals( count1, 0, "Did not trigger foo.zed again" );
1329 equals( count2, 0, "Did not trigger foo.zed again" );
1332 test("live with change", function(){
1335 var selectChange = 0, checkboxChange = 0;
1337 var select = jQuery("select[name='S1']")
1338 select.live("change", function() {
1342 var checkbox = jQuery("#check2"),
1343 checkboxFunction = function(){
1346 checkbox.live("change", checkboxFunction);
1348 // test click on select
1350 // second click that changed it
1352 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1353 select.trigger("change");
1354 equals( selectChange, 1, "Change on click." );
1356 // test keys on select
1358 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1359 select.trigger("change");
1360 equals( selectChange, 1, "Change on keyup." );
1362 // test click on checkbox
1363 checkbox.trigger("change");
1364 equals( checkboxChange, 1, "Change on checkbox." );
1366 // test blur/focus on text
1367 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1368 text.live("change", function() {
1372 text.val(oldTextVal+"foo");
1373 text.trigger("change");
1374 equals( textChange, 1, "Change on text input." );
1376 text.val(oldTextVal);
1379 // test blur/focus on password
1380 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1381 password.live("change", function() {
1385 password.val(oldPasswordVal + "foo");
1386 password.trigger("change");
1387 equals( passwordChange, 1, "Change on password input." );
1389 password.val(oldPasswordVal);
1390 password.die("change");
1392 // make sure die works
1396 select.die("change");
1397 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1398 select.trigger("change");
1399 equals( selectChange, 0, "Die on click works." );
1402 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1403 select.trigger("change");
1404 equals( selectChange, 0, "Die on keyup works." );
1406 // die specific checkbox
1407 checkbox.die("change", checkboxFunction);
1408 checkbox.trigger("change");
1409 equals( checkboxChange, 1, "Die on checkbox." );
1412 test("live with submit", function() {
1413 var count1 = 0, count2 = 0;
1415 jQuery("#testForm").live("submit", function(ev) {
1417 ev.preventDefault();
1420 jQuery("body").live("submit", function(ev) {
1422 ev.preventDefault();
1425 jQuery("#testForm input[name=sub1]").submit();
1426 equals( count1, 1, "Verify form submit." );
1427 equals( count2, 1, "Verify body submit." );
1429 jQuery("#testForm").die("submit");
1430 jQuery("body").die("submit");
1433 test("live with special events", function() {
1436 jQuery.event.special.foo = {
1437 setup: function( data, namespaces, handler ) {
1438 ok( true, "Setup run." );
1440 teardown: function( namespaces ) {
1441 ok( true, "Teardown run." );
1443 add: function( handleObj ) {
1444 ok( true, "Add run." );
1446 remove: function( handleObj ) {
1447 ok( true, "Remove run." );
1449 _default: function( event ) {
1450 ok( true, "Default run." );
1455 jQuery("#liveSpan1").live("foo.a", function(e){
1456 ok( true, "Handler 1 run." );
1460 jQuery("#liveSpan1").live("foo.b", function(e){
1461 ok( true, "Handler 2 run." );
1464 // Run: Handler 1, Handler 2, Default
1465 jQuery("#liveSpan1").trigger("foo");
1467 // Run: Handler 1, Default
1468 // TODO: Namespace doesn't trigger default (?)
1469 jQuery("#liveSpan1").trigger("foo.a");
1472 jQuery("#liveSpan1").die("foo.a");
1474 // Run: Handler 2, Default
1475 jQuery("#liveSpan1").trigger("foo");
1477 // Run: remove, teardown
1478 jQuery("#liveSpan1").die("foo");
1480 delete jQuery.event.special.foo;
1483 test(".delegate()/.undelegate()", function() {
1486 var submit = 0, div = 0, livea = 0, liveb = 0;
1488 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1489 jQuery("#body").delegate("div", "click", function(){ div++; });
1490 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1491 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1493 // Nothing should trigger on the body
1494 jQuery("body").trigger("click");
1495 equals( submit, 0, "Click on body" );
1496 equals( div, 0, "Click on body" );
1497 equals( livea, 0, "Click on body" );
1498 equals( liveb, 0, "Click on body" );
1500 // This should trigger two events
1501 submit = 0, div = 0, livea = 0, liveb = 0;
1502 jQuery("div#nothiddendiv").trigger("click");
1503 equals( submit, 0, "Click on div" );
1504 equals( div, 1, "Click on div" );
1505 equals( livea, 1, "Click on div" );
1506 equals( liveb, 0, "Click on div" );
1508 // This should trigger three events (w/ bubbling)
1509 submit = 0, div = 0, livea = 0, liveb = 0;
1510 jQuery("div#nothiddendivchild").trigger("click");
1511 equals( submit, 0, "Click on inner div" );
1512 equals( div, 2, "Click on inner div" );
1513 equals( livea, 1, "Click on inner div" );
1514 equals( liveb, 1, "Click on inner div" );
1516 // This should trigger one submit
1517 submit = 0, div = 0, livea = 0, liveb = 0;
1518 jQuery("div#nothiddendivchild").trigger("submit");
1519 equals( submit, 1, "Submit on div" );
1520 equals( div, 0, "Submit on div" );
1521 equals( livea, 0, "Submit on div" );
1522 equals( liveb, 0, "Submit on div" );
1524 // Make sure no other events were removed in the process
1525 submit = 0, div = 0, livea = 0, liveb = 0;
1526 jQuery("div#nothiddendivchild").trigger("click");
1527 equals( submit, 0, "undelegate Click on inner div" );
1528 equals( div, 2, "undelegate Click on inner div" );
1529 equals( livea, 1, "undelegate Click on inner div" );
1530 equals( liveb, 1, "undelegate Click on inner div" );
1532 // Now make sure that the removal works
1533 submit = 0, div = 0, livea = 0, liveb = 0;
1534 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1535 jQuery("div#nothiddendivchild").trigger("click");
1536 equals( submit, 0, "undelegate Click on inner div" );
1537 equals( div, 2, "undelegate Click on inner div" );
1538 equals( livea, 1, "undelegate Click on inner div" );
1539 equals( liveb, 0, "undelegate Click on inner div" );
1541 // Make sure that the click wasn't removed too early
1542 submit = 0, div = 0, livea = 0, liveb = 0;
1543 jQuery("div#nothiddendiv").trigger("click");
1544 equals( submit, 0, "undelegate Click on inner div" );
1545 equals( div, 1, "undelegate Click on inner div" );
1546 equals( livea, 1, "undelegate Click on inner div" );
1547 equals( liveb, 0, "undelegate Click on inner div" );
1549 // Make sure that stopPropgation doesn't stop live events
1550 submit = 0, div = 0, livea = 0, liveb = 0;
1551 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1552 jQuery("div#nothiddendivchild").trigger("click");
1553 equals( submit, 0, "stopPropagation Click on inner div" );
1554 equals( div, 1, "stopPropagation Click on inner div" );
1555 equals( livea, 0, "stopPropagation Click on inner div" );
1556 equals( liveb, 1, "stopPropagation Click on inner div" );
1558 // Make sure click events only fire with primary click
1559 submit = 0, div = 0, livea = 0, liveb = 0;
1560 var event = jQuery.Event("click");
1562 jQuery("div#nothiddendiv").trigger(event);
1564 equals( livea, 0, "delegate secondary click" );
1566 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1567 jQuery("#body").undelegate("div#nothiddendiv", "click");
1568 jQuery("#body").undelegate("div", "click");
1569 jQuery("#body").undelegate("div", "submit");
1571 // Test binding with a different context
1572 var clicked = 0, container = jQuery('#main')[0];
1573 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1574 jQuery("div").trigger('click');
1575 jQuery("#foo").trigger('click');
1576 jQuery("#main").trigger('click');
1577 jQuery("body").trigger('click');
1578 equals( clicked, 2, "delegate with a context" );
1580 // Make sure the event is actually stored on the context
1581 ok( jQuery.data(container, "events").live, "delegate with a context" );
1583 // Test unbinding with a different context
1584 jQuery("#main").undelegate("#foo", "click");
1585 jQuery("#foo").trigger('click');
1586 equals( clicked, 2, "undelegate with a context");
1588 // Test binding with event data
1589 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1590 jQuery("#foo").trigger("click");
1591 jQuery("#body").undelegate("#foo", "click");
1593 // Test binding with trigger data
1594 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1595 jQuery("#foo").trigger("click", true);
1596 jQuery("#body").undelegate("#foo", "click");
1598 // Test binding with different this object
1599 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1600 jQuery("#foo").trigger("click");
1601 jQuery("#body").undelegate("#foo", "click");
1603 // Test binding with different this object, event data, and trigger data
1604 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1605 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1606 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1607 equals( data, true, "delegate with with different this object, event data, and trigger data")
1608 }, { foo: "bar" }));
1609 jQuery("#foo").trigger("click", true);
1610 jQuery("#body").undelegate("#foo", "click");
1612 // Verify that return false prevents default action
1613 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1614 var hash = window.location.hash;
1615 jQuery("#anchor2").trigger("click");
1616 equals( window.location.hash, hash, "return false worked" );
1617 jQuery("#body").undelegate("#anchor2", "click");
1619 // Verify that .preventDefault() prevents default action
1620 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1621 var hash = window.location.hash;
1622 jQuery("#anchor2").trigger("click");
1623 equals( window.location.hash, hash, "e.preventDefault() worked" );
1624 jQuery("#body").undelegate("#anchor2", "click");
1626 // Test binding the same handler to multiple points
1628 function callback(){ called++; return false; }
1630 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1631 jQuery("#body").delegate("#anchor2", "click", callback);
1633 jQuery("#nothiddendiv").trigger("click");
1634 equals( called, 1, "Verify that only one click occurred." );
1637 jQuery("#anchor2").trigger("click");
1638 equals( called, 1, "Verify that only one click occurred." );
1640 // Make sure that only one callback is removed
1641 jQuery("#body").undelegate("#anchor2", "click", callback);
1644 jQuery("#nothiddendiv").trigger("click");
1645 equals( called, 1, "Verify that only one click occurred." );
1648 jQuery("#anchor2").trigger("click");
1649 equals( called, 0, "Verify that no click occurred." );
1651 // Make sure that it still works if the selector is the same,
1652 // but the event type is different
1653 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1656 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1659 jQuery("#nothiddendiv").trigger("click");
1660 equals( called, 0, "Verify that no click occurred." );
1663 jQuery("#nothiddendiv").trigger("foo");
1664 equals( called, 1, "Verify that one foo occurred." );
1667 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1669 // Make sure we don't loose the target by DOM modifications
1670 // after the bubble already reached the liveHandler
1671 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1673 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1674 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1676 jQuery("#nothiddendiv span").click();
1677 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1678 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1681 jQuery("#body").undelegate("#nothiddendivchild", "click");
1683 // Verify that .live() ocurs and cancel buble in the same order as
1684 // we would expect .bind() and .click() without delegation
1685 var lived = 0, livee = 0;
1687 // bind one pair in one order
1688 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1689 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1691 jQuery('span#liveSpan1 a').click();
1692 equals( lived, 1, "Verify that only one first handler occurred." );
1693 equals( livee, 0, "Verify that second handler doesn't." );
1695 // and one pair in inverse
1696 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1697 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1701 jQuery('span#liveSpan2 a').click();
1702 equals( lived, 1, "Verify that only one first handler occurred." );
1703 equals( livee, 0, "Verify that second handler doesn't." );
1706 jQuery("#body").undelegate("click");
1708 // Test this, target and currentTarget are correct
1709 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1710 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1711 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1712 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1715 jQuery('span#liveSpan1 a').click();
1717 jQuery("#body").undelegate('span#liveSpan1', 'click');
1719 // Work with deep selectors
1722 function clickB(){ livee++; }
1724 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1725 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1726 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1728 equals( livee, 0, "No clicks, deep selector." );
1731 jQuery("#nothiddendivchild").trigger("click");
1732 equals( livee, 2, "Click, deep selector." );
1735 jQuery("#nothiddendivchild").trigger("mouseover");
1736 equals( livee, 1, "Mouseover, deep selector." );
1738 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1741 jQuery("#nothiddendivchild").trigger("click");
1742 equals( livee, 2, "Click, deep selector." );
1745 jQuery("#nothiddendivchild").trigger("mouseover");
1746 equals( livee, 0, "Mouseover, deep selector." );
1748 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1751 jQuery("#nothiddendivchild").trigger("click");
1752 equals( livee, 1, "Click, deep selector." );
1754 jQuery("#body").undelegate("#nothiddendiv div", "click");
1757 test("undelegate all bound events", function(){
1761 var div = jQuery("#body");
1763 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1766 jQuery("div#nothiddendivchild").trigger("click");
1767 jQuery("div#nothiddendivchild").trigger("submit");
1769 equals( count, 0, "Make sure no events were triggered." );
1772 test("delegate with multiple events", function(){
1776 var div = jQuery("#body");
1778 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1780 jQuery("div#nothiddendivchild").trigger("click");
1781 jQuery("div#nothiddendivchild").trigger("submit");
1783 equals( count, 2, "Make sure both the click and submit were triggered." );
1785 jQuery("#body").undelegate();
1788 test("delegate with change", function(){
1791 var selectChange = 0, checkboxChange = 0;
1793 var select = jQuery("select[name='S1']");
1794 jQuery("#body").delegate("select[name='S1']", "change", function() {
1798 var checkbox = jQuery("#check2"),
1799 checkboxFunction = function(){
1802 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1804 // test click on select
1806 // second click that changed it
1808 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1809 select.trigger("change");
1810 equals( selectChange, 1, "Change on click." );
1812 // test keys on select
1814 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1815 select.trigger("change");
1816 equals( selectChange, 1, "Change on keyup." );
1818 // test click on checkbox
1819 checkbox.trigger("change");
1820 equals( checkboxChange, 1, "Change on checkbox." );
1822 // test blur/focus on text
1823 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1824 jQuery("#body").delegate("#name", "change", function() {
1828 text.val(oldTextVal+"foo");
1829 text.trigger("change");
1830 equals( textChange, 1, "Change on text input." );
1832 text.val(oldTextVal);
1833 jQuery("#body").die("change");
1835 // test blur/focus on password
1836 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1837 jQuery("#body").delegate("#name", "change", function() {
1841 password.val(oldPasswordVal + "foo");
1842 password.trigger("change");
1843 equals( passwordChange, 1, "Change on password input." );
1845 password.val(oldPasswordVal);
1846 jQuery("#body").undelegate("#name", "change");
1848 // make sure die works
1852 jQuery("#body").undelegate("select[name='S1']", "change");
1853 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1854 select.trigger("change");
1855 equals( selectChange, 0, "Die on click works." );
1858 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1859 select.trigger("change");
1860 equals( selectChange, 0, "Die on keyup works." );
1862 // die specific checkbox
1863 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1864 checkbox.trigger("change");
1865 equals( checkboxChange, 1, "Die on checkbox." );
1868 test("delegate with submit", function() {
1869 var count1 = 0, count2 = 0;
1871 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1873 ev.preventDefault();
1876 jQuery(document).delegate("body", "submit", function(ev) {
1878 ev.preventDefault();
1881 jQuery("#testForm input[name=sub1]").submit();
1882 equals( count1, 1, "Verify form submit." );
1883 equals( count2, 1, "Verify body submit." );
1885 jQuery("#body").undelegate();
1886 jQuery(document).undelegate();
1889 test("Non DOM element events", function() {
1894 jQuery(o).bind('nonelementobj', function(e) {
1895 ok( true, "Event on non-DOM object triggered" );
1898 jQuery(o).trigger('nonelementobj');
1901 test("window resize", function() {
1904 jQuery(window).unbind();
1906 jQuery(window).bind("resize", function(){
1907 ok( true, "Resize event fired." );
1908 }).resize().unbind("resize");
1910 ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1913 test("focusin bubbles", function() {
1914 //create an input and focusin on it
1915 var input = jQuery("<input/>"), order = 0;
1917 input.prependTo("body");
1919 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1920 equals(1,order++,"focusin on the body second")
1923 input.bind("focusin.focusinBubblesTest",function(){
1924 equals(0,order++,"focusin on the element first")
1930 jQuery("body").unbind("focusin.focusinBubblesTest");
1934 test("jQuery(function($) {})", function() {
1936 jQuery(function($) {
1937 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");
1942 test("event properties", function() {
1944 jQuery("#simon1").click(function(event) {
1945 ok( event.timeStamp, "assert event.timeStamp is present" );