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", 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 var orig = e.originalEvent;
319 if ( typeof(orig.defaultPrevented) === "boolean" || typeof(orig.returnValue) === "boolean" || orig.getPreventDefault ) {
320 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
323 // Opera < 11 doesn't implement any interface we can use, so give it a pass
324 ok( true, "isDefaultPrevented not supported by this browser, test skipped" );
327 fakeClick( $anchor2 );
328 $anchor2.unbind( "click" );
329 $main.undelegate( "click" );
330 $anchor2.click(function(e) {
331 // Let the default action occur
333 $main.delegate("#foo", "click", function(e) {
334 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
336 fakeClick( $anchor2 );
337 $anchor2.unbind( "click" );
338 $main.undelegate( "click" );
341 test("bind(), iframes", function() {
342 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
343 var doc = jQuery("#loadediframe").contents();
345 jQuery("div", doc).bind("click", function() {
346 ok( true, "Binding to element inside iframe" );
347 }).click().unbind('click');
350 test("bind(), trigger change on select", function() {
353 function selectOnChange(event) {
354 equals( event.data, counter++, "Event.data is not a global event object" );
356 jQuery("#form select").each(function(i){
357 jQuery(this).bind('change', i, selectOnChange);
358 }).trigger('change');
361 test("bind(), namespaced events, cloned events", function() {
364 jQuery("#firstp").bind("custom.test",function(e){
365 ok(true, "Custom event triggered");
368 jQuery("#firstp").bind("click",function(e){
369 ok(true, "Normal click triggered");
372 jQuery("#firstp").bind("click.test",function(e){
373 ok(true, "Namespaced click triggered");
376 // Trigger both bound fn (2)
377 jQuery("#firstp").trigger("click");
379 // Trigger one bound fn (1)
380 jQuery("#firstp").trigger("click.test");
382 // Remove only the one fn
383 jQuery("#firstp").unbind("click.test");
385 // Trigger the remaining fn (1)
386 jQuery("#firstp").trigger("click");
388 // Remove the remaining fn
389 jQuery("#firstp").unbind(".test");
391 // Trigger the remaining fn (0)
392 jQuery("#firstp").trigger("custom");
394 // using contents will get comments regular, text, and comment nodes
395 jQuery("#nonnodes").contents().bind("tester", function () {
396 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
397 }).trigger("tester");
399 // Make sure events stick with appendTo'd elements (which are cloned) #2027
400 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
401 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
404 test("bind(), multi-namespaced events", function() {
416 function check(name, msg){
417 same(name, order.shift(), msg);
420 jQuery("#firstp").bind("custom.test",function(e){
421 check("custom.test", "Custom event triggered");
424 jQuery("#firstp").bind("custom.test2",function(e){
425 check("custom.test2", "Custom event triggered");
428 jQuery("#firstp").bind("click.test",function(e){
429 check("click.test", "Normal click triggered");
432 jQuery("#firstp").bind("click.test.abc",function(e){
433 check("click.test.abc", "Namespaced click triggered");
436 // Those would not trigger/unbind (#5303)
437 jQuery("#firstp").trigger("click.a.test");
438 jQuery("#firstp").unbind("click.a.test");
440 // Trigger both bound fn (1)
441 jQuery("#firstp").trigger("click.test.abc");
443 // Trigger one bound fn (1)
444 jQuery("#firstp").trigger("click.abc");
446 // Trigger two bound fn (2)
447 jQuery("#firstp").trigger("click.test");
449 // Remove only the one fn
450 jQuery("#firstp").unbind("click.abc");
452 // Trigger the remaining fn (1)
453 jQuery("#firstp").trigger("click");
455 // Remove the remaining fn
456 jQuery("#firstp").unbind(".test");
458 // Trigger the remaining fn (1)
459 jQuery("#firstp").trigger("custom");
462 test("bind(), with same function", function() {
465 var count = 0 , func = function(){
469 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
470 jQuery("#liveHandlerOrder").trigger("foo.bar");
472 equals(count, 1, "Verify binding function with multiple namespaces." );
474 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
475 jQuery("#liveHandlerOrder").trigger("foo.bar");
477 equals(count, 1, "Verify that removing events still work." );
480 test("bind(), make sure order is maintained", function() {
483 var elem = jQuery("#firstp"), log = [], check = [];
485 for ( var i = 0; i < 100; i++ ) (function(i){
486 elem.bind( "click", function(){
493 elem.trigger("click");
495 equals( log.join(","), check.join(","), "Make sure order was maintained." );
497 elem.unbind("click");
500 test("bind(), with different this object", function() {
502 var thisObject = { myThis: true },
503 data = { myData: true },
504 handler1 = function( event ) {
505 equals( this, thisObject, "bind() with different this object" );
507 handler2 = function( event ) {
508 equals( this, thisObject, "bind() with different this object and data" );
509 equals( event.data, data, "bind() with different this object and data" );
513 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
514 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
516 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
519 test("bind(name, false), unbind(name, false)", function() {
523 jQuery("#main").bind("click", function(e){ main++; });
524 jQuery("#ap").trigger("click");
525 equals( main, 1, "Verify that the trigger happened correctly." );
528 jQuery("#ap").bind("click", false);
529 jQuery("#ap").trigger("click");
530 equals( main, 0, "Verify that no bubble happened." );
533 jQuery("#ap").unbind("click", false);
534 jQuery("#ap").trigger("click");
535 equals( main, 1, "Verify that the trigger happened correctly." );
538 test("bind()/trigger()/unbind() on plain object", function() {
543 // Make sure it doesn't complain when no events are found
544 jQuery(obj).trigger("test");
546 // Make sure it doesn't complain when no events are found
547 jQuery(obj).unbind("test");
551 ok( true, "Custom event run." );
554 ok( true, "Custom submit event run." );
558 var events = jQuery(obj).data("__events__");
559 ok( events, "Object has events bound." );
560 equals( obj.events, undefined, "Events object on plain objects is not events" );
561 equals( typeof events, "function", "'events' expando is a function on plain objects." );
562 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
563 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
566 jQuery(obj).trigger("test");
567 jQuery(obj).trigger("submit");
569 jQuery(obj).unbind("test");
570 jQuery(obj).unbind("submit");
573 jQuery(obj).trigger("test");
575 // Make sure it doesn't complain when no events are found
576 jQuery(obj).unbind("test");
578 equals( obj.__events__, undefined, "Make sure events object is removed" );
581 test("unbind(type)", function() {
584 var $elem = jQuery("#firstp"),
588 ok( false, message );
591 message = "unbind passing function";
592 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
594 message = "unbind all from event";
595 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
597 message = "unbind all";
598 $elem.bind('error1', error).unbind().triggerHandler('error1');
600 message = "unbind many with function";
601 $elem.bind('error1 error2',error)
602 .unbind('error1 error2', error )
603 .trigger('error1').triggerHandler('error2');
605 message = "unbind many"; // #3538
606 $elem.bind('error1 error2',error)
607 .unbind('error1 error2')
608 .trigger('error1').triggerHandler('error2');
610 message = "unbind without a type or handler";
611 $elem.bind("error1 error2.test",error)
613 .trigger("error1").triggerHandler("error2");
616 test("unbind(eventObject)", function() {
619 var $elem = jQuery("#firstp"),
622 function assert( expected ){
624 $elem.trigger('foo').triggerHandler('bar');
625 equals( num, expected, "Check the right handlers are triggered" );
629 // This handler shouldn't be unbound
630 .bind('foo', function(){
633 .bind('foo', function(e){
638 .bind('bar', function(){
652 test("hover()", function() {
654 handler1 = function( event ) { ++times; },
655 handler2 = function( event ) { ++times; };
658 .hover(handler1, handler2)
659 .mouseenter().mouseleave()
660 .unbind("mouseenter", handler1)
661 .unbind("mouseleave", handler2)
663 .mouseenter().mouseleave()
664 .unbind("mouseenter mouseleave", handler1)
665 .mouseenter().mouseleave();
667 equals( times, 4, "hover handlers fired" );
670 test("trigger() shortcuts", function() {
672 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
673 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
674 equals( close.length, 0, "Context element does not exist, length must be zero" );
675 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
679 jQuery("#check1").click(function() {
680 ok( true, "click event handler for checkbox gets fired twice, see #815" );
684 jQuery('#firstp')[0].onclick = function(event) {
687 jQuery('#firstp').click();
688 equals( counter, 1, "Check that click, triggers onclick event handler also" );
690 var clickCounter = 0;
691 jQuery('#simon1')[0].onclick = function(event) {
694 jQuery('#simon1').click();
695 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
697 jQuery('<img />').load(function(){
698 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
702 test("trigger() bubbling", function() {
705 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
707 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
708 jQuery("html").bind("click", function(e){ html++; });
709 jQuery("body").bind("click", function(e){ body++; });
710 jQuery("#main").bind("click", function(e){ main++; });
711 jQuery("#ap").bind("click", function(){ ap++; return false; });
713 jQuery("html").trigger("click");
714 equals( doc, 1, "HTML bubble" );
715 equals( html, 1, "HTML bubble" );
717 jQuery("body").trigger("click");
718 equals( doc, 2, "Body bubble" );
719 equals( html, 2, "Body bubble" );
720 equals( body, 1, "Body bubble" );
722 jQuery("#main").trigger("click");
723 equals( doc, 3, "Main bubble" );
724 equals( html, 3, "Main bubble" );
725 equals( body, 2, "Main bubble" );
726 equals( main, 1, "Main bubble" );
728 jQuery("#ap").trigger("click");
729 equals( doc, 3, "ap bubble" );
730 equals( html, 3, "ap bubble" );
731 equals( body, 2, "ap bubble" );
732 equals( main, 1, "ap bubble" );
733 equals( ap, 1, "ap bubble" );
736 test("trigger(type, [data], [fn])", function() {
739 var handler = function(event, a, b, c) {
740 equals( event.type, "click", "check passed data" );
741 equals( a, 1, "check passed data" );
742 equals( b, "2", "check passed data" );
743 equals( c, "abc", "check passed data" );
747 var $elem = jQuery("#firstp");
749 // Simulate a "native" click
750 $elem[0].click = function(){
751 ok( true, "Native call was triggered" );
754 // Triggers handlrs and native
756 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
758 // Simulate a "native" click
759 $elem[0].click = function(){
760 ok( false, "Native call was triggered" );
763 // Trigger only the handlers (no native)
765 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
769 jQuery('#form input:first').hide().trigger('focus');
773 ok( pass, "Trigger focus on hidden element" );
777 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
781 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
783 var form = jQuery("<form action=''></form>").appendTo("body");
785 // Make sure it can be prevented locally
786 form.submit(function(){
787 ok( true, "Local bind still works." );
792 form.trigger("submit");
794 form.unbind("submit");
796 jQuery(document).submit(function(){
797 ok( true, "Make sure bubble works up to document." );
802 form.trigger("submit");
804 jQuery(document).unbind("submit");
809 test("jQuery.Event.currentTarget", function(){
812 test("trigger(eventObject, [data], [fn])", function() {
815 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
816 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
818 var event = jQuery.Event("noNew");
819 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
820 equals( event.type, "noNew", "Verify its type" );
822 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
823 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
824 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
826 event.preventDefault();
827 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
828 event.stopPropagation();
829 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
831 event.isPropagationStopped = function(){ return false };
832 event.stopImmediatePropagation();
833 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
834 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
836 $parent.bind('foo',function(e){
838 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
839 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
840 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
841 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
844 // test with an event object
845 event = new jQuery.Event("foo");
846 event.secret = 'boo!';
847 $child.trigger(event);
849 // test with a literal object
850 $child.trigger({type:'foo', secret:'boo!'});
855 ok( false, "This assertion shouldn't be reached");
858 $parent.bind('foo', error );
860 $child.bind('foo',function(e, a, b, c ){
861 equals( arguments.length, 4, "Check arguments length");
862 equals( a, 1, "Check first custom argument");
863 equals( b, 2, "Check second custom argument");
864 equals( c, 3, "Check third custom argument");
866 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
867 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
868 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
871 e.stopImmediatePropagation();
876 // We should add this back in when we want to test the order
877 // in which event handlers are iterated.
878 //$child.bind('foo', error );
880 event = new jQuery.Event("foo");
881 $child.trigger( event, [1,2,3] ).unbind();
882 equals( event.result, "result", "Check event.result attribute");
884 // Will error if it bubbles
885 $child.triggerHandler('foo');
888 $parent.unbind().remove();
891 test("jQuery.Event.currentTarget", function(){
895 $elem = jQuery('<button>a</button>').click(function(e){
896 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
900 $elem.trigger('click');
906 test("toggle(Function, Function, ...)", function() {
910 fn1 = function(e) { count++; },
911 fn2 = function(e) { count--; },
912 preventDefault = function(e) { e.preventDefault() },
913 link = jQuery('#mark');
914 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
915 equals( count, 1, "Check for toggle(fn, fn)" );
917 jQuery("#firstp").toggle(function () {
918 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
919 }, function() {}).trigger("click", [ 1, 2, 3 ]);
922 jQuery("#simon1").one("click", function() {
923 ok( true, "Execute event only once" );
924 jQuery(this).toggle(function() {
925 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
927 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
930 }).click().click().click();
945 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
947 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
949 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
951 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
953 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
955 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
957 $div.unbind('click',fns[0]);
958 var data = jQuery.data( $div[0], 'events' );
959 ok( !data, "Unbinding one function from toggle unbinds them all");
961 // Test Multi-Toggles
963 $div = jQuery("<div/>");
964 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
966 same( a, [1], "Check that a click worked." );
968 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
970 same( a, [1,2], "Check that a click worked with a second toggle." );
971 same( b, [1], "Check that a click worked with a second toggle." );
974 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
975 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
978 test(".live()/.die()", function() {
981 var submit = 0, div = 0, livea = 0, liveb = 0;
983 jQuery("div").live("submit", function(){ submit++; return false; });
984 jQuery("div").live("click", function(){ div++; });
985 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
986 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
988 // Nothing should trigger on the body
989 jQuery("body").trigger("click");
990 equals( submit, 0, "Click on body" );
991 equals( div, 0, "Click on body" );
992 equals( livea, 0, "Click on body" );
993 equals( liveb, 0, "Click on body" );
995 // This should trigger two events
996 submit = 0, div = 0, livea = 0, liveb = 0;
997 jQuery("div#nothiddendiv").trigger("click");
998 equals( submit, 0, "Click on div" );
999 equals( div, 1, "Click on div" );
1000 equals( livea, 1, "Click on div" );
1001 equals( liveb, 0, "Click on div" );
1003 // This should trigger three events (w/ bubbling)
1004 submit = 0, div = 0, livea = 0, liveb = 0;
1005 jQuery("div#nothiddendivchild").trigger("click");
1006 equals( submit, 0, "Click on inner div" );
1007 equals( div, 2, "Click on inner div" );
1008 equals( livea, 1, "Click on inner div" );
1009 equals( liveb, 1, "Click on inner div" );
1011 // This should trigger one submit
1012 submit = 0, div = 0, livea = 0, liveb = 0;
1013 jQuery("div#nothiddendivchild").trigger("submit");
1014 equals( submit, 1, "Submit on div" );
1015 equals( div, 0, "Submit on div" );
1016 equals( livea, 0, "Submit on div" );
1017 equals( liveb, 0, "Submit on div" );
1019 // Make sure no other events were removed in the process
1020 submit = 0, div = 0, livea = 0, liveb = 0;
1021 jQuery("div#nothiddendivchild").trigger("click");
1022 equals( submit, 0, "die Click on inner div" );
1023 equals( div, 2, "die Click on inner div" );
1024 equals( livea, 1, "die Click on inner div" );
1025 equals( liveb, 1, "die Click on inner div" );
1027 // Now make sure that the removal works
1028 submit = 0, div = 0, livea = 0, liveb = 0;
1029 jQuery("div#nothiddendivchild").die("click");
1030 jQuery("div#nothiddendivchild").trigger("click");
1031 equals( submit, 0, "die Click on inner div" );
1032 equals( div, 2, "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 the click wasn't removed too early
1037 submit = 0, div = 0, livea = 0, liveb = 0;
1038 jQuery("div#nothiddendiv").trigger("click");
1039 equals( submit, 0, "die Click on inner div" );
1040 equals( div, 1, "die Click on inner div" );
1041 equals( livea, 1, "die Click on inner div" );
1042 equals( liveb, 0, "die Click on inner div" );
1044 // Make sure that stopPropgation doesn't stop live events
1045 submit = 0, div = 0, livea = 0, liveb = 0;
1046 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1047 jQuery("div#nothiddendivchild").trigger("click");
1048 equals( submit, 0, "stopPropagation Click on inner div" );
1049 equals( div, 1, "stopPropagation Click on inner div" );
1050 equals( livea, 0, "stopPropagation Click on inner div" );
1051 equals( liveb, 1, "stopPropagation Click on inner div" );
1053 // Make sure click events only fire with primary click
1054 submit = 0, div = 0, livea = 0, liveb = 0;
1055 var event = jQuery.Event("click");
1057 jQuery("div#nothiddendiv").trigger(event);
1059 equals( livea, 0, "live secondary click" );
1061 jQuery("div#nothiddendivchild").die("click");
1062 jQuery("div#nothiddendiv").die("click");
1063 jQuery("div").die("click");
1064 jQuery("div").die("submit");
1066 // Test binding with a different context
1067 var clicked = 0, container = jQuery('#main')[0];
1068 jQuery("#foo", container).live("click", function(e){ clicked++; });
1069 jQuery("div").trigger('click');
1070 jQuery("#foo").trigger('click');
1071 jQuery("#main").trigger('click');
1072 jQuery("body").trigger('click');
1073 equals( clicked, 2, "live with a context" );
1075 // Make sure the event is actually stored on the context
1076 ok( jQuery.data(container, "events").live, "live with a context" );
1078 // Test unbinding with a different context
1079 jQuery("#foo", container).die("click");
1080 jQuery("#foo").trigger('click');
1081 equals( clicked, 2, "die with a context");
1083 // Test binding with event data
1084 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1085 jQuery("#foo").trigger("click").die("click");
1087 // Test binding with trigger data
1088 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1089 jQuery("#foo").trigger("click", true).die("click");
1091 // Test binding with different this object
1092 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1093 jQuery("#foo").trigger("click").die("click");
1095 // Test binding with different this object, event data, and trigger data
1096 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1097 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1098 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1099 equals( data, true, "live with with different this object, event data, and trigger data")
1100 }, { foo: "bar" }));
1101 jQuery("#foo").trigger("click", true).die("click");
1103 // Verify that return false prevents default action
1104 jQuery("#anchor2").live("click", function(){ return false; });
1105 var hash = window.location.hash;
1106 jQuery("#anchor2").trigger("click");
1107 equals( window.location.hash, hash, "return false worked" );
1108 jQuery("#anchor2").die("click");
1110 // Verify that .preventDefault() prevents default action
1111 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1112 var hash = window.location.hash;
1113 jQuery("#anchor2").trigger("click");
1114 equals( window.location.hash, hash, "e.preventDefault() worked" );
1115 jQuery("#anchor2").die("click");
1117 // Test binding the same handler to multiple points
1119 function callback(){ called++; return false; }
1121 jQuery("#nothiddendiv").live("click", callback);
1122 jQuery("#anchor2").live("click", callback);
1124 jQuery("#nothiddendiv").trigger("click");
1125 equals( called, 1, "Verify that only one click occurred." );
1128 jQuery("#anchor2").trigger("click");
1129 equals( called, 1, "Verify that only one click occurred." );
1131 // Make sure that only one callback is removed
1132 jQuery("#anchor2").die("click", callback);
1135 jQuery("#nothiddendiv").trigger("click");
1136 equals( called, 1, "Verify that only one click occurred." );
1139 jQuery("#anchor2").trigger("click");
1140 equals( called, 0, "Verify that no click occurred." );
1142 // Make sure that it still works if the selector is the same,
1143 // but the event type is different
1144 jQuery("#nothiddendiv").live("foo", callback);
1147 jQuery("#nothiddendiv").die("click", callback);
1150 jQuery("#nothiddendiv").trigger("click");
1151 equals( called, 0, "Verify that no click occurred." );
1154 jQuery("#nothiddendiv").trigger("foo");
1155 equals( called, 1, "Verify that one foo occurred." );
1158 jQuery("#nothiddendiv").die("foo", callback);
1160 // Make sure we don't loose the target by DOM modifications
1161 // after the bubble already reached the liveHandler
1162 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1164 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1165 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1167 jQuery("#nothiddendiv span").click();
1168 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1169 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1172 jQuery("#nothiddendivchild").die("click");
1174 // Verify that .live() ocurs and cancel buble in the same order as
1175 // we would expect .bind() and .click() without delegation
1176 var lived = 0, livee = 0;
1178 // bind one pair in one order
1179 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1180 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1182 jQuery('span#liveSpan1 a').click();
1183 equals( lived, 1, "Verify that only one first handler occurred." );
1184 equals( livee, 0, "Verify that second handler doesn't." );
1186 // and one pair in inverse
1187 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1188 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1192 jQuery('span#liveSpan2 a').click();
1193 equals( lived, 1, "Verify that only one first handler occurred." );
1194 equals( livee, 0, "Verify that second handler doesn't." );
1197 jQuery("span#liveSpan1 a").die("click")
1198 jQuery("span#liveSpan1").die("click");
1199 jQuery("span#liveSpan2 a").die("click");
1200 jQuery("span#liveSpan2").die("click");
1202 // Test this, target and currentTarget are correct
1203 jQuery('span#liveSpan1').live('click', function(e){
1204 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1205 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1206 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1209 jQuery('span#liveSpan1 a').click();
1211 jQuery('span#liveSpan1').die('click');
1213 // Work with deep selectors
1216 function clickB(){ livee++; }
1218 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1219 jQuery("#nothiddendiv div").live("click", clickB);
1220 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1222 equals( livee, 0, "No clicks, deep selector." );
1225 jQuery("#nothiddendivchild").trigger("click");
1226 equals( livee, 2, "Click, deep selector." );
1229 jQuery("#nothiddendivchild").trigger("mouseover");
1230 equals( livee, 1, "Mouseover, deep selector." );
1232 jQuery("#nothiddendiv div").die("mouseover");
1235 jQuery("#nothiddendivchild").trigger("click");
1236 equals( livee, 2, "Click, deep selector." );
1239 jQuery("#nothiddendivchild").trigger("mouseover");
1240 equals( livee, 0, "Mouseover, deep selector." );
1242 jQuery("#nothiddendiv div").die("click", clickB);
1245 jQuery("#nothiddendivchild").trigger("click");
1246 equals( livee, 1, "Click, deep selector." );
1248 jQuery("#nothiddendiv div").die("click");
1250 jQuery("#nothiddendiv div").live("blur", function(){
1251 ok( true, "Live div trigger blur." );
1254 jQuery("#nothiddendiv div").trigger("blur");
1256 jQuery("#nothiddendiv div").die("blur");
1259 test("die all bound events", function(){
1263 var div = jQuery("div#nothiddendivchild");
1265 div.live("click submit", function(){ count++; });
1268 div.trigger("click");
1269 div.trigger("submit");
1271 equals( count, 0, "Make sure no events were triggered." );
1274 test("live with multiple events", function(){
1278 var div = jQuery("div#nothiddendivchild");
1280 div.live("click submit", function(){ count++; });
1282 div.trigger("click");
1283 div.trigger("submit");
1285 equals( count, 2, "Make sure both the click and submit were triggered." );
1288 test("live with namespaces", function(){
1291 var count1 = 0, count2 = 0;
1293 jQuery("#liveSpan1").live("foo.bar", function(e){
1297 jQuery("#liveSpan1").live("foo.zed", function(e){
1301 jQuery("#liveSpan1").trigger("foo.bar");
1302 equals( count1, 1, "Got live foo.bar" );
1303 equals( count2, 0, "Got live foo.bar" );
1305 count1 = 0, count2 = 0;
1307 jQuery("#liveSpan1").trigger("foo.zed");
1308 equals( count1, 0, "Got live foo.zed" );
1309 equals( count2, 1, "Got live foo.zed" );
1312 count1 = 0, count2 = 0;
1314 jQuery("#liveSpan1").die("foo.zed");
1315 jQuery("#liveSpan1").trigger("foo.bar");
1317 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1318 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1320 count1 = 0, count2 = 0;
1322 jQuery("#liveSpan1").trigger("foo.zed");
1323 equals( count1, 0, "Got live foo.zed" );
1324 equals( count2, 0, "Got live foo.zed" );
1327 jQuery("#liveSpan1").die("foo.bar");
1329 count1 = 0, count2 = 0;
1331 jQuery("#liveSpan1").trigger("foo.bar");
1332 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1333 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1335 jQuery("#liveSpan1").trigger("foo.zed");
1336 equals( count1, 0, "Did not trigger foo.zed again" );
1337 equals( count2, 0, "Did not trigger foo.zed again" );
1340 test("live with change", function(){
1343 var selectChange = 0, checkboxChange = 0;
1345 var select = jQuery("select[name='S1']")
1346 select.live("change", function() {
1350 var checkbox = jQuery("#check2"),
1351 checkboxFunction = function(){
1354 checkbox.live("change", checkboxFunction);
1356 // test click on select
1358 // second click that changed it
1360 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1361 select.trigger("change");
1362 equals( selectChange, 1, "Change on click." );
1364 // test keys on select
1366 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1367 select.trigger("change");
1368 equals( selectChange, 1, "Change on keyup." );
1370 // test click on checkbox
1371 checkbox.trigger("change");
1372 equals( checkboxChange, 1, "Change on checkbox." );
1374 // test blur/focus on text
1375 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1376 text.live("change", function() {
1380 text.val(oldTextVal+"foo");
1381 text.trigger("change");
1382 equals( textChange, 1, "Change on text input." );
1384 text.val(oldTextVal);
1387 // test blur/focus on password
1388 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1389 password.live("change", function() {
1393 password.val(oldPasswordVal + "foo");
1394 password.trigger("change");
1395 equals( passwordChange, 1, "Change on password input." );
1397 password.val(oldPasswordVal);
1398 password.die("change");
1400 // make sure die works
1404 select.die("change");
1405 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1406 select.trigger("change");
1407 equals( selectChange, 0, "Die on click works." );
1410 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1411 select.trigger("change");
1412 equals( selectChange, 0, "Die on keyup works." );
1414 // die specific checkbox
1415 checkbox.die("change", checkboxFunction);
1416 checkbox.trigger("change");
1417 equals( checkboxChange, 1, "Die on checkbox." );
1420 test("live with submit", function() {
1421 var count1 = 0, count2 = 0;
1423 jQuery("#testForm").live("submit", function(ev) {
1425 ev.preventDefault();
1428 jQuery("body").live("submit", function(ev) {
1430 ev.preventDefault();
1433 jQuery("#testForm input[name=sub1]").submit();
1434 equals( count1, 1, "Verify form submit." );
1435 equals( count2, 1, "Verify body submit." );
1437 jQuery("#testForm").die("submit");
1438 jQuery("body").die("submit");
1441 test("live with special events", function() {
1444 jQuery.event.special.foo = {
1445 setup: function( data, namespaces, handler ) {
1446 ok( true, "Setup run." );
1448 teardown: function( namespaces ) {
1449 ok( true, "Teardown run." );
1451 add: function( handleObj ) {
1452 ok( true, "Add run." );
1454 remove: function( handleObj ) {
1455 ok( true, "Remove run." );
1457 _default: function( event ) {
1458 ok( true, "Default run." );
1463 jQuery("#liveSpan1").live("foo.a", function(e){
1464 ok( true, "Handler 1 run." );
1468 jQuery("#liveSpan1").live("foo.b", function(e){
1469 ok( true, "Handler 2 run." );
1472 // Run: Handler 1, Handler 2, Default
1473 jQuery("#liveSpan1").trigger("foo");
1475 // Run: Handler 1, Default
1476 // TODO: Namespace doesn't trigger default (?)
1477 jQuery("#liveSpan1").trigger("foo.a");
1480 jQuery("#liveSpan1").die("foo.a");
1482 // Run: Handler 2, Default
1483 jQuery("#liveSpan1").trigger("foo");
1485 // Run: remove, teardown
1486 jQuery("#liveSpan1").die("foo");
1488 delete jQuery.event.special.foo;
1491 test(".delegate()/.undelegate()", function() {
1494 var submit = 0, div = 0, livea = 0, liveb = 0;
1496 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1497 jQuery("#body").delegate("div", "click", function(){ div++; });
1498 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1499 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1501 // Nothing should trigger on the body
1502 jQuery("body").trigger("click");
1503 equals( submit, 0, "Click on body" );
1504 equals( div, 0, "Click on body" );
1505 equals( livea, 0, "Click on body" );
1506 equals( liveb, 0, "Click on body" );
1508 // This should trigger two events
1509 submit = 0, div = 0, livea = 0, liveb = 0;
1510 jQuery("div#nothiddendiv").trigger("click");
1511 equals( submit, 0, "Click on div" );
1512 equals( div, 1, "Click on div" );
1513 equals( livea, 1, "Click on div" );
1514 equals( liveb, 0, "Click on div" );
1516 // This should trigger three events (w/ bubbling)
1517 submit = 0, div = 0, livea = 0, liveb = 0;
1518 jQuery("div#nothiddendivchild").trigger("click");
1519 equals( submit, 0, "Click on inner div" );
1520 equals( div, 2, "Click on inner div" );
1521 equals( livea, 1, "Click on inner div" );
1522 equals( liveb, 1, "Click on inner div" );
1524 // This should trigger one submit
1525 submit = 0, div = 0, livea = 0, liveb = 0;
1526 jQuery("div#nothiddendivchild").trigger("submit");
1527 equals( submit, 1, "Submit on div" );
1528 equals( div, 0, "Submit on div" );
1529 equals( livea, 0, "Submit on div" );
1530 equals( liveb, 0, "Submit on div" );
1532 // Make sure no other events were removed in the process
1533 submit = 0, div = 0, livea = 0, liveb = 0;
1534 jQuery("div#nothiddendivchild").trigger("click");
1535 equals( submit, 0, "undelegate Click on inner div" );
1536 equals( div, 2, "undelegate Click on inner div" );
1537 equals( livea, 1, "undelegate Click on inner div" );
1538 equals( liveb, 1, "undelegate Click on inner div" );
1540 // Now make sure that the removal works
1541 submit = 0, div = 0, livea = 0, liveb = 0;
1542 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1543 jQuery("div#nothiddendivchild").trigger("click");
1544 equals( submit, 0, "undelegate Click on inner div" );
1545 equals( div, 2, "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 the click wasn't removed too early
1550 submit = 0, div = 0, livea = 0, liveb = 0;
1551 jQuery("div#nothiddendiv").trigger("click");
1552 equals( submit, 0, "undelegate Click on inner div" );
1553 equals( div, 1, "undelegate Click on inner div" );
1554 equals( livea, 1, "undelegate Click on inner div" );
1555 equals( liveb, 0, "undelegate Click on inner div" );
1557 // Make sure that stopPropgation doesn't stop live events
1558 submit = 0, div = 0, livea = 0, liveb = 0;
1559 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1560 jQuery("div#nothiddendivchild").trigger("click");
1561 equals( submit, 0, "stopPropagation Click on inner div" );
1562 equals( div, 1, "stopPropagation Click on inner div" );
1563 equals( livea, 0, "stopPropagation Click on inner div" );
1564 equals( liveb, 1, "stopPropagation Click on inner div" );
1566 // Make sure click events only fire with primary click
1567 submit = 0, div = 0, livea = 0, liveb = 0;
1568 var event = jQuery.Event("click");
1570 jQuery("div#nothiddendiv").trigger(event);
1572 equals( livea, 0, "delegate secondary click" );
1574 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1575 jQuery("#body").undelegate("div#nothiddendiv", "click");
1576 jQuery("#body").undelegate("div", "click");
1577 jQuery("#body").undelegate("div", "submit");
1579 // Test binding with a different context
1580 var clicked = 0, container = jQuery('#main')[0];
1581 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1582 jQuery("div").trigger('click');
1583 jQuery("#foo").trigger('click');
1584 jQuery("#main").trigger('click');
1585 jQuery("body").trigger('click');
1586 equals( clicked, 2, "delegate with a context" );
1588 // Make sure the event is actually stored on the context
1589 ok( jQuery.data(container, "events").live, "delegate with a context" );
1591 // Test unbinding with a different context
1592 jQuery("#main").undelegate("#foo", "click");
1593 jQuery("#foo").trigger('click');
1594 equals( clicked, 2, "undelegate with a context");
1596 // Test binding with event data
1597 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1598 jQuery("#foo").trigger("click");
1599 jQuery("#body").undelegate("#foo", "click");
1601 // Test binding with trigger data
1602 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1603 jQuery("#foo").trigger("click", true);
1604 jQuery("#body").undelegate("#foo", "click");
1606 // Test binding with different this object
1607 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1608 jQuery("#foo").trigger("click");
1609 jQuery("#body").undelegate("#foo", "click");
1611 // Test binding with different this object, event data, and trigger data
1612 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1613 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1614 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1615 equals( data, true, "delegate with with different this object, event data, and trigger data")
1616 }, { foo: "bar" }));
1617 jQuery("#foo").trigger("click", true);
1618 jQuery("#body").undelegate("#foo", "click");
1620 // Verify that return false prevents default action
1621 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1622 var hash = window.location.hash;
1623 jQuery("#anchor2").trigger("click");
1624 equals( window.location.hash, hash, "return false worked" );
1625 jQuery("#body").undelegate("#anchor2", "click");
1627 // Verify that .preventDefault() prevents default action
1628 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1629 var hash = window.location.hash;
1630 jQuery("#anchor2").trigger("click");
1631 equals( window.location.hash, hash, "e.preventDefault() worked" );
1632 jQuery("#body").undelegate("#anchor2", "click");
1634 // Test binding the same handler to multiple points
1636 function callback(){ called++; return false; }
1638 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1639 jQuery("#body").delegate("#anchor2", "click", callback);
1641 jQuery("#nothiddendiv").trigger("click");
1642 equals( called, 1, "Verify that only one click occurred." );
1645 jQuery("#anchor2").trigger("click");
1646 equals( called, 1, "Verify that only one click occurred." );
1648 // Make sure that only one callback is removed
1649 jQuery("#body").undelegate("#anchor2", "click", callback);
1652 jQuery("#nothiddendiv").trigger("click");
1653 equals( called, 1, "Verify that only one click occurred." );
1656 jQuery("#anchor2").trigger("click");
1657 equals( called, 0, "Verify that no click occurred." );
1659 // Make sure that it still works if the selector is the same,
1660 // but the event type is different
1661 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1664 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1667 jQuery("#nothiddendiv").trigger("click");
1668 equals( called, 0, "Verify that no click occurred." );
1671 jQuery("#nothiddendiv").trigger("foo");
1672 equals( called, 1, "Verify that one foo occurred." );
1675 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1677 // Make sure we don't loose the target by DOM modifications
1678 // after the bubble already reached the liveHandler
1679 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1681 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1682 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1684 jQuery("#nothiddendiv span").click();
1685 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1686 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1689 jQuery("#body").undelegate("#nothiddendivchild", "click");
1691 // Verify that .live() ocurs and cancel buble in the same order as
1692 // we would expect .bind() and .click() without delegation
1693 var lived = 0, livee = 0;
1695 // bind one pair in one order
1696 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1697 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1699 jQuery('span#liveSpan1 a').click();
1700 equals( lived, 1, "Verify that only one first handler occurred." );
1701 equals( livee, 0, "Verify that second handler doesn't." );
1703 // and one pair in inverse
1704 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1705 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1709 jQuery('span#liveSpan2 a').click();
1710 equals( lived, 1, "Verify that only one first handler occurred." );
1711 equals( livee, 0, "Verify that second handler doesn't." );
1714 jQuery("#body").undelegate("click");
1716 // Test this, target and currentTarget are correct
1717 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1718 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1719 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1720 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1723 jQuery('span#liveSpan1 a').click();
1725 jQuery("#body").undelegate('span#liveSpan1', 'click');
1727 // Work with deep selectors
1730 function clickB(){ livee++; }
1732 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1733 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1734 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1736 equals( livee, 0, "No clicks, deep selector." );
1739 jQuery("#nothiddendivchild").trigger("click");
1740 equals( livee, 2, "Click, deep selector." );
1743 jQuery("#nothiddendivchild").trigger("mouseover");
1744 equals( livee, 1, "Mouseover, deep selector." );
1746 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1749 jQuery("#nothiddendivchild").trigger("click");
1750 equals( livee, 2, "Click, deep selector." );
1753 jQuery("#nothiddendivchild").trigger("mouseover");
1754 equals( livee, 0, "Mouseover, deep selector." );
1756 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1759 jQuery("#nothiddendivchild").trigger("click");
1760 equals( livee, 1, "Click, deep selector." );
1762 jQuery("#body").undelegate("#nothiddendiv div", "click");
1765 test("undelegate all bound events", function(){
1769 var div = jQuery("#body");
1771 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1774 jQuery("div#nothiddendivchild").trigger("click");
1775 jQuery("div#nothiddendivchild").trigger("submit");
1777 equals( count, 0, "Make sure no events were triggered." );
1780 test("delegate with multiple events", function(){
1784 var div = jQuery("#body");
1786 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1788 jQuery("div#nothiddendivchild").trigger("click");
1789 jQuery("div#nothiddendivchild").trigger("submit");
1791 equals( count, 2, "Make sure both the click and submit were triggered." );
1793 jQuery("#body").undelegate();
1796 test("delegate with change", function(){
1799 var selectChange = 0, checkboxChange = 0;
1801 var select = jQuery("select[name='S1']");
1802 jQuery("#body").delegate("select[name='S1']", "change", function() {
1806 var checkbox = jQuery("#check2"),
1807 checkboxFunction = function(){
1810 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1812 // test click on select
1814 // second click that changed it
1816 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1817 select.trigger("change");
1818 equals( selectChange, 1, "Change on click." );
1820 // test keys on select
1822 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1823 select.trigger("change");
1824 equals( selectChange, 1, "Change on keyup." );
1826 // test click on checkbox
1827 checkbox.trigger("change");
1828 equals( checkboxChange, 1, "Change on checkbox." );
1830 // test blur/focus on text
1831 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1832 jQuery("#body").delegate("#name", "change", function() {
1836 text.val(oldTextVal+"foo");
1837 text.trigger("change");
1838 equals( textChange, 1, "Change on text input." );
1840 text.val(oldTextVal);
1841 jQuery("#body").die("change");
1843 // test blur/focus on password
1844 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1845 jQuery("#body").delegate("#name", "change", function() {
1849 password.val(oldPasswordVal + "foo");
1850 password.trigger("change");
1851 equals( passwordChange, 1, "Change on password input." );
1853 password.val(oldPasswordVal);
1854 jQuery("#body").undelegate("#name", "change");
1856 // make sure die works
1860 jQuery("#body").undelegate("select[name='S1']", "change");
1861 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1862 select.trigger("change");
1863 equals( selectChange, 0, "Die on click works." );
1866 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1867 select.trigger("change");
1868 equals( selectChange, 0, "Die on keyup works." );
1870 // die specific checkbox
1871 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1872 checkbox.trigger("change");
1873 equals( checkboxChange, 1, "Die on checkbox." );
1876 test("delegate with submit", function() {
1877 var count1 = 0, count2 = 0;
1879 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1881 ev.preventDefault();
1884 jQuery(document).delegate("body", "submit", function(ev) {
1886 ev.preventDefault();
1889 jQuery("#testForm input[name=sub1]").submit();
1890 equals( count1, 1, "Verify form submit." );
1891 equals( count2, 1, "Verify body submit." );
1893 jQuery("#body").undelegate();
1894 jQuery(document).undelegate();
1897 test("Non DOM element events", function() {
1902 jQuery(o).bind('nonelementobj', function(e) {
1903 ok( true, "Event on non-DOM object triggered" );
1906 jQuery(o).trigger('nonelementobj');
1909 test("window resize", function() {
1912 jQuery(window).unbind();
1914 jQuery(window).bind("resize", function(){
1915 ok( true, "Resize event fired." );
1916 }).resize().unbind("resize");
1918 ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1921 test("focusin bubbles", function() {
1922 //create an input and focusin on it
1923 var input = jQuery("<input/>"), order = 0;
1925 input.prependTo("body");
1927 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1928 equals(1,order++,"focusin on the body second")
1931 input.bind("focusin.focusinBubblesTest",function(){
1932 equals(0,order++,"focusin on the element first")
1938 jQuery("body").unbind("focusin.focusinBubblesTest");
1942 test("jQuery(function($) {})", function() {
1944 jQuery(function($) {
1945 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");
1950 test("event properties", function() {
1952 jQuery("#simon1").click(function(event) {
1953 ok( event.timeStamp, "assert event.timeStamp is present" );