3 test("bind(), with data", function() {
5 var handler = function(event) {
6 ok( event.data, "bind() with data, check passed data exists" );
7 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
9 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
11 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
14 test("click(), with data", function() {
16 var handler = function(event) {
17 ok( event.data, "bind() with data, check passed data exists" );
18 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
20 jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
22 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
25 test("bind(), with data, trigger with data", function() {
27 var handler = function(event, data) {
28 ok( event.data, "check passed data exists" );
29 equals( event.data.foo, "bar", "Check value of passed data" );
30 ok( data, "Check trigger data" );
31 equals( data.bar, "foo", "Check value of trigger data" );
33 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
36 test("bind(), multiple events at once", function() {
40 var handler = function(event) {
41 if (event.type == "click")
43 else if (event.type == "mouseover")
44 mouseoverCounter += 1;
46 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
47 equals( clickCounter, 1, "bind() with multiple events at once" );
48 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
51 test("bind(), multiple events at once and namespaces", function() {
56 var div = jQuery("<div/>").bind("focusin.a", function(e) {
57 equals( e.type, cur, "Verify right single event was fired." );
61 div.trigger("focusin.a");
63 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
64 equals( e.type, cur, "Verify right multi event was fired." );
65 equals( e.data, obj, "Make sure the data came in correctly." );
72 div.trigger("mouseover");
74 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
75 equals( e.type, cur, "Verify right multi event was fired." );
79 div.trigger("focusin.a");
82 div.trigger("focusout.b");
85 test("bind(), namespace with special add", function() {
88 var div = jQuery("<div/>").bind("test", function(e) {
89 ok( true, "Test event fired." );
94 jQuery.event.special.test = {
95 _default: function(e) {
96 equals( this, document, "Make sure we're at the top of the chain." );
97 equals( e.type, "test", "And that we're still dealing with a test event." );
98 equals( e.target, div[0], "And that the target is correct." );
101 teardown: function(){
102 ok(true, "Teardown called.");
104 add: function( handleObj ) {
105 var handler = handleObj.handler;
106 handleObj.handler = function(e) {
108 handler.apply( this, arguments );
112 ok(true, "Remove called.");
116 div.bind("test.a", {x: 1}, function(e) {
117 ok( !!e.xyz, "Make sure that the data is getting passed through." );
118 equals( e.data.x, 1, "Make sure data is attached properly." );
121 div.bind("test.b", {x: 2}, function(e) {
122 ok( !!e.xyz, "Make sure that the data is getting passed through." );
123 equals( e.data.x, 2, "Make sure data is attached properly." );
130 div.trigger("test.a");
133 div.trigger("test.b");
138 div = jQuery("<div/>").bind("test", function(e) {
139 ok( true, "Test event fired." );
143 div.appendTo("#main").remove();
145 delete jQuery.event.special.test;
148 test("bind(), no data", function() {
150 var handler = function(event) {
151 ok ( !event.data, "Check that no data is added to the event object" );
153 jQuery("#firstp").bind("click", handler).trigger("click");
156 test("bind/one/unbind(Object)", function(){
159 var clickCounter = 0, mouseoverCounter = 0;
160 function handler(event) {
161 if (event.type == "click")
163 else if (event.type == "mouseover")
167 function handlerWithData(event) {
168 if (event.type == "click")
169 clickCounter += event.data;
170 else if (event.type == "mouseover")
171 mouseoverCounter += event.data;
175 $elem.trigger("click").trigger("mouseover");
178 var $elem = jQuery("#firstp")
186 click:handlerWithData,
187 mouseover:handlerWithData
192 equals( clickCounter, 3, "bind(Object)" );
193 equals( mouseoverCounter, 3, "bind(Object)" );
196 equals( clickCounter, 4, "bind(Object)" );
197 equals( mouseoverCounter, 4, "bind(Object)" );
199 jQuery("#firstp").unbind({
205 equals( clickCounter, 4, "bind(Object)" );
206 equals( mouseoverCounter, 4, "bind(Object)" );
209 test("live/die(Object), delegate/undelegate(String, Object)", function() {
212 var clickCounter = 0, mouseoverCounter = 0,
213 $p = jQuery("#firstp"), $a = $p.find("a:first");
216 click: function( event ) {
217 clickCounter += ( event.data || 1 );
219 mouseover: function( event ) {
220 mouseoverCounter += ( event.data || 1 );
225 $a.trigger("click").trigger("mouseover");
229 $p.delegate( "a", events, 2 );
232 equals( clickCounter, 3, "live/delegate" );
233 equals( mouseoverCounter, 3, "live/delegate" );
235 $p.undelegate( "a", events );
238 equals( clickCounter, 4, "undelegate" );
239 equals( mouseoverCounter, 4, "undelegate" );
244 equals( clickCounter, 4, "die" );
245 equals( mouseoverCounter, 4, "die" );
248 test("bind(), iframes", function() {
249 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
250 var doc = jQuery("#loadediframe").contents();
252 jQuery("div", doc).bind("click", function() {
253 ok( true, "Binding to element inside iframe" );
254 }).click().unbind('click');
257 test("bind(), trigger change on select", function() {
260 function selectOnChange(event) {
261 equals( event.data, counter++, "Event.data is not a global event object" );
263 jQuery("#form select").each(function(i){
264 jQuery(this).bind('change', i, selectOnChange);
265 }).trigger('change');
268 test("bind(), namespaced events, cloned events", function() {
271 jQuery("#firstp").bind("custom.test",function(e){
272 ok(true, "Custom event triggered");
275 jQuery("#firstp").bind("click",function(e){
276 ok(true, "Normal click triggered");
279 jQuery("#firstp").bind("click.test",function(e){
280 ok(true, "Namespaced click triggered");
283 // Trigger both bound fn (2)
284 jQuery("#firstp").trigger("click");
286 // Trigger one bound fn (1)
287 jQuery("#firstp").trigger("click.test");
289 // Remove only the one fn
290 jQuery("#firstp").unbind("click.test");
292 // Trigger the remaining fn (1)
293 jQuery("#firstp").trigger("click");
295 // Remove the remaining fn
296 jQuery("#firstp").unbind(".test");
298 // Trigger the remaining fn (0)
299 jQuery("#firstp").trigger("custom");
301 // using contents will get comments regular, text, and comment nodes
302 jQuery("#nonnodes").contents().bind("tester", function () {
303 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
304 }).trigger("tester");
306 // Make sure events stick with appendTo'd elements (which are cloned) #2027
307 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
308 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
311 test("bind(), multi-namespaced events", function() {
323 function check(name, msg){
324 same(name, order.shift(), msg);
327 jQuery("#firstp").bind("custom.test",function(e){
328 check("custom.test", "Custom event triggered");
331 jQuery("#firstp").bind("custom.test2",function(e){
332 check("custom.test2", "Custom event triggered");
335 jQuery("#firstp").bind("click.test",function(e){
336 check("click.test", "Normal click triggered");
339 jQuery("#firstp").bind("click.test.abc",function(e){
340 check("click.test.abc", "Namespaced click triggered");
343 // Those would not trigger/unbind (#5303)
344 jQuery("#firstp").trigger("click.a.test");
345 jQuery("#firstp").unbind("click.a.test");
347 // Trigger both bound fn (1)
348 jQuery("#firstp").trigger("click.test.abc");
350 // Trigger one bound fn (1)
351 jQuery("#firstp").trigger("click.abc");
353 // Trigger two bound fn (2)
354 jQuery("#firstp").trigger("click.test");
356 // Remove only the one fn
357 jQuery("#firstp").unbind("click.abc");
359 // Trigger the remaining fn (1)
360 jQuery("#firstp").trigger("click");
362 // Remove the remaining fn
363 jQuery("#firstp").unbind(".test");
365 // Trigger the remaining fn (1)
366 jQuery("#firstp").trigger("custom");
369 test("bind(), with same function", function() {
372 var count = 0 , func = function(){
376 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
377 jQuery("#liveHandlerOrder").trigger("foo.bar");
379 equals(count, 1, "Verify binding function with multiple namespaces." );
381 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
382 jQuery("#liveHandlerOrder").trigger("foo.bar");
384 equals(count, 1, "Verify that removing events still work." );
387 test("bind(), make sure order is maintained", function() {
390 var elem = jQuery("#firstp"), log = [], check = [];
392 for ( var i = 0; i < 100; i++ ) (function(i){
393 elem.bind( "click", function(){
400 elem.trigger("click");
402 equals( log.join(","), check.join(","), "Make sure order was maintained." );
404 elem.unbind("click");
407 test("bind(), with different this object", function() {
409 var thisObject = { myThis: true },
410 data = { myData: true },
411 handler1 = function( event ) {
412 equals( this, thisObject, "bind() with different this object" );
414 handler2 = function( event ) {
415 equals( this, thisObject, "bind() with different this object and data" );
416 equals( event.data, data, "bind() with different this object and data" );
420 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
421 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
423 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
426 test("bind(name, false), unbind(name, false)", function() {
430 jQuery("#main").bind("click", function(e){ main++; });
431 jQuery("#ap").trigger("click");
432 equals( main, 1, "Verify that the trigger happened correctly." );
435 jQuery("#ap").bind("click", false);
436 jQuery("#ap").trigger("click");
437 equals( main, 0, "Verify that no bubble happened." );
440 jQuery("#ap").unbind("click", false);
441 jQuery("#ap").trigger("click");
442 equals( main, 1, "Verify that the trigger happened correctly." );
445 test("bind()/trigger()/unbind() on plain object", function() {
450 // Make sure it doesn't complain when no events are found
451 jQuery(obj).trigger("test");
453 // Make sure it doesn't complain when no events are found
454 jQuery(obj).unbind("test");
456 jQuery(obj).bind("test", function(){
457 ok( true, "Custom event run." );
460 var events = jQuery(obj).data("__events__");
461 ok( events, "Object has events bound." );
462 equals( obj.events, undefined, "Events object on plain objects is not events" );
463 equals( typeof events, "function", "'events' expando is a function on plain objects." );
464 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
465 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
468 jQuery(obj).trigger("test");
470 jQuery(obj).unbind("test");
473 jQuery(obj).trigger("test");
475 // Make sure it doesn't complain when no events are found
476 jQuery(obj).unbind("test");
478 equals( obj.__events__, undefined, "Make sure events object is removed" );
481 test("unbind(type)", function() {
484 var $elem = jQuery("#firstp"),
488 ok( false, message );
491 message = "unbind passing function";
492 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
494 message = "unbind all from event";
495 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
497 message = "unbind all";
498 $elem.bind('error1', error).unbind().triggerHandler('error1');
500 message = "unbind many with function";
501 $elem.bind('error1 error2',error)
502 .unbind('error1 error2', error )
503 .trigger('error1').triggerHandler('error2');
505 message = "unbind many"; // #3538
506 $elem.bind('error1 error2',error)
507 .unbind('error1 error2')
508 .trigger('error1').triggerHandler('error2');
510 message = "unbind without a type or handler";
511 $elem.bind("error1 error2.test",error)
513 .trigger("error1").triggerHandler("error2");
516 test("unbind(eventObject)", function() {
519 var $elem = jQuery("#firstp"),
522 function assert( expected ){
524 $elem.trigger('foo').triggerHandler('bar');
525 equals( num, expected, "Check the right handlers are triggered" );
529 // This handler shouldn't be unbound
530 .bind('foo', function(){
533 .bind('foo', function(e){
538 .bind('bar', function(){
552 test("hover()", function() {
554 handler1 = function( event ) { ++times; },
555 handler2 = function( event ) { ++times; };
558 .hover(handler1, handler2)
559 .mouseenter().mouseleave()
560 .unbind("mouseenter", handler1)
561 .unbind("mouseleave", handler2)
563 .mouseenter().mouseleave()
564 .unbind("mouseenter mouseleave", handler1)
565 .mouseenter().mouseleave();
567 equals( times, 4, "hover handlers fired" );
570 test("trigger() shortcuts", function() {
572 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
573 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
574 equals( close.length, 0, "Context element does not exist, length must be zero" );
575 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
579 jQuery("#check1").click(function() {
580 ok( true, "click event handler for checkbox gets fired twice, see #815" );
584 jQuery('#firstp')[0].onclick = function(event) {
587 jQuery('#firstp').click();
588 equals( counter, 1, "Check that click, triggers onclick event handler also" );
590 var clickCounter = 0;
591 jQuery('#simon1')[0].onclick = function(event) {
594 jQuery('#simon1').click();
595 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
597 jQuery('<img />').load(function(){
598 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
602 test("trigger() bubbling", function() {
605 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
607 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
608 jQuery("html").bind("click", function(e){ html++; });
609 jQuery("body").bind("click", function(e){ body++; });
610 jQuery("#main").bind("click", function(e){ main++; });
611 jQuery("#ap").bind("click", function(){ ap++; return false; });
613 jQuery("html").trigger("click");
614 equals( doc, 1, "HTML bubble" );
615 equals( html, 1, "HTML bubble" );
617 jQuery("body").trigger("click");
618 equals( doc, 2, "Body bubble" );
619 equals( html, 2, "Body bubble" );
620 equals( body, 1, "Body bubble" );
622 jQuery("#main").trigger("click");
623 equals( doc, 3, "Main bubble" );
624 equals( html, 3, "Main bubble" );
625 equals( body, 2, "Main bubble" );
626 equals( main, 1, "Main bubble" );
628 jQuery("#ap").trigger("click");
629 equals( doc, 3, "ap bubble" );
630 equals( html, 3, "ap bubble" );
631 equals( body, 2, "ap bubble" );
632 equals( main, 1, "ap bubble" );
633 equals( ap, 1, "ap bubble" );
636 test("trigger(type, [data], [fn])", function() {
639 var handler = function(event, a, b, c) {
640 equals( event.type, "click", "check passed data" );
641 equals( a, 1, "check passed data" );
642 equals( b, "2", "check passed data" );
643 equals( c, "abc", "check passed data" );
647 var $elem = jQuery("#firstp");
649 // Simulate a "native" click
650 $elem[0].click = function(){
651 ok( true, "Native call was triggered" );
654 // Triggers handlrs and native
656 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
658 // Simulate a "native" click
659 $elem[0].click = function(){
660 ok( false, "Native call was triggered" );
663 // Trigger only the handlers (no native)
665 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
669 jQuery('#form input:first').hide().trigger('focus');
673 ok( pass, "Trigger focus on hidden element" );
677 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
681 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
683 var form = jQuery("<form action=''></form>").appendTo("body");
685 // Make sure it can be prevented locally
686 form.submit(function(){
687 ok( true, "Local bind still works." );
692 form.trigger("submit");
694 form.unbind("submit");
696 jQuery(document).submit(function(){
697 ok( true, "Make sure bubble works up to document." );
702 form.trigger("submit");
704 jQuery(document).unbind("submit");
709 test("jQuery.Event.currentTarget", function(){
712 test("trigger(eventObject, [data], [fn])", function() {
715 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
716 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
718 var event = jQuery.Event("noNew");
719 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
720 equals( event.type, "noNew", "Verify its type" );
722 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
723 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
724 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
726 event.preventDefault();
727 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
728 event.stopPropagation();
729 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
731 event.isPropagationStopped = function(){ return false };
732 event.stopImmediatePropagation();
733 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
734 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
736 $parent.bind('foo',function(e){
738 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
739 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
740 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
741 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
744 // test with an event object
745 event = new jQuery.Event("foo");
746 event.secret = 'boo!';
747 $child.trigger(event);
749 // test with a literal object
750 $child.trigger({type:'foo', secret:'boo!'});
755 ok( false, "This assertion shouldn't be reached");
758 $parent.bind('foo', error );
760 $child.bind('foo',function(e, a, b, c ){
761 equals( arguments.length, 4, "Check arguments length");
762 equals( a, 1, "Check first custom argument");
763 equals( b, 2, "Check second custom argument");
764 equals( c, 3, "Check third custom argument");
766 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
767 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
768 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
771 e.stopImmediatePropagation();
776 // We should add this back in when we want to test the order
777 // in which event handlers are iterated.
778 //$child.bind('foo', error );
780 event = new jQuery.Event("foo");
781 $child.trigger( event, [1,2,3] ).unbind();
782 equals( event.result, "result", "Check event.result attribute");
784 // Will error if it bubbles
785 $child.triggerHandler('foo');
788 $parent.unbind().remove();
791 test("jQuery.Event.currentTarget", function(){
795 $elem = jQuery('<button>a</button>').click(function(e){
796 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
800 $elem.trigger('click');
806 test("toggle(Function, Function, ...)", function() {
810 fn1 = function(e) { count++; },
811 fn2 = function(e) { count--; },
812 preventDefault = function(e) { e.preventDefault() },
813 link = jQuery('#mark');
814 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
815 equals( count, 1, "Check for toggle(fn, fn)" );
817 jQuery("#firstp").toggle(function () {
818 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
819 }, function() {}).trigger("click", [ 1, 2, 3 ]);
822 jQuery("#simon1").one("click", function() {
823 ok( true, "Execute event only once" );
824 jQuery(this).toggle(function() {
825 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
827 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
830 }).click().click().click();
845 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
847 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
849 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
851 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
853 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
855 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
857 $div.unbind('click',fns[0]);
858 var data = jQuery.data( $div[0], 'events' );
859 ok( !data, "Unbinding one function from toggle unbinds them all");
861 // Test Multi-Toggles
863 $div = jQuery("<div/>");
864 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
866 same( a, [1], "Check that a click worked." );
868 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
870 same( a, [1,2], "Check that a click worked with a second toggle." );
871 same( b, [1], "Check that a click worked with a second toggle." );
874 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
875 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
878 test(".live()/.die()", function() {
881 var submit = 0, div = 0, livea = 0, liveb = 0;
883 jQuery("div").live("submit", function(){ submit++; return false; });
884 jQuery("div").live("click", function(){ div++; });
885 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
886 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
888 // Nothing should trigger on the body
889 jQuery("body").trigger("click");
890 equals( submit, 0, "Click on body" );
891 equals( div, 0, "Click on body" );
892 equals( livea, 0, "Click on body" );
893 equals( liveb, 0, "Click on body" );
895 // This should trigger two events
896 submit = 0, div = 0, livea = 0, liveb = 0;
897 jQuery("div#nothiddendiv").trigger("click");
898 equals( submit, 0, "Click on div" );
899 equals( div, 1, "Click on div" );
900 equals( livea, 1, "Click on div" );
901 equals( liveb, 0, "Click on div" );
903 // This should trigger three events (w/ bubbling)
904 submit = 0, div = 0, livea = 0, liveb = 0;
905 jQuery("div#nothiddendivchild").trigger("click");
906 equals( submit, 0, "Click on inner div" );
907 equals( div, 2, "Click on inner div" );
908 equals( livea, 1, "Click on inner div" );
909 equals( liveb, 1, "Click on inner div" );
911 // This should trigger one submit
912 submit = 0, div = 0, livea = 0, liveb = 0;
913 jQuery("div#nothiddendivchild").trigger("submit");
914 equals( submit, 1, "Submit on div" );
915 equals( div, 0, "Submit on div" );
916 equals( livea, 0, "Submit on div" );
917 equals( liveb, 0, "Submit on div" );
919 // Make sure no other events were removed in the process
920 submit = 0, div = 0, livea = 0, liveb = 0;
921 jQuery("div#nothiddendivchild").trigger("click");
922 equals( submit, 0, "die Click on inner div" );
923 equals( div, 2, "die Click on inner div" );
924 equals( livea, 1, "die Click on inner div" );
925 equals( liveb, 1, "die Click on inner div" );
927 // Now make sure that the removal works
928 submit = 0, div = 0, livea = 0, liveb = 0;
929 jQuery("div#nothiddendivchild").die("click");
930 jQuery("div#nothiddendivchild").trigger("click");
931 equals( submit, 0, "die Click on inner div" );
932 equals( div, 2, "die Click on inner div" );
933 equals( livea, 1, "die Click on inner div" );
934 equals( liveb, 0, "die Click on inner div" );
936 // Make sure that the click wasn't removed too early
937 submit = 0, div = 0, livea = 0, liveb = 0;
938 jQuery("div#nothiddendiv").trigger("click");
939 equals( submit, 0, "die Click on inner div" );
940 equals( div, 1, "die Click on inner div" );
941 equals( livea, 1, "die Click on inner div" );
942 equals( liveb, 0, "die Click on inner div" );
944 // Make sure that stopPropgation doesn't stop live events
945 submit = 0, div = 0, livea = 0, liveb = 0;
946 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
947 jQuery("div#nothiddendivchild").trigger("click");
948 equals( submit, 0, "stopPropagation Click on inner div" );
949 equals( div, 1, "stopPropagation Click on inner div" );
950 equals( livea, 0, "stopPropagation Click on inner div" );
951 equals( liveb, 1, "stopPropagation Click on inner div" );
953 // Make sure click events only fire with primary click
954 submit = 0, div = 0, livea = 0, liveb = 0;
955 var event = jQuery.Event("click");
957 jQuery("div#nothiddendiv").trigger(event);
959 equals( livea, 0, "live secondary click" );
961 jQuery("div#nothiddendivchild").die("click");
962 jQuery("div#nothiddendiv").die("click");
963 jQuery("div").die("click");
964 jQuery("div").die("submit");
966 // Test binding with a different context
967 var clicked = 0, container = jQuery('#main')[0];
968 jQuery("#foo", container).live("click", function(e){ clicked++; });
969 jQuery("div").trigger('click');
970 jQuery("#foo").trigger('click');
971 jQuery("#main").trigger('click');
972 jQuery("body").trigger('click');
973 equals( clicked, 2, "live with a context" );
975 // Make sure the event is actually stored on the context
976 ok( jQuery.data(container, "events").live, "live with a context" );
978 // Test unbinding with a different context
979 jQuery("#foo", container).die("click");
980 jQuery("#foo").trigger('click');
981 equals( clicked, 2, "die with a context");
983 // Test binding with event data
984 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
985 jQuery("#foo").trigger("click").die("click");
987 // Test binding with trigger data
988 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
989 jQuery("#foo").trigger("click", true).die("click");
991 // Test binding with different this object
992 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
993 jQuery("#foo").trigger("click").die("click");
995 // Test binding with different this object, event data, and trigger data
996 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
997 equals( e.data, true, "live with with different this object, event data, and trigger data" );
998 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
999 equals( data, true, "live with with different this object, event data, and trigger data")
1000 }, { foo: "bar" }));
1001 jQuery("#foo").trigger("click", true).die("click");
1003 // Verify that return false prevents default action
1004 jQuery("#anchor2").live("click", function(){ return false; });
1005 var hash = window.location.hash;
1006 jQuery("#anchor2").trigger("click");
1007 equals( window.location.hash, hash, "return false worked" );
1008 jQuery("#anchor2").die("click");
1010 // Verify that .preventDefault() prevents default action
1011 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1012 var hash = window.location.hash;
1013 jQuery("#anchor2").trigger("click");
1014 equals( window.location.hash, hash, "e.preventDefault() worked" );
1015 jQuery("#anchor2").die("click");
1017 // Test binding the same handler to multiple points
1019 function callback(){ called++; return false; }
1021 jQuery("#nothiddendiv").live("click", callback);
1022 jQuery("#anchor2").live("click", callback);
1024 jQuery("#nothiddendiv").trigger("click");
1025 equals( called, 1, "Verify that only one click occurred." );
1028 jQuery("#anchor2").trigger("click");
1029 equals( called, 1, "Verify that only one click occurred." );
1031 // Make sure that only one callback is removed
1032 jQuery("#anchor2").die("click", callback);
1035 jQuery("#nothiddendiv").trigger("click");
1036 equals( called, 1, "Verify that only one click occurred." );
1039 jQuery("#anchor2").trigger("click");
1040 equals( called, 0, "Verify that no click occurred." );
1042 // Make sure that it still works if the selector is the same,
1043 // but the event type is different
1044 jQuery("#nothiddendiv").live("foo", callback);
1047 jQuery("#nothiddendiv").die("click", callback);
1050 jQuery("#nothiddendiv").trigger("click");
1051 equals( called, 0, "Verify that no click occurred." );
1054 jQuery("#nothiddendiv").trigger("foo");
1055 equals( called, 1, "Verify that one foo occurred." );
1058 jQuery("#nothiddendiv").die("foo", callback);
1060 // Make sure we don't loose the target by DOM modifications
1061 // after the bubble already reached the liveHandler
1062 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1064 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1065 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1067 jQuery("#nothiddendiv span").click();
1068 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1069 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1072 jQuery("#nothiddendivchild").die("click");
1074 // Verify that .live() ocurs and cancel buble in the same order as
1075 // we would expect .bind() and .click() without delegation
1076 var lived = 0, livee = 0;
1078 // bind one pair in one order
1079 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1080 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1082 jQuery('span#liveSpan1 a').click();
1083 equals( lived, 1, "Verify that only one first handler occurred." );
1084 equals( livee, 0, "Verify that second handler doesn't." );
1086 // and one pair in inverse
1087 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1088 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1092 jQuery('span#liveSpan2 a').click();
1093 equals( lived, 1, "Verify that only one first handler occurred." );
1094 equals( livee, 0, "Verify that second handler doesn't." );
1097 jQuery("span#liveSpan1 a").die("click")
1098 jQuery("span#liveSpan1").die("click");
1099 jQuery("span#liveSpan2 a").die("click");
1100 jQuery("span#liveSpan2").die("click");
1102 // Test this, target and currentTarget are correct
1103 jQuery('span#liveSpan1').live('click', function(e){
1104 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1105 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1106 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1109 jQuery('span#liveSpan1 a').click();
1111 jQuery('span#liveSpan1').die('click');
1113 // Work with deep selectors
1116 function clickB(){ livee++; }
1118 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1119 jQuery("#nothiddendiv div").live("click", clickB);
1120 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1122 equals( livee, 0, "No clicks, deep selector." );
1125 jQuery("#nothiddendivchild").trigger("click");
1126 equals( livee, 2, "Click, deep selector." );
1129 jQuery("#nothiddendivchild").trigger("mouseover");
1130 equals( livee, 1, "Mouseover, deep selector." );
1132 jQuery("#nothiddendiv div").die("mouseover");
1135 jQuery("#nothiddendivchild").trigger("click");
1136 equals( livee, 2, "Click, deep selector." );
1139 jQuery("#nothiddendivchild").trigger("mouseover");
1140 equals( livee, 0, "Mouseover, deep selector." );
1142 jQuery("#nothiddendiv div").die("click", clickB);
1145 jQuery("#nothiddendivchild").trigger("click");
1146 equals( livee, 1, "Click, deep selector." );
1148 jQuery("#nothiddendiv div").die("click");
1150 jQuery("#nothiddendiv div").live("blur", function(){
1151 ok( true, "Live div trigger blur." );
1154 jQuery("#nothiddendiv div").trigger("blur");
1156 jQuery("#nothiddendiv div").die("blur");
1159 test("die all bound events", function(){
1163 var div = jQuery("div#nothiddendivchild");
1165 div.live("click submit", function(){ count++; });
1168 div.trigger("click");
1169 div.trigger("submit");
1171 equals( count, 0, "Make sure no events were triggered." );
1174 test("live with multiple events", function(){
1178 var div = jQuery("div#nothiddendivchild");
1180 div.live("click submit", function(){ count++; });
1182 div.trigger("click");
1183 div.trigger("submit");
1185 equals( count, 2, "Make sure both the click and submit were triggered." );
1188 test("live with namespaces", function(){
1191 var count1 = 0, count2 = 0;
1193 jQuery("#liveSpan1").live("foo.bar", function(e){
1197 jQuery("#liveSpan1").live("foo.zed", function(e){
1201 jQuery("#liveSpan1").trigger("foo.bar");
1202 equals( count1, 1, "Got live foo.bar" );
1203 equals( count2, 0, "Got live foo.bar" );
1205 count1 = 0, count2 = 0;
1207 jQuery("#liveSpan1").trigger("foo.zed");
1208 equals( count1, 0, "Got live foo.zed" );
1209 equals( count2, 1, "Got live foo.zed" );
1212 count1 = 0, count2 = 0;
1214 jQuery("#liveSpan1").die("foo.zed");
1215 jQuery("#liveSpan1").trigger("foo.bar");
1217 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1218 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1220 count1 = 0, count2 = 0;
1222 jQuery("#liveSpan1").trigger("foo.zed");
1223 equals( count1, 0, "Got live foo.zed" );
1224 equals( count2, 0, "Got live foo.zed" );
1227 jQuery("#liveSpan1").die("foo.bar");
1229 count1 = 0, count2 = 0;
1231 jQuery("#liveSpan1").trigger("foo.bar");
1232 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1233 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1235 jQuery("#liveSpan1").trigger("foo.zed");
1236 equals( count1, 0, "Did not trigger foo.zed again" );
1237 equals( count2, 0, "Did not trigger foo.zed again" );
1240 test("live with change", function(){
1243 var selectChange = 0, checkboxChange = 0;
1245 var select = jQuery("select[name='S1']")
1246 select.live("change", function() {
1250 var checkbox = jQuery("#check2"),
1251 checkboxFunction = function(){
1254 checkbox.live("change", checkboxFunction);
1256 // test click on select
1258 // second click that changed it
1260 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1261 select.trigger("change");
1262 equals( selectChange, 1, "Change on click." );
1264 // test keys on select
1266 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1267 select.trigger("change");
1268 equals( selectChange, 1, "Change on keyup." );
1270 // test click on checkbox
1271 checkbox.trigger("change");
1272 equals( checkboxChange, 1, "Change on checkbox." );
1274 // test blur/focus on text
1275 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1276 text.live("change", function() {
1280 text.val(oldTextVal+"foo");
1281 text.trigger("change");
1282 equals( textChange, 1, "Change on text input." );
1284 text.val(oldTextVal);
1287 // test blur/focus on password
1288 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1289 password.live("change", function() {
1293 password.val(oldPasswordVal + "foo");
1294 password.trigger("change");
1295 equals( passwordChange, 1, "Change on password input." );
1297 password.val(oldPasswordVal);
1298 password.die("change");
1300 // make sure die works
1304 select.die("change");
1305 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1306 select.trigger("change");
1307 equals( selectChange, 0, "Die on click works." );
1310 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1311 select.trigger("change");
1312 equals( selectChange, 0, "Die on keyup works." );
1314 // die specific checkbox
1315 checkbox.die("change", checkboxFunction);
1316 checkbox.trigger("change");
1317 equals( checkboxChange, 1, "Die on checkbox." );
1320 test("live with submit", function() {
1321 var count1 = 0, count2 = 0;
1323 jQuery("#testForm").live("submit", function(ev) {
1325 ev.preventDefault();
1328 jQuery("body").live("submit", function(ev) {
1330 ev.preventDefault();
1333 jQuery("#testForm input[name=sub1]").submit();
1334 equals( count1, 1, "Verify form submit." );
1335 equals( count2, 1, "Verify body submit." );
1337 jQuery("#testForm").die("submit");
1338 jQuery("body").die("submit");
1341 test("live with special events", function() {
1344 jQuery.event.special.foo = {
1345 setup: function( data, namespaces, handler ) {
1346 ok( true, "Setup run." );
1348 teardown: function( namespaces ) {
1349 ok( true, "Teardown run." );
1351 add: function( handleObj ) {
1352 ok( true, "Add run." );
1354 remove: function( handleObj ) {
1355 ok( true, "Remove run." );
1357 _default: function( event ) {
1358 ok( true, "Default run." );
1363 jQuery("#liveSpan1").live("foo.a", function(e){
1364 ok( true, "Handler 1 run." );
1368 jQuery("#liveSpan1").live("foo.b", function(e){
1369 ok( true, "Handler 2 run." );
1372 // Run: Handler 1, Handler 2, Default
1373 jQuery("#liveSpan1").trigger("foo");
1375 // Run: Handler 1, Default
1376 // TODO: Namespace doesn't trigger default (?)
1377 jQuery("#liveSpan1").trigger("foo.a");
1380 jQuery("#liveSpan1").die("foo.a");
1382 // Run: Handler 2, Default
1383 jQuery("#liveSpan1").trigger("foo");
1385 // Run: remove, teardown
1386 jQuery("#liveSpan1").die("foo");
1388 delete jQuery.event.special.foo;
1391 test(".delegate()/.undelegate()", function() {
1394 var submit = 0, div = 0, livea = 0, liveb = 0;
1396 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1397 jQuery("#body").delegate("div", "click", function(){ div++; });
1398 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1399 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1401 // Nothing should trigger on the body
1402 jQuery("body").trigger("click");
1403 equals( submit, 0, "Click on body" );
1404 equals( div, 0, "Click on body" );
1405 equals( livea, 0, "Click on body" );
1406 equals( liveb, 0, "Click on body" );
1408 // This should trigger two events
1409 submit = 0, div = 0, livea = 0, liveb = 0;
1410 jQuery("div#nothiddendiv").trigger("click");
1411 equals( submit, 0, "Click on div" );
1412 equals( div, 1, "Click on div" );
1413 equals( livea, 1, "Click on div" );
1414 equals( liveb, 0, "Click on div" );
1416 // This should trigger three events (w/ bubbling)
1417 submit = 0, div = 0, livea = 0, liveb = 0;
1418 jQuery("div#nothiddendivchild").trigger("click");
1419 equals( submit, 0, "Click on inner div" );
1420 equals( div, 2, "Click on inner div" );
1421 equals( livea, 1, "Click on inner div" );
1422 equals( liveb, 1, "Click on inner div" );
1424 // This should trigger one submit
1425 submit = 0, div = 0, livea = 0, liveb = 0;
1426 jQuery("div#nothiddendivchild").trigger("submit");
1427 equals( submit, 1, "Submit on div" );
1428 equals( div, 0, "Submit on div" );
1429 equals( livea, 0, "Submit on div" );
1430 equals( liveb, 0, "Submit on div" );
1432 // Make sure no other events were removed in the process
1433 submit = 0, div = 0, livea = 0, liveb = 0;
1434 jQuery("div#nothiddendivchild").trigger("click");
1435 equals( submit, 0, "undelegate Click on inner div" );
1436 equals( div, 2, "undelegate Click on inner div" );
1437 equals( livea, 1, "undelegate Click on inner div" );
1438 equals( liveb, 1, "undelegate Click on inner div" );
1440 // Now make sure that the removal works
1441 submit = 0, div = 0, livea = 0, liveb = 0;
1442 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1443 jQuery("div#nothiddendivchild").trigger("click");
1444 equals( submit, 0, "undelegate Click on inner div" );
1445 equals( div, 2, "undelegate Click on inner div" );
1446 equals( livea, 1, "undelegate Click on inner div" );
1447 equals( liveb, 0, "undelegate Click on inner div" );
1449 // Make sure that the click wasn't removed too early
1450 submit = 0, div = 0, livea = 0, liveb = 0;
1451 jQuery("div#nothiddendiv").trigger("click");
1452 equals( submit, 0, "undelegate Click on inner div" );
1453 equals( div, 1, "undelegate Click on inner div" );
1454 equals( livea, 1, "undelegate Click on inner div" );
1455 equals( liveb, 0, "undelegate Click on inner div" );
1457 // Make sure that stopPropgation doesn't stop live events
1458 submit = 0, div = 0, livea = 0, liveb = 0;
1459 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1460 jQuery("div#nothiddendivchild").trigger("click");
1461 equals( submit, 0, "stopPropagation Click on inner div" );
1462 equals( div, 1, "stopPropagation Click on inner div" );
1463 equals( livea, 0, "stopPropagation Click on inner div" );
1464 equals( liveb, 1, "stopPropagation Click on inner div" );
1466 // Make sure click events only fire with primary click
1467 submit = 0, div = 0, livea = 0, liveb = 0;
1468 var event = jQuery.Event("click");
1470 jQuery("div#nothiddendiv").trigger(event);
1472 equals( livea, 0, "delegate secondary click" );
1474 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1475 jQuery("#body").undelegate("div#nothiddendiv", "click");
1476 jQuery("#body").undelegate("div", "click");
1477 jQuery("#body").undelegate("div", "submit");
1479 // Test binding with a different context
1480 var clicked = 0, container = jQuery('#main')[0];
1481 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1482 jQuery("div").trigger('click');
1483 jQuery("#foo").trigger('click');
1484 jQuery("#main").trigger('click');
1485 jQuery("body").trigger('click');
1486 equals( clicked, 2, "delegate with a context" );
1488 // Make sure the event is actually stored on the context
1489 ok( jQuery.data(container, "events").live, "delegate with a context" );
1491 // Test unbinding with a different context
1492 jQuery("#main").undelegate("#foo", "click");
1493 jQuery("#foo").trigger('click');
1494 equals( clicked, 2, "undelegate with a context");
1496 // Test binding with event data
1497 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1498 jQuery("#foo").trigger("click");
1499 jQuery("#body").undelegate("#foo", "click");
1501 // Test binding with trigger data
1502 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1503 jQuery("#foo").trigger("click", true);
1504 jQuery("#body").undelegate("#foo", "click");
1506 // Test binding with different this object
1507 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1508 jQuery("#foo").trigger("click");
1509 jQuery("#body").undelegate("#foo", "click");
1511 // Test binding with different this object, event data, and trigger data
1512 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1513 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1514 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1515 equals( data, true, "delegate with with different this object, event data, and trigger data")
1516 }, { foo: "bar" }));
1517 jQuery("#foo").trigger("click", true);
1518 jQuery("#body").undelegate("#foo", "click");
1520 // Verify that return false prevents default action
1521 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1522 var hash = window.location.hash;
1523 jQuery("#anchor2").trigger("click");
1524 equals( window.location.hash, hash, "return false worked" );
1525 jQuery("#body").undelegate("#anchor2", "click");
1527 // Verify that .preventDefault() prevents default action
1528 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1529 var hash = window.location.hash;
1530 jQuery("#anchor2").trigger("click");
1531 equals( window.location.hash, hash, "e.preventDefault() worked" );
1532 jQuery("#body").undelegate("#anchor2", "click");
1534 // Test binding the same handler to multiple points
1536 function callback(){ called++; return false; }
1538 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1539 jQuery("#body").delegate("#anchor2", "click", callback);
1541 jQuery("#nothiddendiv").trigger("click");
1542 equals( called, 1, "Verify that only one click occurred." );
1545 jQuery("#anchor2").trigger("click");
1546 equals( called, 1, "Verify that only one click occurred." );
1548 // Make sure that only one callback is removed
1549 jQuery("#body").undelegate("#anchor2", "click", callback);
1552 jQuery("#nothiddendiv").trigger("click");
1553 equals( called, 1, "Verify that only one click occurred." );
1556 jQuery("#anchor2").trigger("click");
1557 equals( called, 0, "Verify that no click occurred." );
1559 // Make sure that it still works if the selector is the same,
1560 // but the event type is different
1561 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1564 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1567 jQuery("#nothiddendiv").trigger("click");
1568 equals( called, 0, "Verify that no click occurred." );
1571 jQuery("#nothiddendiv").trigger("foo");
1572 equals( called, 1, "Verify that one foo occurred." );
1575 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1577 // Make sure we don't loose the target by DOM modifications
1578 // after the bubble already reached the liveHandler
1579 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1581 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1582 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1584 jQuery("#nothiddendiv span").click();
1585 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1586 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1589 jQuery("#body").undelegate("#nothiddendivchild", "click");
1591 // Verify that .live() ocurs and cancel buble in the same order as
1592 // we would expect .bind() and .click() without delegation
1593 var lived = 0, livee = 0;
1595 // bind one pair in one order
1596 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1597 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1599 jQuery('span#liveSpan1 a').click();
1600 equals( lived, 1, "Verify that only one first handler occurred." );
1601 equals( livee, 0, "Verify that second handler doesn't." );
1603 // and one pair in inverse
1604 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1605 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1609 jQuery('span#liveSpan2 a').click();
1610 equals( lived, 1, "Verify that only one first handler occurred." );
1611 equals( livee, 0, "Verify that second handler doesn't." );
1614 jQuery("#body").undelegate("click");
1616 // Test this, target and currentTarget are correct
1617 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1618 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1619 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1620 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1623 jQuery('span#liveSpan1 a').click();
1625 jQuery("#body").undelegate('span#liveSpan1', 'click');
1627 // Work with deep selectors
1630 function clickB(){ livee++; }
1632 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1633 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1634 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1636 equals( livee, 0, "No clicks, deep selector." );
1639 jQuery("#nothiddendivchild").trigger("click");
1640 equals( livee, 2, "Click, deep selector." );
1643 jQuery("#nothiddendivchild").trigger("mouseover");
1644 equals( livee, 1, "Mouseover, deep selector." );
1646 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1649 jQuery("#nothiddendivchild").trigger("click");
1650 equals( livee, 2, "Click, deep selector." );
1653 jQuery("#nothiddendivchild").trigger("mouseover");
1654 equals( livee, 0, "Mouseover, deep selector." );
1656 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1659 jQuery("#nothiddendivchild").trigger("click");
1660 equals( livee, 1, "Click, deep selector." );
1662 jQuery("#body").undelegate("#nothiddendiv div", "click");
1665 test("undelegate all bound events", function(){
1669 var div = jQuery("#body");
1671 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1674 jQuery("div#nothiddendivchild").trigger("click");
1675 jQuery("div#nothiddendivchild").trigger("submit");
1677 equals( count, 0, "Make sure no events were triggered." );
1680 test("delegate with multiple events", function(){
1684 var div = jQuery("#body");
1686 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1688 jQuery("div#nothiddendivchild").trigger("click");
1689 jQuery("div#nothiddendivchild").trigger("submit");
1691 equals( count, 2, "Make sure both the click and submit were triggered." );
1693 jQuery("#body").undelegate();
1696 test("delegate with change", function(){
1699 var selectChange = 0, checkboxChange = 0;
1701 var select = jQuery("select[name='S1']");
1702 jQuery("#body").delegate("select[name='S1']", "change", function() {
1706 var checkbox = jQuery("#check2"),
1707 checkboxFunction = function(){
1710 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1712 // test click on select
1714 // second click that changed it
1716 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1717 select.trigger("change");
1718 equals( selectChange, 1, "Change on click." );
1720 // test keys on select
1722 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1723 select.trigger("change");
1724 equals( selectChange, 1, "Change on keyup." );
1726 // test click on checkbox
1727 checkbox.trigger("change");
1728 equals( checkboxChange, 1, "Change on checkbox." );
1730 // test blur/focus on text
1731 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1732 jQuery("#body").delegate("#name", "change", function() {
1736 text.val(oldTextVal+"foo");
1737 text.trigger("change");
1738 equals( textChange, 1, "Change on text input." );
1740 text.val(oldTextVal);
1741 jQuery("#body").die("change");
1743 // test blur/focus on password
1744 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1745 jQuery("#body").delegate("#name", "change", function() {
1749 password.val(oldPasswordVal + "foo");
1750 password.trigger("change");
1751 equals( passwordChange, 1, "Change on password input." );
1753 password.val(oldPasswordVal);
1754 jQuery("#body").undelegate("#name", "change");
1756 // make sure die works
1760 jQuery("#body").undelegate("select[name='S1']", "change");
1761 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1762 select.trigger("change");
1763 equals( selectChange, 0, "Die on click works." );
1766 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1767 select.trigger("change");
1768 equals( selectChange, 0, "Die on keyup works." );
1770 // die specific checkbox
1771 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1772 checkbox.trigger("change");
1773 equals( checkboxChange, 1, "Die on checkbox." );
1776 test("delegate with submit", function() {
1777 var count1 = 0, count2 = 0;
1779 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1781 ev.preventDefault();
1784 jQuery(document).delegate("body", "submit", function(ev) {
1786 ev.preventDefault();
1789 jQuery("#testForm input[name=sub1]").submit();
1790 equals( count1, 1, "Verify form submit." );
1791 equals( count2, 1, "Verify body submit." );
1793 jQuery("#body").undelegate();
1794 jQuery(document).undelegate();
1797 test("Non DOM element events", function() {
1802 jQuery(o).bind('nonelementobj', function(e) {
1803 ok( true, "Event on non-DOM object triggered" );
1806 jQuery(o).trigger('nonelementobj');
1809 test("window resize", function() {
1812 jQuery(window).unbind();
1814 jQuery(window).bind("resize", function(){
1815 ok( true, "Resize event fired." );
1816 }).resize().unbind("resize");
1818 ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1821 test("focusin bubbles", function() {
1822 //create an input and focusin on it
1823 var input = jQuery("<input/>"), order = 0;
1825 input.prependTo("body");
1827 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1828 equals(1,order++,"focusin on the body second")
1831 input.bind("focusin.focusinBubblesTest",function(){
1832 equals(0,order++,"focusin on the element first")
1838 jQuery("body").unbind("focusin.focusinBubblesTest");
1842 test("jQuery(function($) {})", function() {
1844 jQuery(function($) {
1845 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");
1850 test("event properties", function() {
1852 jQuery("#simon1").click(function(event) {
1853 ok( event.timeStamp, "assert event.timeStamp is present" );