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("bind(), iframes", function() {
269 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
270 var doc = jQuery("#loadediframe").contents();
272 jQuery("div", doc).bind("click", function() {
273 ok( true, "Binding to element inside iframe" );
274 }).click().unbind('click');
277 test("bind(), trigger change on select", function() {
280 function selectOnChange(event) {
281 equals( event.data, counter++, "Event.data is not a global event object" );
283 jQuery("#form select").each(function(i){
284 jQuery(this).bind('change', i, selectOnChange);
285 }).trigger('change');
288 test("bind(), namespaced events, cloned events", function() {
291 jQuery("#firstp").bind("custom.test",function(e){
292 ok(true, "Custom event triggered");
295 jQuery("#firstp").bind("click",function(e){
296 ok(true, "Normal click triggered");
299 jQuery("#firstp").bind("click.test",function(e){
300 ok(true, "Namespaced click triggered");
303 // Trigger both bound fn (2)
304 jQuery("#firstp").trigger("click");
306 // Trigger one bound fn (1)
307 jQuery("#firstp").trigger("click.test");
309 // Remove only the one fn
310 jQuery("#firstp").unbind("click.test");
312 // Trigger the remaining fn (1)
313 jQuery("#firstp").trigger("click");
315 // Remove the remaining fn
316 jQuery("#firstp").unbind(".test");
318 // Trigger the remaining fn (0)
319 jQuery("#firstp").trigger("custom");
321 // using contents will get comments regular, text, and comment nodes
322 jQuery("#nonnodes").contents().bind("tester", function () {
323 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
324 }).trigger("tester");
326 // Make sure events stick with appendTo'd elements (which are cloned) #2027
327 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
328 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
331 test("bind(), multi-namespaced events", function() {
343 function check(name, msg){
344 same(name, order.shift(), msg);
347 jQuery("#firstp").bind("custom.test",function(e){
348 check("custom.test", "Custom event triggered");
351 jQuery("#firstp").bind("custom.test2",function(e){
352 check("custom.test2", "Custom event triggered");
355 jQuery("#firstp").bind("click.test",function(e){
356 check("click.test", "Normal click triggered");
359 jQuery("#firstp").bind("click.test.abc",function(e){
360 check("click.test.abc", "Namespaced click triggered");
363 // Those would not trigger/unbind (#5303)
364 jQuery("#firstp").trigger("click.a.test");
365 jQuery("#firstp").unbind("click.a.test");
367 // Trigger both bound fn (1)
368 jQuery("#firstp").trigger("click.test.abc");
370 // Trigger one bound fn (1)
371 jQuery("#firstp").trigger("click.abc");
373 // Trigger two bound fn (2)
374 jQuery("#firstp").trigger("click.test");
376 // Remove only the one fn
377 jQuery("#firstp").unbind("click.abc");
379 // Trigger the remaining fn (1)
380 jQuery("#firstp").trigger("click");
382 // Remove the remaining fn
383 jQuery("#firstp").unbind(".test");
385 // Trigger the remaining fn (1)
386 jQuery("#firstp").trigger("custom");
389 test("bind(), with same function", function() {
392 var count = 0 , func = function(){
396 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
397 jQuery("#liveHandlerOrder").trigger("foo.bar");
399 equals(count, 1, "Verify binding function with multiple namespaces." );
401 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
402 jQuery("#liveHandlerOrder").trigger("foo.bar");
404 equals(count, 1, "Verify that removing events still work." );
407 test("bind(), make sure order is maintained", function() {
410 var elem = jQuery("#firstp"), log = [], check = [];
412 for ( var i = 0; i < 100; i++ ) (function(i){
413 elem.bind( "click", function(){
420 elem.trigger("click");
422 equals( log.join(","), check.join(","), "Make sure order was maintained." );
424 elem.unbind("click");
427 test("bind(), with different this object", function() {
429 var thisObject = { myThis: true },
430 data = { myData: true },
431 handler1 = function( event ) {
432 equals( this, thisObject, "bind() with different this object" );
434 handler2 = function( event ) {
435 equals( this, thisObject, "bind() with different this object and data" );
436 equals( event.data, data, "bind() with different this object and data" );
440 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
441 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
443 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
446 test("bind(name, false), unbind(name, false)", function() {
450 jQuery("#main").bind("click", function(e){ main++; });
451 jQuery("#ap").trigger("click");
452 equals( main, 1, "Verify that the trigger happened correctly." );
455 jQuery("#ap").bind("click", false);
456 jQuery("#ap").trigger("click");
457 equals( main, 0, "Verify that no bubble happened." );
460 jQuery("#ap").unbind("click", false);
461 jQuery("#ap").trigger("click");
462 equals( main, 1, "Verify that the trigger happened correctly." );
465 test("bind()/trigger()/unbind() on plain object", function() {
470 // Make sure it doesn't complain when no events are found
471 jQuery(obj).trigger("test");
473 // Make sure it doesn't complain when no events are found
474 jQuery(obj).unbind("test");
476 jQuery(obj).bind("test", function(){
477 ok( true, "Custom event run." );
480 var events = jQuery(obj).data("__events__");
481 ok( events, "Object has events bound." );
482 equals( obj.events, undefined, "Events object on plain objects is not events" );
483 equals( typeof events, "function", "'events' expando is a function on plain objects." );
484 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
485 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
488 jQuery(obj).trigger("test");
490 jQuery(obj).unbind("test");
493 jQuery(obj).trigger("test");
495 // Make sure it doesn't complain when no events are found
496 jQuery(obj).unbind("test");
498 equals( obj.__events__, undefined, "Make sure events object is removed" );
501 test("unbind(type)", function() {
504 var $elem = jQuery("#firstp"),
508 ok( false, message );
511 message = "unbind passing function";
512 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
514 message = "unbind all from event";
515 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
517 message = "unbind all";
518 $elem.bind('error1', error).unbind().triggerHandler('error1');
520 message = "unbind many with function";
521 $elem.bind('error1 error2',error)
522 .unbind('error1 error2', error )
523 .trigger('error1').triggerHandler('error2');
525 message = "unbind many"; // #3538
526 $elem.bind('error1 error2',error)
527 .unbind('error1 error2')
528 .trigger('error1').triggerHandler('error2');
530 message = "unbind without a type or handler";
531 $elem.bind("error1 error2.test",error)
533 .trigger("error1").triggerHandler("error2");
536 test("unbind(eventObject)", function() {
539 var $elem = jQuery("#firstp"),
542 function assert( expected ){
544 $elem.trigger('foo').triggerHandler('bar');
545 equals( num, expected, "Check the right handlers are triggered" );
549 // This handler shouldn't be unbound
550 .bind('foo', function(){
553 .bind('foo', function(e){
558 .bind('bar', function(){
572 test("hover()", function() {
574 handler1 = function( event ) { ++times; },
575 handler2 = function( event ) { ++times; };
578 .hover(handler1, handler2)
579 .mouseenter().mouseleave()
580 .unbind("mouseenter", handler1)
581 .unbind("mouseleave", handler2)
583 .mouseenter().mouseleave()
584 .unbind("mouseenter mouseleave", handler1)
585 .mouseenter().mouseleave();
587 equals( times, 4, "hover handlers fired" );
590 test("trigger() shortcuts", function() {
592 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
593 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
594 equals( close.length, 0, "Context element does not exist, length must be zero" );
595 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
599 jQuery("#check1").click(function() {
600 ok( true, "click event handler for checkbox gets fired twice, see #815" );
604 jQuery('#firstp')[0].onclick = function(event) {
607 jQuery('#firstp').click();
608 equals( counter, 1, "Check that click, triggers onclick event handler also" );
610 var clickCounter = 0;
611 jQuery('#simon1')[0].onclick = function(event) {
614 jQuery('#simon1').click();
615 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
617 jQuery('<img />').load(function(){
618 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
622 test("trigger() bubbling", function() {
625 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
627 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
628 jQuery("html").bind("click", function(e){ html++; });
629 jQuery("body").bind("click", function(e){ body++; });
630 jQuery("#main").bind("click", function(e){ main++; });
631 jQuery("#ap").bind("click", function(){ ap++; return false; });
633 jQuery("html").trigger("click");
634 equals( doc, 1, "HTML bubble" );
635 equals( html, 1, "HTML bubble" );
637 jQuery("body").trigger("click");
638 equals( doc, 2, "Body bubble" );
639 equals( html, 2, "Body bubble" );
640 equals( body, 1, "Body bubble" );
642 jQuery("#main").trigger("click");
643 equals( doc, 3, "Main bubble" );
644 equals( html, 3, "Main bubble" );
645 equals( body, 2, "Main bubble" );
646 equals( main, 1, "Main bubble" );
648 jQuery("#ap").trigger("click");
649 equals( doc, 3, "ap bubble" );
650 equals( html, 3, "ap bubble" );
651 equals( body, 2, "ap bubble" );
652 equals( main, 1, "ap bubble" );
653 equals( ap, 1, "ap bubble" );
656 test("trigger(type, [data], [fn])", function() {
659 var handler = function(event, a, b, c) {
660 equals( event.type, "click", "check passed data" );
661 equals( a, 1, "check passed data" );
662 equals( b, "2", "check passed data" );
663 equals( c, "abc", "check passed data" );
667 var $elem = jQuery("#firstp");
669 // Simulate a "native" click
670 $elem[0].click = function(){
671 ok( true, "Native call was triggered" );
674 // Triggers handlrs and native
676 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
678 // Simulate a "native" click
679 $elem[0].click = function(){
680 ok( false, "Native call was triggered" );
683 // Trigger only the handlers (no native)
685 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
689 jQuery('#form input:first').hide().trigger('focus');
693 ok( pass, "Trigger focus on hidden element" );
697 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
701 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
703 var form = jQuery("<form action=''></form>").appendTo("body");
705 // Make sure it can be prevented locally
706 form.submit(function(){
707 ok( true, "Local bind still works." );
712 form.trigger("submit");
714 form.unbind("submit");
716 jQuery(document).submit(function(){
717 ok( true, "Make sure bubble works up to document." );
722 form.trigger("submit");
724 jQuery(document).unbind("submit");
729 test("jQuery.Event.currentTarget", function(){
732 test("trigger(eventObject, [data], [fn])", function() {
735 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
736 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
738 var event = jQuery.Event("noNew");
739 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
740 equals( event.type, "noNew", "Verify its type" );
742 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
743 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
744 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
746 event.preventDefault();
747 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
748 event.stopPropagation();
749 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
751 event.isPropagationStopped = function(){ return false };
752 event.stopImmediatePropagation();
753 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
754 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
756 $parent.bind('foo',function(e){
758 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
759 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
760 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
761 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
764 // test with an event object
765 event = new jQuery.Event("foo");
766 event.secret = 'boo!';
767 $child.trigger(event);
769 // test with a literal object
770 $child.trigger({type:'foo', secret:'boo!'});
775 ok( false, "This assertion shouldn't be reached");
778 $parent.bind('foo', error );
780 $child.bind('foo',function(e, a, b, c ){
781 equals( arguments.length, 4, "Check arguments length");
782 equals( a, 1, "Check first custom argument");
783 equals( b, 2, "Check second custom argument");
784 equals( c, 3, "Check third custom argument");
786 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
787 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
788 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
791 e.stopImmediatePropagation();
796 // We should add this back in when we want to test the order
797 // in which event handlers are iterated.
798 //$child.bind('foo', error );
800 event = new jQuery.Event("foo");
801 $child.trigger( event, [1,2,3] ).unbind();
802 equals( event.result, "result", "Check event.result attribute");
804 // Will error if it bubbles
805 $child.triggerHandler('foo');
808 $parent.unbind().remove();
811 test("jQuery.Event.currentTarget", function(){
815 $elem = jQuery('<button>a</button>').click(function(e){
816 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
820 $elem.trigger('click');
826 test("toggle(Function, Function, ...)", function() {
830 fn1 = function(e) { count++; },
831 fn2 = function(e) { count--; },
832 preventDefault = function(e) { e.preventDefault() },
833 link = jQuery('#mark');
834 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
835 equals( count, 1, "Check for toggle(fn, fn)" );
837 jQuery("#firstp").toggle(function () {
838 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
839 }, function() {}).trigger("click", [ 1, 2, 3 ]);
842 jQuery("#simon1").one("click", function() {
843 ok( true, "Execute event only once" );
844 jQuery(this).toggle(function() {
845 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
847 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
850 }).click().click().click();
865 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
867 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
869 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
871 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
873 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
875 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
877 $div.unbind('click',fns[0]);
878 var data = jQuery.data( $div[0], 'events' );
879 ok( !data, "Unbinding one function from toggle unbinds them all");
881 // Test Multi-Toggles
883 $div = jQuery("<div/>");
884 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
886 same( a, [1], "Check that a click worked." );
888 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
890 same( a, [1,2], "Check that a click worked with a second toggle." );
891 same( b, [1], "Check that a click worked with a second toggle." );
894 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
895 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
898 test(".live()/.die()", function() {
901 var submit = 0, div = 0, livea = 0, liveb = 0;
903 jQuery("div").live("submit", function(){ submit++; return false; });
904 jQuery("div").live("click", function(){ div++; });
905 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
906 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
908 // Nothing should trigger on the body
909 jQuery("body").trigger("click");
910 equals( submit, 0, "Click on body" );
911 equals( div, 0, "Click on body" );
912 equals( livea, 0, "Click on body" );
913 equals( liveb, 0, "Click on body" );
915 // This should trigger two events
916 submit = 0, div = 0, livea = 0, liveb = 0;
917 jQuery("div#nothiddendiv").trigger("click");
918 equals( submit, 0, "Click on div" );
919 equals( div, 1, "Click on div" );
920 equals( livea, 1, "Click on div" );
921 equals( liveb, 0, "Click on div" );
923 // This should trigger three events (w/ bubbling)
924 submit = 0, div = 0, livea = 0, liveb = 0;
925 jQuery("div#nothiddendivchild").trigger("click");
926 equals( submit, 0, "Click on inner div" );
927 equals( div, 2, "Click on inner div" );
928 equals( livea, 1, "Click on inner div" );
929 equals( liveb, 1, "Click on inner div" );
931 // This should trigger one submit
932 submit = 0, div = 0, livea = 0, liveb = 0;
933 jQuery("div#nothiddendivchild").trigger("submit");
934 equals( submit, 1, "Submit on div" );
935 equals( div, 0, "Submit on div" );
936 equals( livea, 0, "Submit on div" );
937 equals( liveb, 0, "Submit on div" );
939 // Make sure no other events were removed in the process
940 submit = 0, div = 0, livea = 0, liveb = 0;
941 jQuery("div#nothiddendivchild").trigger("click");
942 equals( submit, 0, "die Click on inner div" );
943 equals( div, 2, "die Click on inner div" );
944 equals( livea, 1, "die Click on inner div" );
945 equals( liveb, 1, "die Click on inner div" );
947 // Now make sure that the removal works
948 submit = 0, div = 0, livea = 0, liveb = 0;
949 jQuery("div#nothiddendivchild").die("click");
950 jQuery("div#nothiddendivchild").trigger("click");
951 equals( submit, 0, "die Click on inner div" );
952 equals( div, 2, "die Click on inner div" );
953 equals( livea, 1, "die Click on inner div" );
954 equals( liveb, 0, "die Click on inner div" );
956 // Make sure that the click wasn't removed too early
957 submit = 0, div = 0, livea = 0, liveb = 0;
958 jQuery("div#nothiddendiv").trigger("click");
959 equals( submit, 0, "die Click on inner div" );
960 equals( div, 1, "die Click on inner div" );
961 equals( livea, 1, "die Click on inner div" );
962 equals( liveb, 0, "die Click on inner div" );
964 // Make sure that stopPropgation doesn't stop live events
965 submit = 0, div = 0, livea = 0, liveb = 0;
966 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
967 jQuery("div#nothiddendivchild").trigger("click");
968 equals( submit, 0, "stopPropagation Click on inner div" );
969 equals( div, 1, "stopPropagation Click on inner div" );
970 equals( livea, 0, "stopPropagation Click on inner div" );
971 equals( liveb, 1, "stopPropagation Click on inner div" );
973 // Make sure click events only fire with primary click
974 submit = 0, div = 0, livea = 0, liveb = 0;
975 var event = jQuery.Event("click");
977 jQuery("div#nothiddendiv").trigger(event);
979 equals( livea, 0, "live secondary click" );
981 jQuery("div#nothiddendivchild").die("click");
982 jQuery("div#nothiddendiv").die("click");
983 jQuery("div").die("click");
984 jQuery("div").die("submit");
986 // Test binding with a different context
987 var clicked = 0, container = jQuery('#main')[0];
988 jQuery("#foo", container).live("click", function(e){ clicked++; });
989 jQuery("div").trigger('click');
990 jQuery("#foo").trigger('click');
991 jQuery("#main").trigger('click');
992 jQuery("body").trigger('click');
993 equals( clicked, 2, "live with a context" );
995 // Make sure the event is actually stored on the context
996 ok( jQuery.data(container, "events").live, "live with a context" );
998 // Test unbinding with a different context
999 jQuery("#foo", container).die("click");
1000 jQuery("#foo").trigger('click');
1001 equals( clicked, 2, "die with a context");
1003 // Test binding with event data
1004 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1005 jQuery("#foo").trigger("click").die("click");
1007 // Test binding with trigger data
1008 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1009 jQuery("#foo").trigger("click", true).die("click");
1011 // Test binding with different this object
1012 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1013 jQuery("#foo").trigger("click").die("click");
1015 // Test binding with different this object, event data, and trigger data
1016 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1017 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1018 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1019 equals( data, true, "live with with different this object, event data, and trigger data")
1020 }, { foo: "bar" }));
1021 jQuery("#foo").trigger("click", true).die("click");
1023 // Verify that return false prevents default action
1024 jQuery("#anchor2").live("click", function(){ return false; });
1025 var hash = window.location.hash;
1026 jQuery("#anchor2").trigger("click");
1027 equals( window.location.hash, hash, "return false worked" );
1028 jQuery("#anchor2").die("click");
1030 // Verify that .preventDefault() prevents default action
1031 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1032 var hash = window.location.hash;
1033 jQuery("#anchor2").trigger("click");
1034 equals( window.location.hash, hash, "e.preventDefault() worked" );
1035 jQuery("#anchor2").die("click");
1037 // Test binding the same handler to multiple points
1039 function callback(){ called++; return false; }
1041 jQuery("#nothiddendiv").live("click", callback);
1042 jQuery("#anchor2").live("click", callback);
1044 jQuery("#nothiddendiv").trigger("click");
1045 equals( called, 1, "Verify that only one click occurred." );
1048 jQuery("#anchor2").trigger("click");
1049 equals( called, 1, "Verify that only one click occurred." );
1051 // Make sure that only one callback is removed
1052 jQuery("#anchor2").die("click", callback);
1055 jQuery("#nothiddendiv").trigger("click");
1056 equals( called, 1, "Verify that only one click occurred." );
1059 jQuery("#anchor2").trigger("click");
1060 equals( called, 0, "Verify that no click occurred." );
1062 // Make sure that it still works if the selector is the same,
1063 // but the event type is different
1064 jQuery("#nothiddendiv").live("foo", callback);
1067 jQuery("#nothiddendiv").die("click", callback);
1070 jQuery("#nothiddendiv").trigger("click");
1071 equals( called, 0, "Verify that no click occurred." );
1074 jQuery("#nothiddendiv").trigger("foo");
1075 equals( called, 1, "Verify that one foo occurred." );
1078 jQuery("#nothiddendiv").die("foo", callback);
1080 // Make sure we don't loose the target by DOM modifications
1081 // after the bubble already reached the liveHandler
1082 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1084 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1085 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1087 jQuery("#nothiddendiv span").click();
1088 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1089 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1092 jQuery("#nothiddendivchild").die("click");
1094 // Verify that .live() ocurs and cancel buble in the same order as
1095 // we would expect .bind() and .click() without delegation
1096 var lived = 0, livee = 0;
1098 // bind one pair in one order
1099 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1100 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1102 jQuery('span#liveSpan1 a').click();
1103 equals( lived, 1, "Verify that only one first handler occurred." );
1104 equals( livee, 0, "Verify that second handler doesn't." );
1106 // and one pair in inverse
1107 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1108 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1112 jQuery('span#liveSpan2 a').click();
1113 equals( lived, 1, "Verify that only one first handler occurred." );
1114 equals( livee, 0, "Verify that second handler doesn't." );
1117 jQuery("span#liveSpan1 a").die("click")
1118 jQuery("span#liveSpan1").die("click");
1119 jQuery("span#liveSpan2 a").die("click");
1120 jQuery("span#liveSpan2").die("click");
1122 // Test this, target and currentTarget are correct
1123 jQuery('span#liveSpan1').live('click', function(e){
1124 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1125 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1126 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1129 jQuery('span#liveSpan1 a').click();
1131 jQuery('span#liveSpan1').die('click');
1133 // Work with deep selectors
1136 function clickB(){ livee++; }
1138 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1139 jQuery("#nothiddendiv div").live("click", clickB);
1140 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1142 equals( livee, 0, "No clicks, deep selector." );
1145 jQuery("#nothiddendivchild").trigger("click");
1146 equals( livee, 2, "Click, deep selector." );
1149 jQuery("#nothiddendivchild").trigger("mouseover");
1150 equals( livee, 1, "Mouseover, deep selector." );
1152 jQuery("#nothiddendiv div").die("mouseover");
1155 jQuery("#nothiddendivchild").trigger("click");
1156 equals( livee, 2, "Click, deep selector." );
1159 jQuery("#nothiddendivchild").trigger("mouseover");
1160 equals( livee, 0, "Mouseover, deep selector." );
1162 jQuery("#nothiddendiv div").die("click", clickB);
1165 jQuery("#nothiddendivchild").trigger("click");
1166 equals( livee, 1, "Click, deep selector." );
1168 jQuery("#nothiddendiv div").die("click");
1170 jQuery("#nothiddendiv div").live("blur", function(){
1171 ok( true, "Live div trigger blur." );
1174 jQuery("#nothiddendiv div").trigger("blur");
1176 jQuery("#nothiddendiv div").die("blur");
1179 test("die all bound events", function(){
1183 var div = jQuery("div#nothiddendivchild");
1185 div.live("click submit", function(){ count++; });
1188 div.trigger("click");
1189 div.trigger("submit");
1191 equals( count, 0, "Make sure no events were triggered." );
1194 test("live with multiple events", function(){
1198 var div = jQuery("div#nothiddendivchild");
1200 div.live("click submit", function(){ count++; });
1202 div.trigger("click");
1203 div.trigger("submit");
1205 equals( count, 2, "Make sure both the click and submit were triggered." );
1208 test("live with namespaces", function(){
1211 var count1 = 0, count2 = 0;
1213 jQuery("#liveSpan1").live("foo.bar", function(e){
1217 jQuery("#liveSpan1").live("foo.zed", function(e){
1221 jQuery("#liveSpan1").trigger("foo.bar");
1222 equals( count1, 1, "Got live foo.bar" );
1223 equals( count2, 0, "Got live foo.bar" );
1225 count1 = 0, count2 = 0;
1227 jQuery("#liveSpan1").trigger("foo.zed");
1228 equals( count1, 0, "Got live foo.zed" );
1229 equals( count2, 1, "Got live foo.zed" );
1232 count1 = 0, count2 = 0;
1234 jQuery("#liveSpan1").die("foo.zed");
1235 jQuery("#liveSpan1").trigger("foo.bar");
1237 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1238 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1240 count1 = 0, count2 = 0;
1242 jQuery("#liveSpan1").trigger("foo.zed");
1243 equals( count1, 0, "Got live foo.zed" );
1244 equals( count2, 0, "Got live foo.zed" );
1247 jQuery("#liveSpan1").die("foo.bar");
1249 count1 = 0, count2 = 0;
1251 jQuery("#liveSpan1").trigger("foo.bar");
1252 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1253 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1255 jQuery("#liveSpan1").trigger("foo.zed");
1256 equals( count1, 0, "Did not trigger foo.zed again" );
1257 equals( count2, 0, "Did not trigger foo.zed again" );
1260 test("live with change", function(){
1263 var selectChange = 0, checkboxChange = 0;
1265 var select = jQuery("select[name='S1']")
1266 select.live("change", function() {
1270 var checkbox = jQuery("#check2"),
1271 checkboxFunction = function(){
1274 checkbox.live("change", checkboxFunction);
1276 // test click on select
1278 // second click that changed it
1280 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1281 select.trigger("change");
1282 equals( selectChange, 1, "Change on click." );
1284 // test keys on select
1286 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1287 select.trigger("change");
1288 equals( selectChange, 1, "Change on keyup." );
1290 // test click on checkbox
1291 checkbox.trigger("change");
1292 equals( checkboxChange, 1, "Change on checkbox." );
1294 // test blur/focus on text
1295 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1296 text.live("change", function() {
1300 text.val(oldTextVal+"foo");
1301 text.trigger("change");
1302 equals( textChange, 1, "Change on text input." );
1304 text.val(oldTextVal);
1307 // test blur/focus on password
1308 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1309 password.live("change", function() {
1313 password.val(oldPasswordVal + "foo");
1314 password.trigger("change");
1315 equals( passwordChange, 1, "Change on password input." );
1317 password.val(oldPasswordVal);
1318 password.die("change");
1320 // make sure die works
1324 select.die("change");
1325 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1326 select.trigger("change");
1327 equals( selectChange, 0, "Die on click works." );
1330 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1331 select.trigger("change");
1332 equals( selectChange, 0, "Die on keyup works." );
1334 // die specific checkbox
1335 checkbox.die("change", checkboxFunction);
1336 checkbox.trigger("change");
1337 equals( checkboxChange, 1, "Die on checkbox." );
1340 test("live with submit", function() {
1341 var count1 = 0, count2 = 0;
1343 jQuery("#testForm").live("submit", function(ev) {
1345 ev.preventDefault();
1348 jQuery("body").live("submit", function(ev) {
1350 ev.preventDefault();
1353 jQuery("#testForm input[name=sub1]").submit();
1354 equals( count1, 1, "Verify form submit." );
1355 equals( count2, 1, "Verify body submit." );
1357 jQuery("#testForm").die("submit");
1358 jQuery("body").die("submit");
1361 test("live with special events", function() {
1364 jQuery.event.special.foo = {
1365 setup: function( data, namespaces, handler ) {
1366 ok( true, "Setup run." );
1368 teardown: function( namespaces ) {
1369 ok( true, "Teardown run." );
1371 add: function( handleObj ) {
1372 ok( true, "Add run." );
1374 remove: function( handleObj ) {
1375 ok( true, "Remove run." );
1377 _default: function( event ) {
1378 ok( true, "Default run." );
1383 jQuery("#liveSpan1").live("foo.a", function(e){
1384 ok( true, "Handler 1 run." );
1388 jQuery("#liveSpan1").live("foo.b", function(e){
1389 ok( true, "Handler 2 run." );
1392 // Run: Handler 1, Handler 2, Default
1393 jQuery("#liveSpan1").trigger("foo");
1395 // Run: Handler 1, Default
1396 // TODO: Namespace doesn't trigger default (?)
1397 jQuery("#liveSpan1").trigger("foo.a");
1400 jQuery("#liveSpan1").die("foo.a");
1402 // Run: Handler 2, Default
1403 jQuery("#liveSpan1").trigger("foo");
1405 // Run: remove, teardown
1406 jQuery("#liveSpan1").die("foo");
1408 delete jQuery.event.special.foo;
1411 test(".delegate()/.undelegate()", function() {
1414 var submit = 0, div = 0, livea = 0, liveb = 0;
1416 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1417 jQuery("#body").delegate("div", "click", function(){ div++; });
1418 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1419 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1421 // Nothing should trigger on the body
1422 jQuery("body").trigger("click");
1423 equals( submit, 0, "Click on body" );
1424 equals( div, 0, "Click on body" );
1425 equals( livea, 0, "Click on body" );
1426 equals( liveb, 0, "Click on body" );
1428 // This should trigger two events
1429 submit = 0, div = 0, livea = 0, liveb = 0;
1430 jQuery("div#nothiddendiv").trigger("click");
1431 equals( submit, 0, "Click on div" );
1432 equals( div, 1, "Click on div" );
1433 equals( livea, 1, "Click on div" );
1434 equals( liveb, 0, "Click on div" );
1436 // This should trigger three events (w/ bubbling)
1437 submit = 0, div = 0, livea = 0, liveb = 0;
1438 jQuery("div#nothiddendivchild").trigger("click");
1439 equals( submit, 0, "Click on inner div" );
1440 equals( div, 2, "Click on inner div" );
1441 equals( livea, 1, "Click on inner div" );
1442 equals( liveb, 1, "Click on inner div" );
1444 // This should trigger one submit
1445 submit = 0, div = 0, livea = 0, liveb = 0;
1446 jQuery("div#nothiddendivchild").trigger("submit");
1447 equals( submit, 1, "Submit on div" );
1448 equals( div, 0, "Submit on div" );
1449 equals( livea, 0, "Submit on div" );
1450 equals( liveb, 0, "Submit on div" );
1452 // Make sure no other events were removed in the process
1453 submit = 0, div = 0, livea = 0, liveb = 0;
1454 jQuery("div#nothiddendivchild").trigger("click");
1455 equals( submit, 0, "undelegate Click on inner div" );
1456 equals( div, 2, "undelegate Click on inner div" );
1457 equals( livea, 1, "undelegate Click on inner div" );
1458 equals( liveb, 1, "undelegate Click on inner div" );
1460 // Now make sure that the removal works
1461 submit = 0, div = 0, livea = 0, liveb = 0;
1462 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1463 jQuery("div#nothiddendivchild").trigger("click");
1464 equals( submit, 0, "undelegate Click on inner div" );
1465 equals( div, 2, "undelegate Click on inner div" );
1466 equals( livea, 1, "undelegate Click on inner div" );
1467 equals( liveb, 0, "undelegate Click on inner div" );
1469 // Make sure that the click wasn't removed too early
1470 submit = 0, div = 0, livea = 0, liveb = 0;
1471 jQuery("div#nothiddendiv").trigger("click");
1472 equals( submit, 0, "undelegate Click on inner div" );
1473 equals( div, 1, "undelegate Click on inner div" );
1474 equals( livea, 1, "undelegate Click on inner div" );
1475 equals( liveb, 0, "undelegate Click on inner div" );
1477 // Make sure that stopPropgation doesn't stop live events
1478 submit = 0, div = 0, livea = 0, liveb = 0;
1479 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1480 jQuery("div#nothiddendivchild").trigger("click");
1481 equals( submit, 0, "stopPropagation Click on inner div" );
1482 equals( div, 1, "stopPropagation Click on inner div" );
1483 equals( livea, 0, "stopPropagation Click on inner div" );
1484 equals( liveb, 1, "stopPropagation Click on inner div" );
1486 // Make sure click events only fire with primary click
1487 submit = 0, div = 0, livea = 0, liveb = 0;
1488 var event = jQuery.Event("click");
1490 jQuery("div#nothiddendiv").trigger(event);
1492 equals( livea, 0, "delegate secondary click" );
1494 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1495 jQuery("#body").undelegate("div#nothiddendiv", "click");
1496 jQuery("#body").undelegate("div", "click");
1497 jQuery("#body").undelegate("div", "submit");
1499 // Test binding with a different context
1500 var clicked = 0, container = jQuery('#main')[0];
1501 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1502 jQuery("div").trigger('click');
1503 jQuery("#foo").trigger('click');
1504 jQuery("#main").trigger('click');
1505 jQuery("body").trigger('click');
1506 equals( clicked, 2, "delegate with a context" );
1508 // Make sure the event is actually stored on the context
1509 ok( jQuery.data(container, "events").live, "delegate with a context" );
1511 // Test unbinding with a different context
1512 jQuery("#main").undelegate("#foo", "click");
1513 jQuery("#foo").trigger('click');
1514 equals( clicked, 2, "undelegate with a context");
1516 // Test binding with event data
1517 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1518 jQuery("#foo").trigger("click");
1519 jQuery("#body").undelegate("#foo", "click");
1521 // Test binding with trigger data
1522 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1523 jQuery("#foo").trigger("click", true);
1524 jQuery("#body").undelegate("#foo", "click");
1526 // Test binding with different this object
1527 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1528 jQuery("#foo").trigger("click");
1529 jQuery("#body").undelegate("#foo", "click");
1531 // Test binding with different this object, event data, and trigger data
1532 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1533 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1534 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1535 equals( data, true, "delegate with with different this object, event data, and trigger data")
1536 }, { foo: "bar" }));
1537 jQuery("#foo").trigger("click", true);
1538 jQuery("#body").undelegate("#foo", "click");
1540 // Verify that return false prevents default action
1541 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1542 var hash = window.location.hash;
1543 jQuery("#anchor2").trigger("click");
1544 equals( window.location.hash, hash, "return false worked" );
1545 jQuery("#body").undelegate("#anchor2", "click");
1547 // Verify that .preventDefault() prevents default action
1548 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1549 var hash = window.location.hash;
1550 jQuery("#anchor2").trigger("click");
1551 equals( window.location.hash, hash, "e.preventDefault() worked" );
1552 jQuery("#body").undelegate("#anchor2", "click");
1554 // Test binding the same handler to multiple points
1556 function callback(){ called++; return false; }
1558 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1559 jQuery("#body").delegate("#anchor2", "click", callback);
1561 jQuery("#nothiddendiv").trigger("click");
1562 equals( called, 1, "Verify that only one click occurred." );
1565 jQuery("#anchor2").trigger("click");
1566 equals( called, 1, "Verify that only one click occurred." );
1568 // Make sure that only one callback is removed
1569 jQuery("#body").undelegate("#anchor2", "click", callback);
1572 jQuery("#nothiddendiv").trigger("click");
1573 equals( called, 1, "Verify that only one click occurred." );
1576 jQuery("#anchor2").trigger("click");
1577 equals( called, 0, "Verify that no click occurred." );
1579 // Make sure that it still works if the selector is the same,
1580 // but the event type is different
1581 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1584 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1587 jQuery("#nothiddendiv").trigger("click");
1588 equals( called, 0, "Verify that no click occurred." );
1591 jQuery("#nothiddendiv").trigger("foo");
1592 equals( called, 1, "Verify that one foo occurred." );
1595 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1597 // Make sure we don't loose the target by DOM modifications
1598 // after the bubble already reached the liveHandler
1599 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1601 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1602 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1604 jQuery("#nothiddendiv span").click();
1605 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1606 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1609 jQuery("#body").undelegate("#nothiddendivchild", "click");
1611 // Verify that .live() ocurs and cancel buble in the same order as
1612 // we would expect .bind() and .click() without delegation
1613 var lived = 0, livee = 0;
1615 // bind one pair in one order
1616 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1617 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1619 jQuery('span#liveSpan1 a').click();
1620 equals( lived, 1, "Verify that only one first handler occurred." );
1621 equals( livee, 0, "Verify that second handler doesn't." );
1623 // and one pair in inverse
1624 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1625 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1629 jQuery('span#liveSpan2 a').click();
1630 equals( lived, 1, "Verify that only one first handler occurred." );
1631 equals( livee, 0, "Verify that second handler doesn't." );
1634 jQuery("#body").undelegate("click");
1636 // Test this, target and currentTarget are correct
1637 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1638 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1639 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1640 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1643 jQuery('span#liveSpan1 a').click();
1645 jQuery("#body").undelegate('span#liveSpan1', 'click');
1647 // Work with deep selectors
1650 function clickB(){ livee++; }
1652 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1653 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1654 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1656 equals( livee, 0, "No clicks, deep selector." );
1659 jQuery("#nothiddendivchild").trigger("click");
1660 equals( livee, 2, "Click, deep selector." );
1663 jQuery("#nothiddendivchild").trigger("mouseover");
1664 equals( livee, 1, "Mouseover, deep selector." );
1666 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1669 jQuery("#nothiddendivchild").trigger("click");
1670 equals( livee, 2, "Click, deep selector." );
1673 jQuery("#nothiddendivchild").trigger("mouseover");
1674 equals( livee, 0, "Mouseover, deep selector." );
1676 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1679 jQuery("#nothiddendivchild").trigger("click");
1680 equals( livee, 1, "Click, deep selector." );
1682 jQuery("#body").undelegate("#nothiddendiv div", "click");
1685 test("undelegate all bound events", function(){
1689 var div = jQuery("#body");
1691 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1694 jQuery("div#nothiddendivchild").trigger("click");
1695 jQuery("div#nothiddendivchild").trigger("submit");
1697 equals( count, 0, "Make sure no events were triggered." );
1700 test("delegate with multiple events", function(){
1704 var div = jQuery("#body");
1706 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1708 jQuery("div#nothiddendivchild").trigger("click");
1709 jQuery("div#nothiddendivchild").trigger("submit");
1711 equals( count, 2, "Make sure both the click and submit were triggered." );
1713 jQuery("#body").undelegate();
1716 test("delegate with change", function(){
1719 var selectChange = 0, checkboxChange = 0;
1721 var select = jQuery("select[name='S1']");
1722 jQuery("#body").delegate("select[name='S1']", "change", function() {
1726 var checkbox = jQuery("#check2"),
1727 checkboxFunction = function(){
1730 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1732 // test click on select
1734 // second click that changed it
1736 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1737 select.trigger("change");
1738 equals( selectChange, 1, "Change on click." );
1740 // test keys on select
1742 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1743 select.trigger("change");
1744 equals( selectChange, 1, "Change on keyup." );
1746 // test click on checkbox
1747 checkbox.trigger("change");
1748 equals( checkboxChange, 1, "Change on checkbox." );
1750 // test blur/focus on text
1751 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1752 jQuery("#body").delegate("#name", "change", function() {
1756 text.val(oldTextVal+"foo");
1757 text.trigger("change");
1758 equals( textChange, 1, "Change on text input." );
1760 text.val(oldTextVal);
1761 jQuery("#body").die("change");
1763 // test blur/focus on password
1764 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1765 jQuery("#body").delegate("#name", "change", function() {
1769 password.val(oldPasswordVal + "foo");
1770 password.trigger("change");
1771 equals( passwordChange, 1, "Change on password input." );
1773 password.val(oldPasswordVal);
1774 jQuery("#body").undelegate("#name", "change");
1776 // make sure die works
1780 jQuery("#body").undelegate("select[name='S1']", "change");
1781 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1782 select.trigger("change");
1783 equals( selectChange, 0, "Die on click works." );
1786 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1787 select.trigger("change");
1788 equals( selectChange, 0, "Die on keyup works." );
1790 // die specific checkbox
1791 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1792 checkbox.trigger("change");
1793 equals( checkboxChange, 1, "Die on checkbox." );
1796 test("delegate with submit", function() {
1797 var count1 = 0, count2 = 0;
1799 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1801 ev.preventDefault();
1804 jQuery(document).delegate("body", "submit", function(ev) {
1806 ev.preventDefault();
1809 jQuery("#testForm input[name=sub1]").submit();
1810 equals( count1, 1, "Verify form submit." );
1811 equals( count2, 1, "Verify body submit." );
1813 jQuery("#body").undelegate();
1814 jQuery(document).undelegate();
1817 test("Non DOM element events", function() {
1822 jQuery(o).bind('nonelementobj', function(e) {
1823 ok( true, "Event on non-DOM object triggered" );
1826 jQuery(o).trigger('nonelementobj');
1829 test("window resize", function() {
1832 jQuery(window).unbind();
1834 jQuery(window).bind("resize", function(){
1835 ok( true, "Resize event fired." );
1836 }).resize().unbind("resize");
1838 ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1841 test("focusin bubbles", function() {
1842 //create an input and focusin on it
1843 var input = jQuery("<input/>"), order = 0;
1845 input.prependTo("body");
1847 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1848 equals(1,order++,"focusin on the body second")
1851 input.bind("focusin.focusinBubblesTest",function(){
1852 equals(0,order++,"focusin on the element first")
1858 jQuery("body").unbind("focusin.focusinBubblesTest");
1862 test("jQuery(function($) {})", function() {
1864 jQuery(function($) {
1865 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");
1870 test("event properties", function() {
1872 jQuery("#simon1").click(function(event) {
1873 ok( event.timeStamp, "assert event.timeStamp is present" );