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( typeof events, "function", "'events' expando is a function on plain objects." );
463 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
464 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
467 jQuery(obj).trigger("test");
469 jQuery(obj).unbind("test");
472 jQuery(obj).trigger("test");
474 // Make sure it doesn't complain when no events are found
475 jQuery(obj).unbind("test");
478 test("unbind(type)", function() {
481 var $elem = jQuery("#firstp"),
485 ok( false, message );
488 message = "unbind passing function";
489 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
491 message = "unbind all from event";
492 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
494 message = "unbind all";
495 $elem.bind('error1', error).unbind().triggerHandler('error1');
497 message = "unbind many with function";
498 $elem.bind('error1 error2',error)
499 .unbind('error1 error2', error )
500 .trigger('error1').triggerHandler('error2');
502 message = "unbind many"; // #3538
503 $elem.bind('error1 error2',error)
504 .unbind('error1 error2')
505 .trigger('error1').triggerHandler('error2');
507 message = "unbind without a type or handler";
508 $elem.bind("error1 error2.test",error)
510 .trigger("error1").triggerHandler("error2");
513 test("unbind(eventObject)", function() {
516 var $elem = jQuery("#firstp"),
519 function assert( expected ){
521 $elem.trigger('foo').triggerHandler('bar');
522 equals( num, expected, "Check the right handlers are triggered" );
526 // This handler shouldn't be unbound
527 .bind('foo', function(){
530 .bind('foo', function(e){
535 .bind('bar', function(){
549 test("hover()", function() {
551 handler1 = function( event ) { ++times; },
552 handler2 = function( event ) { ++times; };
555 .hover(handler1, handler2)
556 .mouseenter().mouseleave()
557 .unbind("mouseenter", handler1)
558 .unbind("mouseleave", handler2)
560 .mouseenter().mouseleave()
561 .unbind("mouseenter mouseleave", handler1)
562 .mouseenter().mouseleave();
564 equals( times, 4, "hover handlers fired" );
567 test("trigger() shortcuts", function() {
569 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
570 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
571 equals( close.length, 0, "Context element does not exist, length must be zero" );
572 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
576 jQuery("#check1").click(function() {
577 ok( true, "click event handler for checkbox gets fired twice, see #815" );
581 jQuery('#firstp')[0].onclick = function(event) {
584 jQuery('#firstp').click();
585 equals( counter, 1, "Check that click, triggers onclick event handler also" );
587 var clickCounter = 0;
588 jQuery('#simon1')[0].onclick = function(event) {
591 jQuery('#simon1').click();
592 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
594 jQuery('<img />').load(function(){
595 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
599 test("trigger() bubbling", function() {
602 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
604 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
605 jQuery("html").bind("click", function(e){ html++; });
606 jQuery("body").bind("click", function(e){ body++; });
607 jQuery("#main").bind("click", function(e){ main++; });
608 jQuery("#ap").bind("click", function(){ ap++; return false; });
610 jQuery("html").trigger("click");
611 equals( doc, 1, "HTML bubble" );
612 equals( html, 1, "HTML bubble" );
614 jQuery("body").trigger("click");
615 equals( doc, 2, "Body bubble" );
616 equals( html, 2, "Body bubble" );
617 equals( body, 1, "Body bubble" );
619 jQuery("#main").trigger("click");
620 equals( doc, 3, "Main bubble" );
621 equals( html, 3, "Main bubble" );
622 equals( body, 2, "Main bubble" );
623 equals( main, 1, "Main bubble" );
625 jQuery("#ap").trigger("click");
626 equals( doc, 3, "ap bubble" );
627 equals( html, 3, "ap bubble" );
628 equals( body, 2, "ap bubble" );
629 equals( main, 1, "ap bubble" );
630 equals( ap, 1, "ap bubble" );
633 test("trigger(type, [data], [fn])", function() {
636 var handler = function(event, a, b, c) {
637 equals( event.type, "click", "check passed data" );
638 equals( a, 1, "check passed data" );
639 equals( b, "2", "check passed data" );
640 equals( c, "abc", "check passed data" );
644 var $elem = jQuery("#firstp");
646 // Simulate a "native" click
647 $elem[0].click = function(){
648 ok( true, "Native call was triggered" );
651 // Triggers handlrs and native
653 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
655 // Simulate a "native" click
656 $elem[0].click = function(){
657 ok( false, "Native call was triggered" );
660 // Trigger only the handlers (no native)
662 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
666 jQuery('#form input:first').hide().trigger('focus');
670 ok( pass, "Trigger focus on hidden element" );
674 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
678 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
680 var form = jQuery("<form action=''></form>").appendTo("body");
682 // Make sure it can be prevented locally
683 form.submit(function(){
684 ok( true, "Local bind still works." );
689 form.trigger("submit");
691 form.unbind("submit");
693 jQuery(document).submit(function(){
694 ok( true, "Make sure bubble works up to document." );
699 form.trigger("submit");
701 jQuery(document).unbind("submit");
706 test("jQuery.Event.currentTarget", function(){
709 test("trigger(eventObject, [data], [fn])", function() {
712 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
713 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
715 var event = jQuery.Event("noNew");
716 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
717 equals( event.type, "noNew", "Verify its type" );
719 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
720 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
721 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
723 event.preventDefault();
724 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
725 event.stopPropagation();
726 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
728 event.isPropagationStopped = function(){ return false };
729 event.stopImmediatePropagation();
730 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
731 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
733 $parent.bind('foo',function(e){
735 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
736 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
737 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
738 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
741 // test with an event object
742 event = new jQuery.Event("foo");
743 event.secret = 'boo!';
744 $child.trigger(event);
746 // test with a literal object
747 $child.trigger({type:'foo', secret:'boo!'});
752 ok( false, "This assertion shouldn't be reached");
755 $parent.bind('foo', error );
757 $child.bind('foo',function(e, a, b, c ){
758 equals( arguments.length, 4, "Check arguments length");
759 equals( a, 1, "Check first custom argument");
760 equals( b, 2, "Check second custom argument");
761 equals( c, 3, "Check third custom argument");
763 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
764 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
765 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
768 e.stopImmediatePropagation();
773 // We should add this back in when we want to test the order
774 // in which event handlers are iterated.
775 //$child.bind('foo', error );
777 event = new jQuery.Event("foo");
778 $child.trigger( event, [1,2,3] ).unbind();
779 equals( event.result, "result", "Check event.result attribute");
781 // Will error if it bubbles
782 $child.triggerHandler('foo');
785 $parent.unbind().remove();
788 test("jQuery.Event.currentTarget", function(){
792 $elem = jQuery('<button>a</button>').click(function(e){
793 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
797 $elem.trigger('click');
803 test("toggle(Function, Function, ...)", function() {
807 fn1 = function(e) { count++; },
808 fn2 = function(e) { count--; },
809 preventDefault = function(e) { e.preventDefault() },
810 link = jQuery('#mark');
811 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
812 equals( count, 1, "Check for toggle(fn, fn)" );
814 jQuery("#firstp").toggle(function () {
815 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
816 }, function() {}).trigger("click", [ 1, 2, 3 ]);
819 jQuery("#simon1").one("click", function() {
820 ok( true, "Execute event only once" );
821 jQuery(this).toggle(function() {
822 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
824 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
827 }).click().click().click();
842 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
844 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
846 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
848 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
850 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
852 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
854 $div.unbind('click',fns[0]);
855 var data = jQuery.data( $div[0], 'events' );
856 ok( !data, "Unbinding one function from toggle unbinds them all");
858 // Test Multi-Toggles
860 $div = jQuery("<div/>");
861 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
863 same( a, [1], "Check that a click worked." );
865 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
867 same( a, [1,2], "Check that a click worked with a second toggle." );
868 same( b, [1], "Check that a click worked with a second toggle." );
871 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
872 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
875 test(".live()/.die()", function() {
878 var submit = 0, div = 0, livea = 0, liveb = 0;
880 jQuery("div").live("submit", function(){ submit++; return false; });
881 jQuery("div").live("click", function(){ div++; });
882 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
883 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
885 // Nothing should trigger on the body
886 jQuery("body").trigger("click");
887 equals( submit, 0, "Click on body" );
888 equals( div, 0, "Click on body" );
889 equals( livea, 0, "Click on body" );
890 equals( liveb, 0, "Click on body" );
892 // This should trigger two events
893 submit = 0, div = 0, livea = 0, liveb = 0;
894 jQuery("div#nothiddendiv").trigger("click");
895 equals( submit, 0, "Click on div" );
896 equals( div, 1, "Click on div" );
897 equals( livea, 1, "Click on div" );
898 equals( liveb, 0, "Click on div" );
900 // This should trigger three events (w/ bubbling)
901 submit = 0, div = 0, livea = 0, liveb = 0;
902 jQuery("div#nothiddendivchild").trigger("click");
903 equals( submit, 0, "Click on inner div" );
904 equals( div, 2, "Click on inner div" );
905 equals( livea, 1, "Click on inner div" );
906 equals( liveb, 1, "Click on inner div" );
908 // This should trigger one submit
909 submit = 0, div = 0, livea = 0, liveb = 0;
910 jQuery("div#nothiddendivchild").trigger("submit");
911 equals( submit, 1, "Submit on div" );
912 equals( div, 0, "Submit on div" );
913 equals( livea, 0, "Submit on div" );
914 equals( liveb, 0, "Submit on div" );
916 // Make sure no other events were removed in the process
917 submit = 0, div = 0, livea = 0, liveb = 0;
918 jQuery("div#nothiddendivchild").trigger("click");
919 equals( submit, 0, "die Click on inner div" );
920 equals( div, 2, "die Click on inner div" );
921 equals( livea, 1, "die Click on inner div" );
922 equals( liveb, 1, "die Click on inner div" );
924 // Now make sure that the removal works
925 submit = 0, div = 0, livea = 0, liveb = 0;
926 jQuery("div#nothiddendivchild").die("click");
927 jQuery("div#nothiddendivchild").trigger("click");
928 equals( submit, 0, "die Click on inner div" );
929 equals( div, 2, "die Click on inner div" );
930 equals( livea, 1, "die Click on inner div" );
931 equals( liveb, 0, "die Click on inner div" );
933 // Make sure that the click wasn't removed too early
934 submit = 0, div = 0, livea = 0, liveb = 0;
935 jQuery("div#nothiddendiv").trigger("click");
936 equals( submit, 0, "die Click on inner div" );
937 equals( div, 1, "die Click on inner div" );
938 equals( livea, 1, "die Click on inner div" );
939 equals( liveb, 0, "die Click on inner div" );
941 // Make sure that stopPropgation doesn't stop live events
942 submit = 0, div = 0, livea = 0, liveb = 0;
943 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
944 jQuery("div#nothiddendivchild").trigger("click");
945 equals( submit, 0, "stopPropagation Click on inner div" );
946 equals( div, 1, "stopPropagation Click on inner div" );
947 equals( livea, 0, "stopPropagation Click on inner div" );
948 equals( liveb, 1, "stopPropagation Click on inner div" );
950 // Make sure click events only fire with primary click
951 submit = 0, div = 0, livea = 0, liveb = 0;
952 var event = jQuery.Event("click");
954 jQuery("div#nothiddendiv").trigger(event);
956 equals( livea, 0, "live secondary click" );
958 jQuery("div#nothiddendivchild").die("click");
959 jQuery("div#nothiddendiv").die("click");
960 jQuery("div").die("click");
961 jQuery("div").die("submit");
963 // Test binding with a different context
964 var clicked = 0, container = jQuery('#main')[0];
965 jQuery("#foo", container).live("click", function(e){ clicked++; });
966 jQuery("div").trigger('click');
967 jQuery("#foo").trigger('click');
968 jQuery("#main").trigger('click');
969 jQuery("body").trigger('click');
970 equals( clicked, 2, "live with a context" );
972 // Make sure the event is actually stored on the context
973 ok( jQuery.data(container, "events").live, "live with a context" );
975 // Test unbinding with a different context
976 jQuery("#foo", container).die("click");
977 jQuery("#foo").trigger('click');
978 equals( clicked, 2, "die with a context");
980 // Test binding with event data
981 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
982 jQuery("#foo").trigger("click").die("click");
984 // Test binding with trigger data
985 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
986 jQuery("#foo").trigger("click", true).die("click");
988 // Test binding with different this object
989 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
990 jQuery("#foo").trigger("click").die("click");
992 // Test binding with different this object, event data, and trigger data
993 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
994 equals( e.data, true, "live with with different this object, event data, and trigger data" );
995 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
996 equals( data, true, "live with with different this object, event data, and trigger data")
998 jQuery("#foo").trigger("click", true).die("click");
1000 // Verify that return false prevents default action
1001 jQuery("#anchor2").live("click", function(){ return false; });
1002 var hash = window.location.hash;
1003 jQuery("#anchor2").trigger("click");
1004 equals( window.location.hash, hash, "return false worked" );
1005 jQuery("#anchor2").die("click");
1007 // Verify that .preventDefault() prevents default action
1008 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1009 var hash = window.location.hash;
1010 jQuery("#anchor2").trigger("click");
1011 equals( window.location.hash, hash, "e.preventDefault() worked" );
1012 jQuery("#anchor2").die("click");
1014 // Test binding the same handler to multiple points
1016 function callback(){ called++; return false; }
1018 jQuery("#nothiddendiv").live("click", callback);
1019 jQuery("#anchor2").live("click", callback);
1021 jQuery("#nothiddendiv").trigger("click");
1022 equals( called, 1, "Verify that only one click occurred." );
1025 jQuery("#anchor2").trigger("click");
1026 equals( called, 1, "Verify that only one click occurred." );
1028 // Make sure that only one callback is removed
1029 jQuery("#anchor2").die("click", callback);
1032 jQuery("#nothiddendiv").trigger("click");
1033 equals( called, 1, "Verify that only one click occurred." );
1036 jQuery("#anchor2").trigger("click");
1037 equals( called, 0, "Verify that no click occurred." );
1039 // Make sure that it still works if the selector is the same,
1040 // but the event type is different
1041 jQuery("#nothiddendiv").live("foo", callback);
1044 jQuery("#nothiddendiv").die("click", callback);
1047 jQuery("#nothiddendiv").trigger("click");
1048 equals( called, 0, "Verify that no click occurred." );
1051 jQuery("#nothiddendiv").trigger("foo");
1052 equals( called, 1, "Verify that one foo occurred." );
1055 jQuery("#nothiddendiv").die("foo", callback);
1057 // Make sure we don't loose the target by DOM modifications
1058 // after the bubble already reached the liveHandler
1059 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1061 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1062 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1064 jQuery("#nothiddendiv span").click();
1065 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1066 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1069 jQuery("#nothiddendivchild").die("click");
1071 // Verify that .live() ocurs and cancel buble in the same order as
1072 // we would expect .bind() and .click() without delegation
1073 var lived = 0, livee = 0;
1075 // bind one pair in one order
1076 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1077 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1079 jQuery('span#liveSpan1 a').click();
1080 equals( lived, 1, "Verify that only one first handler occurred." );
1081 equals( livee, 0, "Verify that second handler doesn't." );
1083 // and one pair in inverse
1084 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1085 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1089 jQuery('span#liveSpan2 a').click();
1090 equals( lived, 1, "Verify that only one first handler occurred." );
1091 equals( livee, 0, "Verify that second handler doesn't." );
1094 jQuery("span#liveSpan1 a").die("click")
1095 jQuery("span#liveSpan1").die("click");
1096 jQuery("span#liveSpan2 a").die("click");
1097 jQuery("span#liveSpan2").die("click");
1099 // Test this, target and currentTarget are correct
1100 jQuery('span#liveSpan1').live('click', function(e){
1101 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1102 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1103 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1106 jQuery('span#liveSpan1 a').click();
1108 jQuery('span#liveSpan1').die('click');
1110 // Work with deep selectors
1113 function clickB(){ livee++; }
1115 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1116 jQuery("#nothiddendiv div").live("click", clickB);
1117 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1119 equals( livee, 0, "No clicks, deep selector." );
1122 jQuery("#nothiddendivchild").trigger("click");
1123 equals( livee, 2, "Click, deep selector." );
1126 jQuery("#nothiddendivchild").trigger("mouseover");
1127 equals( livee, 1, "Mouseover, deep selector." );
1129 jQuery("#nothiddendiv div").die("mouseover");
1132 jQuery("#nothiddendivchild").trigger("click");
1133 equals( livee, 2, "Click, deep selector." );
1136 jQuery("#nothiddendivchild").trigger("mouseover");
1137 equals( livee, 0, "Mouseover, deep selector." );
1139 jQuery("#nothiddendiv div").die("click", clickB);
1142 jQuery("#nothiddendivchild").trigger("click");
1143 equals( livee, 1, "Click, deep selector." );
1145 jQuery("#nothiddendiv div").die("click");
1147 jQuery("#nothiddendiv div").live("blur", function(){
1148 ok( true, "Live div trigger blur." );
1151 jQuery("#nothiddendiv div").trigger("blur");
1153 jQuery("#nothiddendiv div").die("blur");
1156 test("die all bound events", function(){
1160 var div = jQuery("div#nothiddendivchild");
1162 div.live("click submit", function(){ count++; });
1165 div.trigger("click");
1166 div.trigger("submit");
1168 equals( count, 0, "Make sure no events were triggered." );
1171 test("live with multiple events", function(){
1175 var div = jQuery("div#nothiddendivchild");
1177 div.live("click submit", function(){ count++; });
1179 div.trigger("click");
1180 div.trigger("submit");
1182 equals( count, 2, "Make sure both the click and submit were triggered." );
1185 test("live with namespaces", function(){
1188 var count1 = 0, count2 = 0;
1190 jQuery("#liveSpan1").live("foo.bar", function(e){
1194 jQuery("#liveSpan1").live("foo.zed", function(e){
1198 jQuery("#liveSpan1").trigger("foo.bar");
1199 equals( count1, 1, "Got live foo.bar" );
1200 equals( count2, 0, "Got live foo.bar" );
1202 count1 = 0, count2 = 0;
1204 jQuery("#liveSpan1").trigger("foo.zed");
1205 equals( count1, 0, "Got live foo.zed" );
1206 equals( count2, 1, "Got live foo.zed" );
1209 count1 = 0, count2 = 0;
1211 jQuery("#liveSpan1").die("foo.zed");
1212 jQuery("#liveSpan1").trigger("foo.bar");
1214 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1215 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1217 count1 = 0, count2 = 0;
1219 jQuery("#liveSpan1").trigger("foo.zed");
1220 equals( count1, 0, "Got live foo.zed" );
1221 equals( count2, 0, "Got live foo.zed" );
1224 jQuery("#liveSpan1").die("foo.bar");
1226 count1 = 0, count2 = 0;
1228 jQuery("#liveSpan1").trigger("foo.bar");
1229 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1230 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1232 jQuery("#liveSpan1").trigger("foo.zed");
1233 equals( count1, 0, "Did not trigger foo.zed again" );
1234 equals( count2, 0, "Did not trigger foo.zed again" );
1237 test("live with change", function(){
1238 var selectChange = 0, checkboxChange = 0;
1240 var select = jQuery("select[name='S1']")
1241 select.live("change", function() {
1245 var checkbox = jQuery("#check2"),
1246 checkboxFunction = function(){
1249 checkbox.live("change", checkboxFunction);
1251 // test click on select
1253 // second click that changed it
1255 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1256 select.trigger("change");
1257 equals( selectChange, 1, "Change on click." );
1259 // test keys on select
1261 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1262 select.trigger("change");
1263 equals( selectChange, 1, "Change on keyup." );
1265 // test click on checkbox
1266 checkbox.trigger("change");
1267 equals( checkboxChange, 1, "Change on checkbox." );
1269 // test before activate on radio
1271 // test blur/focus on textarea
1272 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1273 textarea.live("change", function() {
1277 textarea.val(oldVal + "foo");
1278 textarea.trigger("change");
1279 equals( textareaChange, 1, "Change on textarea." );
1281 textarea.val(oldVal);
1282 textarea.die("change");
1284 // test blur/focus on text
1285 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1286 text.live("change", function() {
1290 text.val(oldVal+"foo");
1291 text.trigger("change");
1292 equals( textChange, 1, "Change on text input." );
1294 text.val(oldTextVal);
1297 // test blur/focus on password
1298 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1299 password.live("change", function() {
1303 password.val(oldPasswordVal + "foo");
1304 password.trigger("change");
1305 equals( passwordChange, 1, "Change on password input." );
1307 password.val(oldPasswordVal);
1308 password.die("change");
1310 // make sure die works
1314 select.die("change");
1315 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1316 select.trigger("change");
1317 equals( selectChange, 0, "Die on click works." );
1320 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1321 select.trigger("change");
1322 equals( selectChange, 0, "Die on keyup works." );
1324 // die specific checkbox
1325 checkbox.die("change", checkboxFunction);
1326 checkbox.trigger("change");
1327 equals( checkboxChange, 1, "Die on checkbox." );
1330 test("live with submit", function() {
1331 var count1 = 0, count2 = 0;
1333 jQuery("#testForm").live("submit", function(ev) {
1335 ev.preventDefault();
1338 jQuery("body").live("submit", function(ev) {
1340 ev.preventDefault();
1343 jQuery("#testForm input[name=sub1]").submit();
1344 equals( count1, 1, "Verify form submit." );
1345 equals( count2, 1, "Verify body submit." );
1347 jQuery("#testForm").die("submit");
1348 jQuery("body").die("submit");
1351 test("live with special events", function() {
1354 jQuery.event.special.foo = {
1355 setup: function( data, namespaces, handler ) {
1356 ok( true, "Setup run." );
1358 teardown: function( namespaces ) {
1359 ok( true, "Teardown run." );
1361 add: function( handleObj ) {
1362 ok( true, "Add run." );
1364 remove: function( handleObj ) {
1365 ok( true, "Remove run." );
1367 _default: function( event ) {
1368 ok( true, "Default run." );
1373 jQuery("#liveSpan1").live("foo.a", function(e){
1374 ok( true, "Handler 1 run." );
1378 jQuery("#liveSpan1").live("foo.b", function(e){
1379 ok( true, "Handler 2 run." );
1382 // Run: Handler 1, Handler 2, Default
1383 jQuery("#liveSpan1").trigger("foo");
1385 // Run: Handler 1, Default
1386 // TODO: Namespace doesn't trigger default (?)
1387 jQuery("#liveSpan1").trigger("foo.a");
1390 jQuery("#liveSpan1").die("foo.a");
1392 // Run: Handler 2, Default
1393 jQuery("#liveSpan1").trigger("foo");
1395 // Run: remove, teardown
1396 jQuery("#liveSpan1").die("foo");
1398 delete jQuery.event.special.foo;
1401 test(".delegate()/.undelegate()", function() {
1404 var submit = 0, div = 0, livea = 0, liveb = 0;
1406 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1407 jQuery("#body").delegate("div", "click", function(){ div++; });
1408 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1409 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1411 // Nothing should trigger on the body
1412 jQuery("body").trigger("click");
1413 equals( submit, 0, "Click on body" );
1414 equals( div, 0, "Click on body" );
1415 equals( livea, 0, "Click on body" );
1416 equals( liveb, 0, "Click on body" );
1418 // This should trigger two events
1419 submit = 0, div = 0, livea = 0, liveb = 0;
1420 jQuery("div#nothiddendiv").trigger("click");
1421 equals( submit, 0, "Click on div" );
1422 equals( div, 1, "Click on div" );
1423 equals( livea, 1, "Click on div" );
1424 equals( liveb, 0, "Click on div" );
1426 // This should trigger three events (w/ bubbling)
1427 submit = 0, div = 0, livea = 0, liveb = 0;
1428 jQuery("div#nothiddendivchild").trigger("click");
1429 equals( submit, 0, "Click on inner div" );
1430 equals( div, 2, "Click on inner div" );
1431 equals( livea, 1, "Click on inner div" );
1432 equals( liveb, 1, "Click on inner div" );
1434 // This should trigger one submit
1435 submit = 0, div = 0, livea = 0, liveb = 0;
1436 jQuery("div#nothiddendivchild").trigger("submit");
1437 equals( submit, 1, "Submit on div" );
1438 equals( div, 0, "Submit on div" );
1439 equals( livea, 0, "Submit on div" );
1440 equals( liveb, 0, "Submit on div" );
1442 // Make sure no other events were removed in the process
1443 submit = 0, div = 0, livea = 0, liveb = 0;
1444 jQuery("div#nothiddendivchild").trigger("click");
1445 equals( submit, 0, "undelegate Click on inner div" );
1446 equals( div, 2, "undelegate Click on inner div" );
1447 equals( livea, 1, "undelegate Click on inner div" );
1448 equals( liveb, 1, "undelegate Click on inner div" );
1450 // Now make sure that the removal works
1451 submit = 0, div = 0, livea = 0, liveb = 0;
1452 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1453 jQuery("div#nothiddendivchild").trigger("click");
1454 equals( submit, 0, "undelegate Click on inner div" );
1455 equals( div, 2, "undelegate Click on inner div" );
1456 equals( livea, 1, "undelegate Click on inner div" );
1457 equals( liveb, 0, "undelegate Click on inner div" );
1459 // Make sure that the click wasn't removed too early
1460 submit = 0, div = 0, livea = 0, liveb = 0;
1461 jQuery("div#nothiddendiv").trigger("click");
1462 equals( submit, 0, "undelegate Click on inner div" );
1463 equals( div, 1, "undelegate Click on inner div" );
1464 equals( livea, 1, "undelegate Click on inner div" );
1465 equals( liveb, 0, "undelegate Click on inner div" );
1467 // Make sure that stopPropgation doesn't stop live events
1468 submit = 0, div = 0, livea = 0, liveb = 0;
1469 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1470 jQuery("div#nothiddendivchild").trigger("click");
1471 equals( submit, 0, "stopPropagation Click on inner div" );
1472 equals( div, 1, "stopPropagation Click on inner div" );
1473 equals( livea, 0, "stopPropagation Click on inner div" );
1474 equals( liveb, 1, "stopPropagation Click on inner div" );
1476 // Make sure click events only fire with primary click
1477 submit = 0, div = 0, livea = 0, liveb = 0;
1478 var event = jQuery.Event("click");
1480 jQuery("div#nothiddendiv").trigger(event);
1482 equals( livea, 0, "delegate secondary click" );
1484 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1485 jQuery("#body").undelegate("div#nothiddendiv", "click");
1486 jQuery("#body").undelegate("div", "click");
1487 jQuery("#body").undelegate("div", "submit");
1489 // Test binding with a different context
1490 var clicked = 0, container = jQuery('#main')[0];
1491 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1492 jQuery("div").trigger('click');
1493 jQuery("#foo").trigger('click');
1494 jQuery("#main").trigger('click');
1495 jQuery("body").trigger('click');
1496 equals( clicked, 2, "delegate with a context" );
1498 // Make sure the event is actually stored on the context
1499 ok( jQuery.data(container, "events").live, "delegate with a context" );
1501 // Test unbinding with a different context
1502 jQuery("#main").undelegate("#foo", "click");
1503 jQuery("#foo").trigger('click');
1504 equals( clicked, 2, "undelegate with a context");
1506 // Test binding with event data
1507 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1508 jQuery("#foo").trigger("click");
1509 jQuery("#body").undelegate("#foo", "click");
1511 // Test binding with trigger data
1512 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1513 jQuery("#foo").trigger("click", true);
1514 jQuery("#body").undelegate("#foo", "click");
1516 // Test binding with different this object
1517 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1518 jQuery("#foo").trigger("click");
1519 jQuery("#body").undelegate("#foo", "click");
1521 // Test binding with different this object, event data, and trigger data
1522 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1523 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1524 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1525 equals( data, true, "delegate with with different this object, event data, and trigger data")
1526 }, { foo: "bar" }));
1527 jQuery("#foo").trigger("click", true);
1528 jQuery("#body").undelegate("#foo", "click");
1530 // Verify that return false prevents default action
1531 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1532 var hash = window.location.hash;
1533 jQuery("#anchor2").trigger("click");
1534 equals( window.location.hash, hash, "return false worked" );
1535 jQuery("#body").undelegate("#anchor2", "click");
1537 // Verify that .preventDefault() prevents default action
1538 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1539 var hash = window.location.hash;
1540 jQuery("#anchor2").trigger("click");
1541 equals( window.location.hash, hash, "e.preventDefault() worked" );
1542 jQuery("#body").undelegate("#anchor2", "click");
1544 // Test binding the same handler to multiple points
1546 function callback(){ called++; return false; }
1548 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1549 jQuery("#body").delegate("#anchor2", "click", callback);
1551 jQuery("#nothiddendiv").trigger("click");
1552 equals( called, 1, "Verify that only one click occurred." );
1555 jQuery("#anchor2").trigger("click");
1556 equals( called, 1, "Verify that only one click occurred." );
1558 // Make sure that only one callback is removed
1559 jQuery("#body").undelegate("#anchor2", "click", callback);
1562 jQuery("#nothiddendiv").trigger("click");
1563 equals( called, 1, "Verify that only one click occurred." );
1566 jQuery("#anchor2").trigger("click");
1567 equals( called, 0, "Verify that no click occurred." );
1569 // Make sure that it still works if the selector is the same,
1570 // but the event type is different
1571 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1574 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1577 jQuery("#nothiddendiv").trigger("click");
1578 equals( called, 0, "Verify that no click occurred." );
1581 jQuery("#nothiddendiv").trigger("foo");
1582 equals( called, 1, "Verify that one foo occurred." );
1585 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1587 // Make sure we don't loose the target by DOM modifications
1588 // after the bubble already reached the liveHandler
1589 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1591 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1592 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1594 jQuery("#nothiddendiv span").click();
1595 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1596 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1599 jQuery("#body").undelegate("#nothiddendivchild", "click");
1601 // Verify that .live() ocurs and cancel buble in the same order as
1602 // we would expect .bind() and .click() without delegation
1603 var lived = 0, livee = 0;
1605 // bind one pair in one order
1606 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1607 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1609 jQuery('span#liveSpan1 a').click();
1610 equals( lived, 1, "Verify that only one first handler occurred." );
1611 equals( livee, 0, "Verify that second handler doesn't." );
1613 // and one pair in inverse
1614 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1615 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1619 jQuery('span#liveSpan2 a').click();
1620 equals( lived, 1, "Verify that only one first handler occurred." );
1621 equals( livee, 0, "Verify that second handler doesn't." );
1624 jQuery("#body").undelegate("click");
1626 // Test this, target and currentTarget are correct
1627 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1628 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1629 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1630 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1633 jQuery('span#liveSpan1 a').click();
1635 jQuery("#body").undelegate('span#liveSpan1', 'click');
1637 // Work with deep selectors
1640 function clickB(){ livee++; }
1642 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1643 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1644 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1646 equals( livee, 0, "No clicks, deep selector." );
1649 jQuery("#nothiddendivchild").trigger("click");
1650 equals( livee, 2, "Click, deep selector." );
1653 jQuery("#nothiddendivchild").trigger("mouseover");
1654 equals( livee, 1, "Mouseover, deep selector." );
1656 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1659 jQuery("#nothiddendivchild").trigger("click");
1660 equals( livee, 2, "Click, deep selector." );
1663 jQuery("#nothiddendivchild").trigger("mouseover");
1664 equals( livee, 0, "Mouseover, deep selector." );
1666 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1669 jQuery("#nothiddendivchild").trigger("click");
1670 equals( livee, 1, "Click, deep selector." );
1672 jQuery("#body").undelegate("#nothiddendiv div", "click");
1675 test("undelegate all bound events", function(){
1679 var div = jQuery("#body");
1681 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1684 jQuery("div#nothiddendivchild").trigger("click");
1685 jQuery("div#nothiddendivchild").trigger("submit");
1687 equals( count, 0, "Make sure no events were triggered." );
1690 test("delegate with multiple events", function(){
1694 var div = jQuery("#body");
1696 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1698 jQuery("div#nothiddendivchild").trigger("click");
1699 jQuery("div#nothiddendivchild").trigger("submit");
1701 equals( count, 2, "Make sure both the click and submit were triggered." );
1703 jQuery("#body").undelegate();
1706 test("delegate with change", function(){
1707 var selectChange = 0, checkboxChange = 0;
1709 var select = jQuery("select[name='S1']");
1710 jQuery("#body").delegate("select[name='S1']", "change", function() {
1714 var checkbox = jQuery("#check2"),
1715 checkboxFunction = function(){
1718 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1720 // test click on select
1722 // second click that changed it
1724 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1725 select.trigger("change");
1726 equals( selectChange, 1, "Change on click." );
1728 // test keys on select
1730 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1731 select.trigger("change");
1732 equals( selectChange, 1, "Change on keyup." );
1734 // test click on checkbox
1735 checkbox.trigger("change");
1736 equals( checkboxChange, 1, "Change on checkbox." );
1738 // test before activate on radio
1740 // test blur/focus on textarea
1741 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1742 jQuery("#body").delegate("#area1", "change", function() {
1746 textarea.val(oldVal + "foo");
1747 textarea.trigger("change");
1748 equals( textareaChange, 1, "Change on textarea." );
1750 textarea.val(oldVal);
1751 jQuery("#body").undelegate("#area1", "change");
1753 // test blur/focus on text
1754 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1755 jQuery("#body").delegate("#name", "change", function() {
1759 text.val(oldVal+"foo");
1760 text.trigger("change");
1761 equals( textChange, 1, "Change on text input." );
1763 text.val(oldTextVal);
1764 jQuery("#body").die("change");
1766 // test blur/focus on password
1767 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1768 jQuery("#body").delegate("#name", "change", function() {
1772 password.val(oldPasswordVal + "foo");
1773 password.trigger("change");
1774 equals( passwordChange, 1, "Change on password input." );
1776 password.val(oldPasswordVal);
1777 jQuery("#body").undelegate("#name", "change");
1779 // make sure die works
1783 jQuery("#body").undelegate("select[name='S1']", "change");
1784 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1785 select.trigger("change");
1786 equals( selectChange, 0, "Die on click works." );
1789 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1790 select.trigger("change");
1791 equals( selectChange, 0, "Die on keyup works." );
1793 // die specific checkbox
1794 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1795 checkbox.trigger("change");
1796 equals( checkboxChange, 1, "Die on checkbox." );
1799 test("delegate with submit", function() {
1800 var count1 = 0, count2 = 0;
1802 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1804 ev.preventDefault();
1807 jQuery(document).delegate("body", "submit", function(ev) {
1809 ev.preventDefault();
1812 jQuery("#testForm input[name=sub1]").submit();
1813 equals( count1, 1, "Verify form submit." );
1814 equals( count2, 1, "Verify body submit." );
1816 jQuery("#body").undelegate();
1817 jQuery(document).undelegate();
1820 test("Non DOM element events", function() {
1825 jQuery(o).bind('nonelementobj', function(e) {
1826 ok( true, "Event on non-DOM object triggered" );
1829 jQuery(o).trigger('nonelementobj');
1833 test("jQuery(function($) {})", function() {
1835 jQuery(function($) {
1836 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");
1841 test("event properties", function() {
1843 jQuery("#simon1").click(function(event) {
1844 ok( event.timeStamp, "assert event.timeStamp is present" );