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 ok( jQuery(obj).data("events"), "Object has events bound." );
463 jQuery(obj).trigger("test");
465 jQuery(obj).unbind("test");
468 jQuery(obj).trigger("test");
470 // Make sure it doesn't complain when no events are found
471 jQuery(obj).unbind("test");
474 test("unbind(type)", function() {
477 var $elem = jQuery("#firstp"),
481 ok( false, message );
484 message = "unbind passing function";
485 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
487 message = "unbind all from event";
488 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
490 message = "unbind all";
491 $elem.bind('error1', error).unbind().triggerHandler('error1');
493 message = "unbind many with function";
494 $elem.bind('error1 error2',error)
495 .unbind('error1 error2', error )
496 .trigger('error1').triggerHandler('error2');
498 message = "unbind many"; // #3538
499 $elem.bind('error1 error2',error)
500 .unbind('error1 error2')
501 .trigger('error1').triggerHandler('error2');
503 message = "unbind without a type or handler";
504 $elem.bind("error1 error2.test",error)
506 .trigger("error1").triggerHandler("error2");
509 test("unbind(eventObject)", function() {
512 var $elem = jQuery("#firstp"),
515 function assert( expected ){
517 $elem.trigger('foo').triggerHandler('bar');
518 equals( num, expected, "Check the right handlers are triggered" );
522 // This handler shouldn't be unbound
523 .bind('foo', function(){
526 .bind('foo', function(e){
531 .bind('bar', function(){
545 test("hover()", function() {
547 handler1 = function( event ) { ++times; },
548 handler2 = function( event ) { ++times; };
551 .hover(handler1, handler2)
552 .mouseenter().mouseleave()
553 .unbind("mouseenter", handler1)
554 .unbind("mouseleave", handler2)
556 .mouseenter().mouseleave()
557 .unbind("mouseenter mouseleave", handler1)
558 .mouseenter().mouseleave();
560 equals( times, 4, "hover handlers fired" );
563 test("trigger() shortcuts", function() {
565 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
566 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
567 equals( close.length, 0, "Context element does not exist, length must be zero" );
568 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
572 jQuery("#check1").click(function() {
573 ok( true, "click event handler for checkbox gets fired twice, see #815" );
577 jQuery('#firstp')[0].onclick = function(event) {
580 jQuery('#firstp').click();
581 equals( counter, 1, "Check that click, triggers onclick event handler also" );
583 var clickCounter = 0;
584 jQuery('#simon1')[0].onclick = function(event) {
587 jQuery('#simon1').click();
588 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
590 jQuery('<img />').load(function(){
591 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
595 test("trigger() bubbling", function() {
598 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
600 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
601 jQuery("html").bind("click", function(e){ html++; });
602 jQuery("body").bind("click", function(e){ body++; });
603 jQuery("#main").bind("click", function(e){ main++; });
604 jQuery("#ap").bind("click", function(){ ap++; return false; });
606 jQuery("html").trigger("click");
607 equals( doc, 1, "HTML bubble" );
608 equals( html, 1, "HTML bubble" );
610 jQuery("body").trigger("click");
611 equals( doc, 2, "Body bubble" );
612 equals( html, 2, "Body bubble" );
613 equals( body, 1, "Body bubble" );
615 jQuery("#main").trigger("click");
616 equals( doc, 3, "Main bubble" );
617 equals( html, 3, "Main bubble" );
618 equals( body, 2, "Main bubble" );
619 equals( main, 1, "Main bubble" );
621 jQuery("#ap").trigger("click");
622 equals( doc, 3, "ap bubble" );
623 equals( html, 3, "ap bubble" );
624 equals( body, 2, "ap bubble" );
625 equals( main, 1, "ap bubble" );
626 equals( ap, 1, "ap bubble" );
629 test("trigger(type, [data], [fn])", function() {
632 var handler = function(event, a, b, c) {
633 equals( event.type, "click", "check passed data" );
634 equals( a, 1, "check passed data" );
635 equals( b, "2", "check passed data" );
636 equals( c, "abc", "check passed data" );
640 var $elem = jQuery("#firstp");
642 // Simulate a "native" click
643 $elem[0].click = function(){
644 ok( true, "Native call was triggered" );
647 // Triggers handlrs and native
649 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
651 // Simulate a "native" click
652 $elem[0].click = function(){
653 ok( false, "Native call was triggered" );
656 // Trigger only the handlers (no native)
658 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
662 jQuery('#form input:first').hide().trigger('focus');
666 ok( pass, "Trigger focus on hidden element" );
670 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
674 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
676 var form = jQuery("<form action=''></form>").appendTo("body");
678 // Make sure it can be prevented locally
679 form.submit(function(){
680 ok( true, "Local bind still works." );
685 form.trigger("submit");
687 form.unbind("submit");
689 jQuery(document).submit(function(){
690 ok( true, "Make sure bubble works up to document." );
695 form.trigger("submit");
697 jQuery(document).unbind("submit");
702 test("jQuery.Event.currentTarget", function(){
705 test("trigger(eventObject, [data], [fn])", function() {
708 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
709 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
711 var event = jQuery.Event("noNew");
712 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
713 equals( event.type, "noNew", "Verify its type" );
715 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
716 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
717 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
719 event.preventDefault();
720 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
721 event.stopPropagation();
722 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
724 event.isPropagationStopped = function(){ return false };
725 event.stopImmediatePropagation();
726 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
727 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
729 $parent.bind('foo',function(e){
731 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
732 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
733 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
734 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
737 // test with an event object
738 event = new jQuery.Event("foo");
739 event.secret = 'boo!';
740 $child.trigger(event);
742 // test with a literal object
743 $child.trigger({type:'foo', secret:'boo!'});
748 ok( false, "This assertion shouldn't be reached");
751 $parent.bind('foo', error );
753 $child.bind('foo',function(e, a, b, c ){
754 equals( arguments.length, 4, "Check arguments length");
755 equals( a, 1, "Check first custom argument");
756 equals( b, 2, "Check second custom argument");
757 equals( c, 3, "Check third custom argument");
759 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
760 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
761 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
764 e.stopImmediatePropagation();
769 // We should add this back in when we want to test the order
770 // in which event handlers are iterated.
771 //$child.bind('foo', error );
773 event = new jQuery.Event("foo");
774 $child.trigger( event, [1,2,3] ).unbind();
775 equals( event.result, "result", "Check event.result attribute");
777 // Will error if it bubbles
778 $child.triggerHandler('foo');
781 $parent.unbind().remove();
784 test("jQuery.Event.currentTarget", function(){
788 $elem = jQuery('<button>a</button>').click(function(e){
789 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
793 $elem.trigger('click');
799 test("toggle(Function, Function, ...)", function() {
803 fn1 = function(e) { count++; },
804 fn2 = function(e) { count--; },
805 preventDefault = function(e) { e.preventDefault() },
806 link = jQuery('#mark');
807 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
808 equals( count, 1, "Check for toggle(fn, fn)" );
810 jQuery("#firstp").toggle(function () {
811 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
812 }, function() {}).trigger("click", [ 1, 2, 3 ]);
815 jQuery("#simon1").one("click", function() {
816 ok( true, "Execute event only once" );
817 jQuery(this).toggle(function() {
818 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
820 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
823 }).click().click().click();
838 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
840 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
842 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
844 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
846 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
848 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
850 $div.unbind('click',fns[0]);
851 var data = jQuery.data( $div[0], 'events' );
852 ok( !data, "Unbinding one function from toggle unbinds them all");
854 // Test Multi-Toggles
856 $div = jQuery("<div/>");
857 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
859 same( a, [1], "Check that a click worked." );
861 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
863 same( a, [1,2], "Check that a click worked with a second toggle." );
864 same( b, [1], "Check that a click worked with a second toggle." );
867 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
868 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
871 test(".live()/.die()", function() {
874 var submit = 0, div = 0, livea = 0, liveb = 0;
876 jQuery("div").live("submit", function(){ submit++; return false; });
877 jQuery("div").live("click", function(){ div++; });
878 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
879 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
881 // Nothing should trigger on the body
882 jQuery("body").trigger("click");
883 equals( submit, 0, "Click on body" );
884 equals( div, 0, "Click on body" );
885 equals( livea, 0, "Click on body" );
886 equals( liveb, 0, "Click on body" );
888 // This should trigger two events
889 submit = 0, div = 0, livea = 0, liveb = 0;
890 jQuery("div#nothiddendiv").trigger("click");
891 equals( submit, 0, "Click on div" );
892 equals( div, 1, "Click on div" );
893 equals( livea, 1, "Click on div" );
894 equals( liveb, 0, "Click on div" );
896 // This should trigger three events (w/ bubbling)
897 submit = 0, div = 0, livea = 0, liveb = 0;
898 jQuery("div#nothiddendivchild").trigger("click");
899 equals( submit, 0, "Click on inner div" );
900 equals( div, 2, "Click on inner div" );
901 equals( livea, 1, "Click on inner div" );
902 equals( liveb, 1, "Click on inner div" );
904 // This should trigger one submit
905 submit = 0, div = 0, livea = 0, liveb = 0;
906 jQuery("div#nothiddendivchild").trigger("submit");
907 equals( submit, 1, "Submit on div" );
908 equals( div, 0, "Submit on div" );
909 equals( livea, 0, "Submit on div" );
910 equals( liveb, 0, "Submit on div" );
912 // Make sure no other events were removed in the process
913 submit = 0, div = 0, livea = 0, liveb = 0;
914 jQuery("div#nothiddendivchild").trigger("click");
915 equals( submit, 0, "die Click on inner div" );
916 equals( div, 2, "die Click on inner div" );
917 equals( livea, 1, "die Click on inner div" );
918 equals( liveb, 1, "die Click on inner div" );
920 // Now make sure that the removal works
921 submit = 0, div = 0, livea = 0, liveb = 0;
922 jQuery("div#nothiddendivchild").die("click");
923 jQuery("div#nothiddendivchild").trigger("click");
924 equals( submit, 0, "die Click on inner div" );
925 equals( div, 2, "die Click on inner div" );
926 equals( livea, 1, "die Click on inner div" );
927 equals( liveb, 0, "die Click on inner div" );
929 // Make sure that the click wasn't removed too early
930 submit = 0, div = 0, livea = 0, liveb = 0;
931 jQuery("div#nothiddendiv").trigger("click");
932 equals( submit, 0, "die Click on inner div" );
933 equals( div, 1, "die Click on inner div" );
934 equals( livea, 1, "die Click on inner div" );
935 equals( liveb, 0, "die Click on inner div" );
937 // Make sure that stopPropgation doesn't stop live events
938 submit = 0, div = 0, livea = 0, liveb = 0;
939 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
940 jQuery("div#nothiddendivchild").trigger("click");
941 equals( submit, 0, "stopPropagation Click on inner div" );
942 equals( div, 1, "stopPropagation Click on inner div" );
943 equals( livea, 0, "stopPropagation Click on inner div" );
944 equals( liveb, 1, "stopPropagation Click on inner div" );
946 // Make sure click events only fire with primary click
947 submit = 0, div = 0, livea = 0, liveb = 0;
948 var event = jQuery.Event("click");
950 jQuery("div#nothiddendiv").trigger(event);
952 equals( livea, 0, "live secondary click" );
954 jQuery("div#nothiddendivchild").die("click");
955 jQuery("div#nothiddendiv").die("click");
956 jQuery("div").die("click");
957 jQuery("div").die("submit");
959 // Test binding with a different context
960 var clicked = 0, container = jQuery('#main')[0];
961 jQuery("#foo", container).live("click", function(e){ clicked++; });
962 jQuery("div").trigger('click');
963 jQuery("#foo").trigger('click');
964 jQuery("#main").trigger('click');
965 jQuery("body").trigger('click');
966 equals( clicked, 2, "live with a context" );
968 // Make sure the event is actually stored on the context
969 ok( jQuery.data(container, "events").live, "live with a context" );
971 // Test unbinding with a different context
972 jQuery("#foo", container).die("click");
973 jQuery("#foo").trigger('click');
974 equals( clicked, 2, "die with a context");
976 // Test binding with event data
977 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
978 jQuery("#foo").trigger("click").die("click");
980 // Test binding with trigger data
981 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
982 jQuery("#foo").trigger("click", true).die("click");
984 // Test binding with different this object
985 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
986 jQuery("#foo").trigger("click").die("click");
988 // Test binding with different this object, event data, and trigger data
989 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
990 equals( e.data, true, "live with with different this object, event data, and trigger data" );
991 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
992 equals( data, true, "live with with different this object, event data, and trigger data")
994 jQuery("#foo").trigger("click", true).die("click");
996 // Verify that return false prevents default action
997 jQuery("#anchor2").live("click", function(){ return false; });
998 var hash = window.location.hash;
999 jQuery("#anchor2").trigger("click");
1000 equals( window.location.hash, hash, "return false worked" );
1001 jQuery("#anchor2").die("click");
1003 // Verify that .preventDefault() prevents default action
1004 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1005 var hash = window.location.hash;
1006 jQuery("#anchor2").trigger("click");
1007 equals( window.location.hash, hash, "e.preventDefault() worked" );
1008 jQuery("#anchor2").die("click");
1010 // Test binding the same handler to multiple points
1012 function callback(){ called++; return false; }
1014 jQuery("#nothiddendiv").live("click", callback);
1015 jQuery("#anchor2").live("click", callback);
1017 jQuery("#nothiddendiv").trigger("click");
1018 equals( called, 1, "Verify that only one click occurred." );
1021 jQuery("#anchor2").trigger("click");
1022 equals( called, 1, "Verify that only one click occurred." );
1024 // Make sure that only one callback is removed
1025 jQuery("#anchor2").die("click", callback);
1028 jQuery("#nothiddendiv").trigger("click");
1029 equals( called, 1, "Verify that only one click occurred." );
1032 jQuery("#anchor2").trigger("click");
1033 equals( called, 0, "Verify that no click occurred." );
1035 // Make sure that it still works if the selector is the same,
1036 // but the event type is different
1037 jQuery("#nothiddendiv").live("foo", callback);
1040 jQuery("#nothiddendiv").die("click", callback);
1043 jQuery("#nothiddendiv").trigger("click");
1044 equals( called, 0, "Verify that no click occurred." );
1047 jQuery("#nothiddendiv").trigger("foo");
1048 equals( called, 1, "Verify that one foo occurred." );
1051 jQuery("#nothiddendiv").die("foo", callback);
1053 // Make sure we don't loose the target by DOM modifications
1054 // after the bubble already reached the liveHandler
1055 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1057 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1058 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1060 jQuery("#nothiddendiv span").click();
1061 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1062 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1065 jQuery("#nothiddendivchild").die("click");
1067 // Verify that .live() ocurs and cancel buble in the same order as
1068 // we would expect .bind() and .click() without delegation
1069 var lived = 0, livee = 0;
1071 // bind one pair in one order
1072 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1073 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1075 jQuery('span#liveSpan1 a').click();
1076 equals( lived, 1, "Verify that only one first handler occurred." );
1077 equals( livee, 0, "Verify that second handler doesn't." );
1079 // and one pair in inverse
1080 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1081 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1085 jQuery('span#liveSpan2 a').click();
1086 equals( lived, 1, "Verify that only one first handler occurred." );
1087 equals( livee, 0, "Verify that second handler doesn't." );
1090 jQuery("span#liveSpan1 a").die("click")
1091 jQuery("span#liveSpan1").die("click");
1092 jQuery("span#liveSpan2 a").die("click");
1093 jQuery("span#liveSpan2").die("click");
1095 // Test this, target and currentTarget are correct
1096 jQuery('span#liveSpan1').live('click', function(e){
1097 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1098 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1099 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1102 jQuery('span#liveSpan1 a').click();
1104 jQuery('span#liveSpan1').die('click');
1106 // Work with deep selectors
1109 function clickB(){ livee++; }
1111 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1112 jQuery("#nothiddendiv div").live("click", clickB);
1113 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1115 equals( livee, 0, "No clicks, deep selector." );
1118 jQuery("#nothiddendivchild").trigger("click");
1119 equals( livee, 2, "Click, deep selector." );
1122 jQuery("#nothiddendivchild").trigger("mouseover");
1123 equals( livee, 1, "Mouseover, deep selector." );
1125 jQuery("#nothiddendiv div").die("mouseover");
1128 jQuery("#nothiddendivchild").trigger("click");
1129 equals( livee, 2, "Click, deep selector." );
1132 jQuery("#nothiddendivchild").trigger("mouseover");
1133 equals( livee, 0, "Mouseover, deep selector." );
1135 jQuery("#nothiddendiv div").die("click", clickB);
1138 jQuery("#nothiddendivchild").trigger("click");
1139 equals( livee, 1, "Click, deep selector." );
1141 jQuery("#nothiddendiv div").die("click");
1143 jQuery("#nothiddendiv div").live("blur", function(){
1144 ok( true, "Live div trigger blur." );
1147 jQuery("#nothiddendiv div").trigger("blur");
1149 jQuery("#nothiddendiv div").die("blur");
1152 test("die all bound events", function(){
1156 var div = jQuery("div#nothiddendivchild");
1158 div.live("click submit", function(){ count++; });
1161 div.trigger("click");
1162 div.trigger("submit");
1164 equals( count, 0, "Make sure no events were triggered." );
1167 test("live with multiple events", function(){
1171 var div = jQuery("div#nothiddendivchild");
1173 div.live("click submit", function(){ count++; });
1175 div.trigger("click");
1176 div.trigger("submit");
1178 equals( count, 2, "Make sure both the click and submit were triggered." );
1181 test("live with namespaces", function(){
1184 var count1 = 0, count2 = 0;
1186 jQuery("#liveSpan1").live("foo.bar", function(e){
1190 jQuery("#liveSpan1").live("foo.zed", function(e){
1194 jQuery("#liveSpan1").trigger("foo.bar");
1195 equals( count1, 1, "Got live foo.bar" );
1196 equals( count2, 0, "Got live foo.bar" );
1198 count1 = 0, count2 = 0;
1200 jQuery("#liveSpan1").trigger("foo.zed");
1201 equals( count1, 0, "Got live foo.zed" );
1202 equals( count2, 1, "Got live foo.zed" );
1205 count1 = 0, count2 = 0;
1207 jQuery("#liveSpan1").die("foo.zed");
1208 jQuery("#liveSpan1").trigger("foo.bar");
1210 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1211 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1213 count1 = 0, count2 = 0;
1215 jQuery("#liveSpan1").trigger("foo.zed");
1216 equals( count1, 0, "Got live foo.zed" );
1217 equals( count2, 0, "Got live foo.zed" );
1220 jQuery("#liveSpan1").die("foo.bar");
1222 count1 = 0, count2 = 0;
1224 jQuery("#liveSpan1").trigger("foo.bar");
1225 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1226 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1228 jQuery("#liveSpan1").trigger("foo.zed");
1229 equals( count1, 0, "Did not trigger foo.zed again" );
1230 equals( count2, 0, "Did not trigger foo.zed again" );
1233 test("live with change", function(){
1234 var selectChange = 0, checkboxChange = 0;
1236 var select = jQuery("select[name='S1']")
1237 select.live("change", function() {
1241 var checkbox = jQuery("#check2"),
1242 checkboxFunction = function(){
1245 checkbox.live("change", checkboxFunction);
1247 // test click on select
1249 // second click that changed it
1251 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1252 select.trigger("change");
1253 equals( selectChange, 1, "Change on click." );
1255 // test keys on select
1257 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1258 select.trigger("change");
1259 equals( selectChange, 1, "Change on keyup." );
1261 // test click on checkbox
1262 checkbox.trigger("change");
1263 equals( checkboxChange, 1, "Change on checkbox." );
1265 // test before activate on radio
1267 // test blur/focus on textarea
1268 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1269 textarea.live("change", function() {
1273 textarea.val(oldVal + "foo");
1274 textarea.trigger("change");
1275 equals( textareaChange, 1, "Change on textarea." );
1277 textarea.val(oldVal);
1278 textarea.die("change");
1280 // test blur/focus on text
1281 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1282 text.live("change", function() {
1286 text.val(oldVal+"foo");
1287 text.trigger("change");
1288 equals( textChange, 1, "Change on text input." );
1290 text.val(oldTextVal);
1293 // test blur/focus on password
1294 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1295 password.live("change", function() {
1299 password.val(oldPasswordVal + "foo");
1300 password.trigger("change");
1301 equals( passwordChange, 1, "Change on password input." );
1303 password.val(oldPasswordVal);
1304 password.die("change");
1306 // make sure die works
1310 select.die("change");
1311 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1312 select.trigger("change");
1313 equals( selectChange, 0, "Die on click works." );
1316 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1317 select.trigger("change");
1318 equals( selectChange, 0, "Die on keyup works." );
1320 // die specific checkbox
1321 checkbox.die("change", checkboxFunction);
1322 checkbox.trigger("change");
1323 equals( checkboxChange, 1, "Die on checkbox." );
1326 test("live with submit", function() {
1327 var count1 = 0, count2 = 0;
1329 jQuery("#testForm").live("submit", function(ev) {
1331 ev.preventDefault();
1334 jQuery("body").live("submit", function(ev) {
1336 ev.preventDefault();
1339 jQuery("#testForm input[name=sub1]").submit();
1340 equals( count1, 1, "Verify form submit." );
1341 equals( count2, 1, "Verify body submit." );
1343 jQuery("#testForm").die("submit");
1344 jQuery("body").die("submit");
1347 test("live with special events", function() {
1350 jQuery.event.special.foo = {
1351 setup: function( data, namespaces, handler ) {
1352 ok( true, "Setup run." );
1354 teardown: function( namespaces ) {
1355 ok( true, "Teardown run." );
1357 add: function( handleObj ) {
1358 ok( true, "Add run." );
1360 remove: function( handleObj ) {
1361 ok( true, "Remove run." );
1363 _default: function( event ) {
1364 ok( true, "Default run." );
1369 jQuery("#liveSpan1").live("foo.a", function(e){
1370 ok( true, "Handler 1 run." );
1374 jQuery("#liveSpan1").live("foo.b", function(e){
1375 ok( true, "Handler 2 run." );
1378 // Run: Handler 1, Handler 2, Default
1379 jQuery("#liveSpan1").trigger("foo");
1381 // Run: Handler 1, Default
1382 // TODO: Namespace doesn't trigger default (?)
1383 jQuery("#liveSpan1").trigger("foo.a");
1386 jQuery("#liveSpan1").die("foo.a");
1388 // Run: Handler 2, Default
1389 jQuery("#liveSpan1").trigger("foo");
1391 // Run: remove, teardown
1392 jQuery("#liveSpan1").die("foo");
1394 delete jQuery.event.special.foo;
1397 test(".delegate()/.undelegate()", function() {
1400 var submit = 0, div = 0, livea = 0, liveb = 0;
1402 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1403 jQuery("#body").delegate("div", "click", function(){ div++; });
1404 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1405 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1407 // Nothing should trigger on the body
1408 jQuery("body").trigger("click");
1409 equals( submit, 0, "Click on body" );
1410 equals( div, 0, "Click on body" );
1411 equals( livea, 0, "Click on body" );
1412 equals( liveb, 0, "Click on body" );
1414 // This should trigger two events
1415 submit = 0, div = 0, livea = 0, liveb = 0;
1416 jQuery("div#nothiddendiv").trigger("click");
1417 equals( submit, 0, "Click on div" );
1418 equals( div, 1, "Click on div" );
1419 equals( livea, 1, "Click on div" );
1420 equals( liveb, 0, "Click on div" );
1422 // This should trigger three events (w/ bubbling)
1423 submit = 0, div = 0, livea = 0, liveb = 0;
1424 jQuery("div#nothiddendivchild").trigger("click");
1425 equals( submit, 0, "Click on inner div" );
1426 equals( div, 2, "Click on inner div" );
1427 equals( livea, 1, "Click on inner div" );
1428 equals( liveb, 1, "Click on inner div" );
1430 // This should trigger one submit
1431 submit = 0, div = 0, livea = 0, liveb = 0;
1432 jQuery("div#nothiddendivchild").trigger("submit");
1433 equals( submit, 1, "Submit on div" );
1434 equals( div, 0, "Submit on div" );
1435 equals( livea, 0, "Submit on div" );
1436 equals( liveb, 0, "Submit on div" );
1438 // Make sure no other events were removed in the process
1439 submit = 0, div = 0, livea = 0, liveb = 0;
1440 jQuery("div#nothiddendivchild").trigger("click");
1441 equals( submit, 0, "undelegate Click on inner div" );
1442 equals( div, 2, "undelegate Click on inner div" );
1443 equals( livea, 1, "undelegate Click on inner div" );
1444 equals( liveb, 1, "undelegate Click on inner div" );
1446 // Now make sure that the removal works
1447 submit = 0, div = 0, livea = 0, liveb = 0;
1448 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1449 jQuery("div#nothiddendivchild").trigger("click");
1450 equals( submit, 0, "undelegate Click on inner div" );
1451 equals( div, 2, "undelegate Click on inner div" );
1452 equals( livea, 1, "undelegate Click on inner div" );
1453 equals( liveb, 0, "undelegate Click on inner div" );
1455 // Make sure that the click wasn't removed too early
1456 submit = 0, div = 0, livea = 0, liveb = 0;
1457 jQuery("div#nothiddendiv").trigger("click");
1458 equals( submit, 0, "undelegate Click on inner div" );
1459 equals( div, 1, "undelegate Click on inner div" );
1460 equals( livea, 1, "undelegate Click on inner div" );
1461 equals( liveb, 0, "undelegate Click on inner div" );
1463 // Make sure that stopPropgation doesn't stop live events
1464 submit = 0, div = 0, livea = 0, liveb = 0;
1465 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1466 jQuery("div#nothiddendivchild").trigger("click");
1467 equals( submit, 0, "stopPropagation Click on inner div" );
1468 equals( div, 1, "stopPropagation Click on inner div" );
1469 equals( livea, 0, "stopPropagation Click on inner div" );
1470 equals( liveb, 1, "stopPropagation Click on inner div" );
1472 // Make sure click events only fire with primary click
1473 submit = 0, div = 0, livea = 0, liveb = 0;
1474 var event = jQuery.Event("click");
1476 jQuery("div#nothiddendiv").trigger(event);
1478 equals( livea, 0, "delegate secondary click" );
1480 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1481 jQuery("#body").undelegate("div#nothiddendiv", "click");
1482 jQuery("#body").undelegate("div", "click");
1483 jQuery("#body").undelegate("div", "submit");
1485 // Test binding with a different context
1486 var clicked = 0, container = jQuery('#main')[0];
1487 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1488 jQuery("div").trigger('click');
1489 jQuery("#foo").trigger('click');
1490 jQuery("#main").trigger('click');
1491 jQuery("body").trigger('click');
1492 equals( clicked, 2, "delegate with a context" );
1494 // Make sure the event is actually stored on the context
1495 ok( jQuery.data(container, "events").live, "delegate with a context" );
1497 // Test unbinding with a different context
1498 jQuery("#main").undelegate("#foo", "click");
1499 jQuery("#foo").trigger('click');
1500 equals( clicked, 2, "undelegate with a context");
1502 // Test binding with event data
1503 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1504 jQuery("#foo").trigger("click");
1505 jQuery("#body").undelegate("#foo", "click");
1507 // Test binding with trigger data
1508 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1509 jQuery("#foo").trigger("click", true);
1510 jQuery("#body").undelegate("#foo", "click");
1512 // Test binding with different this object
1513 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1514 jQuery("#foo").trigger("click");
1515 jQuery("#body").undelegate("#foo", "click");
1517 // Test binding with different this object, event data, and trigger data
1518 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1519 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1520 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1521 equals( data, true, "delegate with with different this object, event data, and trigger data")
1522 }, { foo: "bar" }));
1523 jQuery("#foo").trigger("click", true);
1524 jQuery("#body").undelegate("#foo", "click");
1526 // Verify that return false prevents default action
1527 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1528 var hash = window.location.hash;
1529 jQuery("#anchor2").trigger("click");
1530 equals( window.location.hash, hash, "return false worked" );
1531 jQuery("#body").undelegate("#anchor2", "click");
1533 // Verify that .preventDefault() prevents default action
1534 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1535 var hash = window.location.hash;
1536 jQuery("#anchor2").trigger("click");
1537 equals( window.location.hash, hash, "e.preventDefault() worked" );
1538 jQuery("#body").undelegate("#anchor2", "click");
1540 // Test binding the same handler to multiple points
1542 function callback(){ called++; return false; }
1544 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1545 jQuery("#body").delegate("#anchor2", "click", callback);
1547 jQuery("#nothiddendiv").trigger("click");
1548 equals( called, 1, "Verify that only one click occurred." );
1551 jQuery("#anchor2").trigger("click");
1552 equals( called, 1, "Verify that only one click occurred." );
1554 // Make sure that only one callback is removed
1555 jQuery("#body").undelegate("#anchor2", "click", callback);
1558 jQuery("#nothiddendiv").trigger("click");
1559 equals( called, 1, "Verify that only one click occurred." );
1562 jQuery("#anchor2").trigger("click");
1563 equals( called, 0, "Verify that no click occurred." );
1565 // Make sure that it still works if the selector is the same,
1566 // but the event type is different
1567 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1570 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1573 jQuery("#nothiddendiv").trigger("click");
1574 equals( called, 0, "Verify that no click occurred." );
1577 jQuery("#nothiddendiv").trigger("foo");
1578 equals( called, 1, "Verify that one foo occurred." );
1581 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1583 // Make sure we don't loose the target by DOM modifications
1584 // after the bubble already reached the liveHandler
1585 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1587 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1588 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1590 jQuery("#nothiddendiv span").click();
1591 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1592 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1595 jQuery("#body").undelegate("#nothiddendivchild", "click");
1597 // Verify that .live() ocurs and cancel buble in the same order as
1598 // we would expect .bind() and .click() without delegation
1599 var lived = 0, livee = 0;
1601 // bind one pair in one order
1602 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1603 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1605 jQuery('span#liveSpan1 a').click();
1606 equals( lived, 1, "Verify that only one first handler occurred." );
1607 equals( livee, 0, "Verify that second handler doesn't." );
1609 // and one pair in inverse
1610 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1611 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1615 jQuery('span#liveSpan2 a').click();
1616 equals( lived, 1, "Verify that only one first handler occurred." );
1617 equals( livee, 0, "Verify that second handler doesn't." );
1620 jQuery("#body").undelegate("click");
1622 // Test this, target and currentTarget are correct
1623 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1624 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1625 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1626 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1629 jQuery('span#liveSpan1 a').click();
1631 jQuery("#body").undelegate('span#liveSpan1', 'click');
1633 // Work with deep selectors
1636 function clickB(){ livee++; }
1638 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1639 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1640 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1642 equals( livee, 0, "No clicks, deep selector." );
1645 jQuery("#nothiddendivchild").trigger("click");
1646 equals( livee, 2, "Click, deep selector." );
1649 jQuery("#nothiddendivchild").trigger("mouseover");
1650 equals( livee, 1, "Mouseover, deep selector." );
1652 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1655 jQuery("#nothiddendivchild").trigger("click");
1656 equals( livee, 2, "Click, deep selector." );
1659 jQuery("#nothiddendivchild").trigger("mouseover");
1660 equals( livee, 0, "Mouseover, deep selector." );
1662 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1665 jQuery("#nothiddendivchild").trigger("click");
1666 equals( livee, 1, "Click, deep selector." );
1668 jQuery("#body").undelegate("#nothiddendiv div", "click");
1671 test("undelegate all bound events", function(){
1675 var div = jQuery("#body");
1677 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1680 jQuery("div#nothiddendivchild").trigger("click");
1681 jQuery("div#nothiddendivchild").trigger("submit");
1683 equals( count, 0, "Make sure no events were triggered." );
1686 test("delegate with multiple events", function(){
1690 var div = jQuery("#body");
1692 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1694 jQuery("div#nothiddendivchild").trigger("click");
1695 jQuery("div#nothiddendivchild").trigger("submit");
1697 equals( count, 2, "Make sure both the click and submit were triggered." );
1699 jQuery("#body").undelegate();
1702 test("delegate with change", function(){
1703 var selectChange = 0, checkboxChange = 0;
1705 var select = jQuery("select[name='S1']");
1706 jQuery("#body").delegate("select[name='S1']", "change", function() {
1710 var checkbox = jQuery("#check2"),
1711 checkboxFunction = function(){
1714 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1716 // test click on select
1718 // second click that changed it
1720 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1721 select.trigger("change");
1722 equals( selectChange, 1, "Change on click." );
1724 // test keys on select
1726 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1727 select.trigger("change");
1728 equals( selectChange, 1, "Change on keyup." );
1730 // test click on checkbox
1731 checkbox.trigger("change");
1732 equals( checkboxChange, 1, "Change on checkbox." );
1734 // test before activate on radio
1736 // test blur/focus on textarea
1737 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1738 jQuery("#body").delegate("#area1", "change", function() {
1742 textarea.val(oldVal + "foo");
1743 textarea.trigger("change");
1744 equals( textareaChange, 1, "Change on textarea." );
1746 textarea.val(oldVal);
1747 jQuery("#body").undelegate("#area1", "change");
1749 // test blur/focus on text
1750 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1751 jQuery("#body").delegate("#name", "change", function() {
1755 text.val(oldVal+"foo");
1756 text.trigger("change");
1757 equals( textChange, 1, "Change on text input." );
1759 text.val(oldTextVal);
1760 jQuery("#body").die("change");
1762 // test blur/focus on password
1763 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1764 jQuery("#body").delegate("#name", "change", function() {
1768 password.val(oldPasswordVal + "foo");
1769 password.trigger("change");
1770 equals( passwordChange, 1, "Change on password input." );
1772 password.val(oldPasswordVal);
1773 jQuery("#body").undelegate("#name", "change");
1775 // make sure die works
1779 jQuery("#body").undelegate("select[name='S1']", "change");
1780 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1781 select.trigger("change");
1782 equals( selectChange, 0, "Die on click works." );
1785 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1786 select.trigger("change");
1787 equals( selectChange, 0, "Die on keyup works." );
1789 // die specific checkbox
1790 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1791 checkbox.trigger("change");
1792 equals( checkboxChange, 1, "Die on checkbox." );
1795 test("delegate with submit", function() {
1796 var count1 = 0, count2 = 0;
1798 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1800 ev.preventDefault();
1803 jQuery(document).delegate("body", "submit", function(ev) {
1805 ev.preventDefault();
1808 jQuery("#testForm input[name=sub1]").submit();
1809 equals( count1, 1, "Verify form submit." );
1810 equals( count2, 1, "Verify body submit." );
1812 jQuery("#body").undelegate();
1813 jQuery(document).undelegate();
1816 test("Non DOM element events", function() {
1821 jQuery(o).bind('nonelementobj', function(e) {
1822 ok( true, "Event on non-DOM object triggered" );
1825 jQuery(o).trigger('nonelementobj');
1829 test("jQuery(function($) {})", function() {
1831 jQuery(function($) {
1832 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");
1837 test("event properties", function() {
1839 jQuery("#simon1").click(function(event) {
1840 ok( event.timeStamp, "assert event.timeStamp is present" );