3 test("null or undefined handler", function() {
5 // Supports Fixes bug #7229
8 jQuery("#firstp").click(null);
10 ok(true, "Passing a null handler will not throw an exception");
16 jQuery("#firstp").click(undefined);
18 ok(true, "Passing an undefined handler will not throw an exception");
23 test("bind(), with data", function() {
25 var handler = function(event) {
26 ok( event.data, "bind() with data, check passed data exists" );
27 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
29 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
31 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
34 test("click(), with data", function() {
36 var handler = function(event) {
37 ok( event.data, "bind() with data, check passed data exists" );
38 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
40 jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
42 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
45 test("bind(), with data, trigger with data", function() {
47 var handler = function(event, data) {
48 ok( event.data, "check passed data exists" );
49 equals( event.data.foo, "bar", "Check value of passed data" );
50 ok( data, "Check trigger data" );
51 equals( data.bar, "foo", "Check value of trigger data" );
53 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
56 test("bind(), multiple events at once", function() {
60 var handler = function(event) {
61 if (event.type == "click")
63 else if (event.type == "mouseover")
64 mouseoverCounter += 1;
66 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
67 equals( clickCounter, 1, "bind() with multiple events at once" );
68 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
71 test("bind(), multiple events at once and namespaces", function() {
76 var div = jQuery("<div/>").bind("focusin.a", function(e) {
77 equals( e.type, cur, "Verify right single event was fired." );
81 div.trigger("focusin.a");
83 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
84 equals( e.type, cur, "Verify right multi event was fired." );
85 equals( e.data, obj, "Make sure the data came in correctly." );
92 div.trigger("mouseover");
94 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
95 equals( e.type, cur, "Verify right multi event was fired." );
99 div.trigger("focusin.a");
102 div.trigger("focusout.b");
105 test("bind(), namespace with special add", function() {
108 var div = jQuery("<div/>").bind("test", function(e) {
109 ok( true, "Test event fired." );
114 jQuery.event.special.test = {
115 _default: function(e) {
116 equals( this, document, "Make sure we're at the top of the chain." );
117 equals( e.type, "test", "And that we're still dealing with a test event." );
118 equals( e.target, div[0], "And that the target is correct." );
121 teardown: function(){
122 ok(true, "Teardown called.");
124 add: function( handleObj ) {
125 var handler = handleObj.handler;
126 handleObj.handler = function(e) {
128 handler.apply( this, arguments );
132 ok(true, "Remove called.");
136 div.bind("test.a", {x: 1}, function(e) {
137 ok( !!e.xyz, "Make sure that the data is getting passed through." );
138 equals( e.data.x, 1, "Make sure data is attached properly." );
141 div.bind("test.b", {x: 2}, function(e) {
142 ok( !!e.xyz, "Make sure that the data is getting passed through." );
143 equals( e.data.x, 2, "Make sure data is attached properly." );
150 div.trigger("test.a");
153 div.trigger("test.b");
158 div = jQuery("<div/>").bind("test", function(e) {
159 ok( true, "Test event fired." );
163 div.appendTo("#main").remove();
165 delete jQuery.event.special.test;
168 test("bind(), no data", function() {
170 var handler = function(event) {
171 ok ( !event.data, "Check that no data is added to the event object" );
173 jQuery("#firstp").bind("click", handler).trigger("click");
176 test("bind/one/unbind(Object)", function(){
179 var clickCounter = 0, mouseoverCounter = 0;
180 function handler(event) {
181 if (event.type == "click")
183 else if (event.type == "mouseover")
187 function handlerWithData(event) {
188 if (event.type == "click")
189 clickCounter += event.data;
190 else if (event.type == "mouseover")
191 mouseoverCounter += event.data;
195 $elem.trigger("click").trigger("mouseover");
198 var $elem = jQuery("#firstp")
206 click:handlerWithData,
207 mouseover:handlerWithData
212 equals( clickCounter, 3, "bind(Object)" );
213 equals( mouseoverCounter, 3, "bind(Object)" );
216 equals( clickCounter, 4, "bind(Object)" );
217 equals( mouseoverCounter, 4, "bind(Object)" );
219 jQuery("#firstp").unbind({
225 equals( clickCounter, 4, "bind(Object)" );
226 equals( mouseoverCounter, 4, "bind(Object)" );
229 test("live/die(Object), delegate/undelegate(String, Object)", function() {
232 var clickCounter = 0, mouseoverCounter = 0,
233 $p = jQuery("#firstp"), $a = $p.find("a:first");
236 click: function( event ) {
237 clickCounter += ( event.data || 1 );
239 mouseover: function( event ) {
240 mouseoverCounter += ( event.data || 1 );
245 $a.trigger("click").trigger("mouseover");
249 $p.delegate( "a", events, 2 );
252 equals( clickCounter, 3, "live/delegate" );
253 equals( mouseoverCounter, 3, "live/delegate" );
255 $p.undelegate( "a", events );
258 equals( clickCounter, 4, "undelegate" );
259 equals( mouseoverCounter, 4, "undelegate" );
264 equals( clickCounter, 4, "die" );
265 equals( mouseoverCounter, 4, "die" );
268 test("live/delegate immediate propagation", function() {
271 var $p = jQuery("#firstp"), $a = $p.find("a:first"), lastClick;
274 $a.live( "click", function(e) {
275 lastClick = "click1";
276 e.stopImmediatePropagation();
278 $a.live( "click", function(e) {
279 lastClick = "click2";
281 $a.trigger( "click" );
282 equals( lastClick, "click1", "live stopImmediatePropagation" );
286 $p.delegate( "a", "click", function(e) {
287 lastClick = "click1";
288 e.stopImmediatePropagation();
290 $p.delegate( "a", "click", function(e) {
291 lastClick = "click2";
293 $a.trigger( "click" );
294 equals( lastClick, "click1", "delegate stopImmediatePropagation" );
295 $p.undelegate( "click" );
298 test("bind/delegate bubbling, isDefaultPrevented (Bug #7793)", function() {
300 var $anchor2 = jQuery( "#anchor2" ),
301 $main = jQuery( "#main" ),
302 fakeClick = function($jq) {
303 // Use a native click so we don't get jQuery simulated bubbling
304 if ( document.createEvent ) {
305 var e = document.createEvent( "MouseEvents" );
306 e.initEvent( "click", true, true );
307 $jq[0].dispatchEvent(e);
309 else if ( $jq[0].click ) {
310 $jq[0].click(); // IE
313 $anchor2.click(function(e) {
316 $main.delegate("#foo", "click", function(e) {
317 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
319 fakeClick( $anchor2 );
320 $anchor2.unbind( "click" );
321 $main.undelegate( "click" );
322 $anchor2.click(function(e) {
323 // Let the default action occur
325 $main.delegate("#foo", "click", function(e) {
326 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
328 fakeClick( $anchor2 );
329 $anchor2.unbind( "click" );
330 $main.undelegate( "click" );
333 test("bind(), iframes", function() {
334 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
335 var doc = jQuery("#loadediframe").contents();
337 jQuery("div", doc).bind("click", function() {
338 ok( true, "Binding to element inside iframe" );
339 }).click().unbind('click');
342 test("bind(), trigger change on select", function() {
345 function selectOnChange(event) {
346 equals( event.data, counter++, "Event.data is not a global event object" );
348 jQuery("#form select").each(function(i){
349 jQuery(this).bind('change', i, selectOnChange);
350 }).trigger('change');
353 test("bind(), namespaced events, cloned events", function() {
356 jQuery("#firstp").bind("custom.test",function(e){
357 ok(true, "Custom event triggered");
360 jQuery("#firstp").bind("click",function(e){
361 ok(true, "Normal click triggered");
364 jQuery("#firstp").bind("click.test",function(e){
365 ok(true, "Namespaced click triggered");
368 // Trigger both bound fn (2)
369 jQuery("#firstp").trigger("click");
371 // Trigger one bound fn (1)
372 jQuery("#firstp").trigger("click.test");
374 // Remove only the one fn
375 jQuery("#firstp").unbind("click.test");
377 // Trigger the remaining fn (1)
378 jQuery("#firstp").trigger("click");
380 // Remove the remaining fn
381 jQuery("#firstp").unbind(".test");
383 // Trigger the remaining fn (0)
384 jQuery("#firstp").trigger("custom");
386 // using contents will get comments regular, text, and comment nodes
387 jQuery("#nonnodes").contents().bind("tester", function () {
388 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
389 }).trigger("tester");
391 // Make sure events stick with appendTo'd elements (which are cloned) #2027
392 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
393 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
396 test("bind(), multi-namespaced events", function() {
408 function check(name, msg){
409 same(name, order.shift(), msg);
412 jQuery("#firstp").bind("custom.test",function(e){
413 check("custom.test", "Custom event triggered");
416 jQuery("#firstp").bind("custom.test2",function(e){
417 check("custom.test2", "Custom event triggered");
420 jQuery("#firstp").bind("click.test",function(e){
421 check("click.test", "Normal click triggered");
424 jQuery("#firstp").bind("click.test.abc",function(e){
425 check("click.test.abc", "Namespaced click triggered");
428 // Those would not trigger/unbind (#5303)
429 jQuery("#firstp").trigger("click.a.test");
430 jQuery("#firstp").unbind("click.a.test");
432 // Trigger both bound fn (1)
433 jQuery("#firstp").trigger("click.test.abc");
435 // Trigger one bound fn (1)
436 jQuery("#firstp").trigger("click.abc");
438 // Trigger two bound fn (2)
439 jQuery("#firstp").trigger("click.test");
441 // Remove only the one fn
442 jQuery("#firstp").unbind("click.abc");
444 // Trigger the remaining fn (1)
445 jQuery("#firstp").trigger("click");
447 // Remove the remaining fn
448 jQuery("#firstp").unbind(".test");
450 // Trigger the remaining fn (1)
451 jQuery("#firstp").trigger("custom");
454 test("bind(), with same function", function() {
457 var count = 0 , func = function(){
461 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
462 jQuery("#liveHandlerOrder").trigger("foo.bar");
464 equals(count, 1, "Verify binding function with multiple namespaces." );
466 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
467 jQuery("#liveHandlerOrder").trigger("foo.bar");
469 equals(count, 1, "Verify that removing events still work." );
472 test("bind(), make sure order is maintained", function() {
475 var elem = jQuery("#firstp"), log = [], check = [];
477 for ( var i = 0; i < 100; i++ ) (function(i){
478 elem.bind( "click", function(){
485 elem.trigger("click");
487 equals( log.join(","), check.join(","), "Make sure order was maintained." );
489 elem.unbind("click");
492 test("bind(), with different this object", function() {
494 var thisObject = { myThis: true },
495 data = { myData: true },
496 handler1 = function( event ) {
497 equals( this, thisObject, "bind() with different this object" );
499 handler2 = function( event ) {
500 equals( this, thisObject, "bind() with different this object and data" );
501 equals( event.data, data, "bind() with different this object and data" );
505 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
506 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
508 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
511 test("bind(name, false), unbind(name, false)", function() {
515 jQuery("#main").bind("click", function(e){ main++; });
516 jQuery("#ap").trigger("click");
517 equals( main, 1, "Verify that the trigger happened correctly." );
520 jQuery("#ap").bind("click", false);
521 jQuery("#ap").trigger("click");
522 equals( main, 0, "Verify that no bubble happened." );
525 jQuery("#ap").unbind("click", false);
526 jQuery("#ap").trigger("click");
527 equals( main, 1, "Verify that the trigger happened correctly." );
530 test("bind()/trigger()/unbind() on plain object", function() {
535 // Make sure it doesn't complain when no events are found
536 jQuery(obj).trigger("test");
538 // Make sure it doesn't complain when no events are found
539 jQuery(obj).unbind("test");
543 ok( true, "Custom event run." );
546 ok( true, "Custom submit event run." );
550 var events = jQuery._data(obj, "events");
551 ok( events, "Object has events bound." );
552 equals( obj.events, undefined, "Events object on plain objects is not events" );
553 equals( typeof events, "function", "'events' expando is a function on plain objects." );
554 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
555 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
558 jQuery(obj).trigger("test");
559 jQuery(obj).trigger("submit");
561 jQuery(obj).unbind("test");
562 jQuery(obj).unbind("submit");
565 jQuery(obj).trigger("test");
567 // Make sure it doesn't complain when no events are found
568 jQuery(obj).unbind("test");
570 equals( obj && obj[ jQuery.expando ] &&
571 obj[ jQuery.expando ][ jQuery.expando ] &&
572 obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
575 test("unbind(type)", function() {
578 var $elem = jQuery("#firstp"),
582 ok( false, message );
585 message = "unbind passing function";
586 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
588 message = "unbind all from event";
589 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
591 message = "unbind all";
592 $elem.bind('error1', error).unbind().triggerHandler('error1');
594 message = "unbind many with function";
595 $elem.bind('error1 error2',error)
596 .unbind('error1 error2', error )
597 .trigger('error1').triggerHandler('error2');
599 message = "unbind many"; // #3538
600 $elem.bind('error1 error2',error)
601 .unbind('error1 error2')
602 .trigger('error1').triggerHandler('error2');
604 message = "unbind without a type or handler";
605 $elem.bind("error1 error2.test",error)
607 .trigger("error1").triggerHandler("error2");
610 test("unbind(eventObject)", function() {
613 var $elem = jQuery("#firstp"),
616 function assert( expected ){
618 $elem.trigger('foo').triggerHandler('bar');
619 equals( num, expected, "Check the right handlers are triggered" );
623 // This handler shouldn't be unbound
624 .bind('foo', function(){
627 .bind('foo', function(e){
632 .bind('bar', function(){
646 test("hover()", function() {
648 handler1 = function( event ) { ++times; },
649 handler2 = function( event ) { ++times; };
652 .hover(handler1, handler2)
653 .mouseenter().mouseleave()
654 .unbind("mouseenter", handler1)
655 .unbind("mouseleave", handler2)
657 .mouseenter().mouseleave()
658 .unbind("mouseenter mouseleave", handler1)
659 .mouseenter().mouseleave();
661 equals( times, 4, "hover handlers fired" );
664 test("trigger() shortcuts", function() {
666 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
667 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
668 equals( close.length, 0, "Context element does not exist, length must be zero" );
669 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
673 jQuery("#check1").click(function() {
674 ok( true, "click event handler for checkbox gets fired twice, see #815" );
678 jQuery('#firstp')[0].onclick = function(event) {
681 jQuery('#firstp').click();
682 equals( counter, 1, "Check that click, triggers onclick event handler also" );
684 var clickCounter = 0;
685 jQuery('#simon1')[0].onclick = function(event) {
688 jQuery('#simon1').click();
689 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
691 jQuery('<img />').load(function(){
692 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
696 test("trigger() bubbling", function() {
699 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
701 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
702 jQuery("html").bind("click", function(e){ html++; });
703 jQuery("body").bind("click", function(e){ body++; });
704 jQuery("#main").bind("click", function(e){ main++; });
705 jQuery("#ap").bind("click", function(){ ap++; return false; });
707 jQuery("html").trigger("click");
708 equals( doc, 1, "HTML bubble" );
709 equals( html, 1, "HTML bubble" );
711 jQuery("body").trigger("click");
712 equals( doc, 2, "Body bubble" );
713 equals( html, 2, "Body bubble" );
714 equals( body, 1, "Body bubble" );
716 jQuery("#main").trigger("click");
717 equals( doc, 3, "Main bubble" );
718 equals( html, 3, "Main bubble" );
719 equals( body, 2, "Main bubble" );
720 equals( main, 1, "Main bubble" );
722 jQuery("#ap").trigger("click");
723 equals( doc, 3, "ap bubble" );
724 equals( html, 3, "ap bubble" );
725 equals( body, 2, "ap bubble" );
726 equals( main, 1, "ap bubble" );
727 equals( ap, 1, "ap bubble" );
730 test("trigger(type, [data], [fn])", function() {
733 var handler = function(event, a, b, c) {
734 equals( event.type, "click", "check passed data" );
735 equals( a, 1, "check passed data" );
736 equals( b, "2", "check passed data" );
737 equals( c, "abc", "check passed data" );
741 var $elem = jQuery("#firstp");
743 // Simulate a "native" click
744 $elem[0].click = function(){
745 ok( true, "Native call was triggered" );
748 // Triggers handlrs and native
750 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
752 // Simulate a "native" click
753 $elem[0].click = function(){
754 ok( false, "Native call was triggered" );
757 // Trigger only the handlers (no native)
759 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
763 jQuery('#form input:first').hide().trigger('focus');
767 ok( pass, "Trigger focus on hidden element" );
771 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
775 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
777 var form = jQuery("<form action=''></form>").appendTo("body");
779 // Make sure it can be prevented locally
780 form.submit(function(){
781 ok( true, "Local bind still works." );
786 form.trigger("submit");
788 form.unbind("submit");
790 jQuery(document).submit(function(){
791 ok( true, "Make sure bubble works up to document." );
796 form.trigger("submit");
798 jQuery(document).unbind("submit");
803 test("jQuery.Event.currentTarget", function(){
806 test("trigger(eventObject, [data], [fn])", function() {
809 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
810 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
812 var event = jQuery.Event("noNew");
813 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
814 equals( event.type, "noNew", "Verify its type" );
816 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
817 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
818 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
820 event.preventDefault();
821 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
822 event.stopPropagation();
823 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
825 event.isPropagationStopped = function(){ return false };
826 event.stopImmediatePropagation();
827 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
828 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
830 $parent.bind('foo',function(e){
832 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
833 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
834 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
835 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
838 // test with an event object
839 event = new jQuery.Event("foo");
840 event.secret = 'boo!';
841 $child.trigger(event);
843 // test with a literal object
844 $child.trigger({type:'foo', secret:'boo!'});
849 ok( false, "This assertion shouldn't be reached");
852 $parent.bind('foo', error );
854 $child.bind('foo',function(e, a, b, c ){
855 equals( arguments.length, 4, "Check arguments length");
856 equals( a, 1, "Check first custom argument");
857 equals( b, 2, "Check second custom argument");
858 equals( c, 3, "Check third custom argument");
860 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
861 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
862 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
865 e.stopImmediatePropagation();
870 // We should add this back in when we want to test the order
871 // in which event handlers are iterated.
872 //$child.bind('foo', error );
874 event = new jQuery.Event("foo");
875 $child.trigger( event, [1,2,3] ).unbind();
876 equals( event.result, "result", "Check event.result attribute");
878 // Will error if it bubbles
879 $child.triggerHandler('foo');
882 $parent.unbind().remove();
885 test("jQuery.Event.currentTarget", function(){
889 $elem = jQuery('<button>a</button>').click(function(e){
890 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
894 $elem.trigger('click');
900 test("toggle(Function, Function, ...)", function() {
904 fn1 = function(e) { count++; },
905 fn2 = function(e) { count--; },
906 preventDefault = function(e) { e.preventDefault() },
907 link = jQuery('#mark');
908 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
909 equals( count, 1, "Check for toggle(fn, fn)" );
911 jQuery("#firstp").toggle(function () {
912 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
913 }, function() {}).trigger("click", [ 1, 2, 3 ]);
916 jQuery("#simon1").one("click", function() {
917 ok( true, "Execute event only once" );
918 jQuery(this).toggle(function() {
919 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
921 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
924 }).click().click().click();
939 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
941 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
943 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
945 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
947 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
949 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
951 $div.unbind('click',fns[0]);
952 var data = jQuery._data( $div[0], 'events' );
953 ok( !data, "Unbinding one function from toggle unbinds them all");
955 // Test Multi-Toggles
957 $div = jQuery("<div/>");
958 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
960 same( a, [1], "Check that a click worked." );
962 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
964 same( a, [1,2], "Check that a click worked with a second toggle." );
965 same( b, [1], "Check that a click worked with a second toggle." );
968 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
969 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
972 test(".live()/.die()", function() {
975 var submit = 0, div = 0, livea = 0, liveb = 0;
977 jQuery("div").live("submit", function(){ submit++; return false; });
978 jQuery("div").live("click", function(){ div++; });
979 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
980 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
982 // Nothing should trigger on the body
983 jQuery("body").trigger("click");
984 equals( submit, 0, "Click on body" );
985 equals( div, 0, "Click on body" );
986 equals( livea, 0, "Click on body" );
987 equals( liveb, 0, "Click on body" );
989 // This should trigger two events
990 submit = 0, div = 0, livea = 0, liveb = 0;
991 jQuery("div#nothiddendiv").trigger("click");
992 equals( submit, 0, "Click on div" );
993 equals( div, 1, "Click on div" );
994 equals( livea, 1, "Click on div" );
995 equals( liveb, 0, "Click on div" );
997 // This should trigger three events (w/ bubbling)
998 submit = 0, div = 0, livea = 0, liveb = 0;
999 jQuery("div#nothiddendivchild").trigger("click");
1000 equals( submit, 0, "Click on inner div" );
1001 equals( div, 2, "Click on inner div" );
1002 equals( livea, 1, "Click on inner div" );
1003 equals( liveb, 1, "Click on inner div" );
1005 // This should trigger one submit
1006 submit = 0, div = 0, livea = 0, liveb = 0;
1007 jQuery("div#nothiddendivchild").trigger("submit");
1008 equals( submit, 1, "Submit on div" );
1009 equals( div, 0, "Submit on div" );
1010 equals( livea, 0, "Submit on div" );
1011 equals( liveb, 0, "Submit on div" );
1013 // Make sure no other events were removed in the process
1014 submit = 0, div = 0, livea = 0, liveb = 0;
1015 jQuery("div#nothiddendivchild").trigger("click");
1016 equals( submit, 0, "die Click on inner div" );
1017 equals( div, 2, "die Click on inner div" );
1018 equals( livea, 1, "die Click on inner div" );
1019 equals( liveb, 1, "die Click on inner div" );
1021 // Now make sure that the removal works
1022 submit = 0, div = 0, livea = 0, liveb = 0;
1023 jQuery("div#nothiddendivchild").die("click");
1024 jQuery("div#nothiddendivchild").trigger("click");
1025 equals( submit, 0, "die Click on inner div" );
1026 equals( div, 2, "die Click on inner div" );
1027 equals( livea, 1, "die Click on inner div" );
1028 equals( liveb, 0, "die Click on inner div" );
1030 // Make sure that the click wasn't removed too early
1031 submit = 0, div = 0, livea = 0, liveb = 0;
1032 jQuery("div#nothiddendiv").trigger("click");
1033 equals( submit, 0, "die Click on inner div" );
1034 equals( div, 1, "die Click on inner div" );
1035 equals( livea, 1, "die Click on inner div" );
1036 equals( liveb, 0, "die Click on inner div" );
1038 // Make sure that stopPropgation doesn't stop live events
1039 submit = 0, div = 0, livea = 0, liveb = 0;
1040 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1041 jQuery("div#nothiddendivchild").trigger("click");
1042 equals( submit, 0, "stopPropagation Click on inner div" );
1043 equals( div, 1, "stopPropagation Click on inner div" );
1044 equals( livea, 0, "stopPropagation Click on inner div" );
1045 equals( liveb, 1, "stopPropagation Click on inner div" );
1047 // Make sure click events only fire with primary click
1048 submit = 0, div = 0, livea = 0, liveb = 0;
1049 var event = jQuery.Event("click");
1051 jQuery("div#nothiddendiv").trigger(event);
1053 equals( livea, 0, "live secondary click" );
1055 jQuery("div#nothiddendivchild").die("click");
1056 jQuery("div#nothiddendiv").die("click");
1057 jQuery("div").die("click");
1058 jQuery("div").die("submit");
1060 // Test binding with a different context
1061 var clicked = 0, container = jQuery('#main')[0];
1062 jQuery("#foo", container).live("click", function(e){ clicked++; });
1063 jQuery("div").trigger('click');
1064 jQuery("#foo").trigger('click');
1065 jQuery("#main").trigger('click');
1066 jQuery("body").trigger('click');
1067 equals( clicked, 2, "live with a context" );
1069 // Make sure the event is actually stored on the context
1070 ok( jQuery._data(container, "events").live, "live with a context" );
1072 // Test unbinding with a different context
1073 jQuery("#foo", container).die("click");
1074 jQuery("#foo").trigger('click');
1075 equals( clicked, 2, "die with a context");
1077 // Test binding with event data
1078 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1079 jQuery("#foo").trigger("click").die("click");
1081 // Test binding with trigger data
1082 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1083 jQuery("#foo").trigger("click", true).die("click");
1085 // Test binding with different this object
1086 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1087 jQuery("#foo").trigger("click").die("click");
1089 // Test binding with different this object, event data, and trigger data
1090 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1091 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1092 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1093 equals( data, true, "live with with different this object, event data, and trigger data")
1094 }, { foo: "bar" }));
1095 jQuery("#foo").trigger("click", true).die("click");
1097 // Verify that return false prevents default action
1098 jQuery("#anchor2").live("click", function(){ return false; });
1099 var hash = window.location.hash;
1100 jQuery("#anchor2").trigger("click");
1101 equals( window.location.hash, hash, "return false worked" );
1102 jQuery("#anchor2").die("click");
1104 // Verify that .preventDefault() prevents default action
1105 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1106 var hash = window.location.hash;
1107 jQuery("#anchor2").trigger("click");
1108 equals( window.location.hash, hash, "e.preventDefault() worked" );
1109 jQuery("#anchor2").die("click");
1111 // Test binding the same handler to multiple points
1113 function callback(){ called++; return false; }
1115 jQuery("#nothiddendiv").live("click", callback);
1116 jQuery("#anchor2").live("click", callback);
1118 jQuery("#nothiddendiv").trigger("click");
1119 equals( called, 1, "Verify that only one click occurred." );
1122 jQuery("#anchor2").trigger("click");
1123 equals( called, 1, "Verify that only one click occurred." );
1125 // Make sure that only one callback is removed
1126 jQuery("#anchor2").die("click", callback);
1129 jQuery("#nothiddendiv").trigger("click");
1130 equals( called, 1, "Verify that only one click occurred." );
1133 jQuery("#anchor2").trigger("click");
1134 equals( called, 0, "Verify that no click occurred." );
1136 // Make sure that it still works if the selector is the same,
1137 // but the event type is different
1138 jQuery("#nothiddendiv").live("foo", callback);
1141 jQuery("#nothiddendiv").die("click", callback);
1144 jQuery("#nothiddendiv").trigger("click");
1145 equals( called, 0, "Verify that no click occurred." );
1148 jQuery("#nothiddendiv").trigger("foo");
1149 equals( called, 1, "Verify that one foo occurred." );
1152 jQuery("#nothiddendiv").die("foo", callback);
1154 // Make sure we don't loose the target by DOM modifications
1155 // after the bubble already reached the liveHandler
1156 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1158 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1159 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1161 jQuery("#nothiddendiv span").click();
1162 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1163 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1166 jQuery("#nothiddendivchild").die("click");
1168 // Verify that .live() ocurs and cancel buble in the same order as
1169 // we would expect .bind() and .click() without delegation
1170 var lived = 0, livee = 0;
1172 // bind one pair in one order
1173 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1174 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1176 jQuery('span#liveSpan1 a').click();
1177 equals( lived, 1, "Verify that only one first handler occurred." );
1178 equals( livee, 0, "Verify that second handler doesn't." );
1180 // and one pair in inverse
1181 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1182 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1186 jQuery('span#liveSpan2 a').click();
1187 equals( lived, 1, "Verify that only one first handler occurred." );
1188 equals( livee, 0, "Verify that second handler doesn't." );
1191 jQuery("span#liveSpan1 a").die("click")
1192 jQuery("span#liveSpan1").die("click");
1193 jQuery("span#liveSpan2 a").die("click");
1194 jQuery("span#liveSpan2").die("click");
1196 // Test this, target and currentTarget are correct
1197 jQuery('span#liveSpan1').live('click', function(e){
1198 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1199 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1200 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1203 jQuery('span#liveSpan1 a').click();
1205 jQuery('span#liveSpan1').die('click');
1207 // Work with deep selectors
1210 function clickB(){ livee++; }
1212 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1213 jQuery("#nothiddendiv div").live("click", clickB);
1214 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1216 equals( livee, 0, "No clicks, deep selector." );
1219 jQuery("#nothiddendivchild").trigger("click");
1220 equals( livee, 2, "Click, deep selector." );
1223 jQuery("#nothiddendivchild").trigger("mouseover");
1224 equals( livee, 1, "Mouseover, deep selector." );
1226 jQuery("#nothiddendiv div").die("mouseover");
1229 jQuery("#nothiddendivchild").trigger("click");
1230 equals( livee, 2, "Click, deep selector." );
1233 jQuery("#nothiddendivchild").trigger("mouseover");
1234 equals( livee, 0, "Mouseover, deep selector." );
1236 jQuery("#nothiddendiv div").die("click", clickB);
1239 jQuery("#nothiddendivchild").trigger("click");
1240 equals( livee, 1, "Click, deep selector." );
1242 jQuery("#nothiddendiv div").die("click");
1244 jQuery("#nothiddendiv div").live("blur", function(){
1245 ok( true, "Live div trigger blur." );
1248 jQuery("#nothiddendiv div").trigger("blur");
1250 jQuery("#nothiddendiv div").die("blur");
1253 test("die all bound events", function(){
1257 var div = jQuery("div#nothiddendivchild");
1259 div.live("click submit", function(){ count++; });
1262 div.trigger("click");
1263 div.trigger("submit");
1265 equals( count, 0, "Make sure no events were triggered." );
1268 test("live with multiple events", function(){
1272 var div = jQuery("div#nothiddendivchild");
1274 div.live("click submit", function(){ count++; });
1276 div.trigger("click");
1277 div.trigger("submit");
1279 equals( count, 2, "Make sure both the click and submit were triggered." );
1282 test("live with namespaces", function(){
1285 var count1 = 0, count2 = 0;
1287 jQuery("#liveSpan1").live("foo.bar", function(e){
1291 jQuery("#liveSpan1").live("foo.zed", function(e){
1295 jQuery("#liveSpan1").trigger("foo.bar");
1296 equals( count1, 1, "Got live foo.bar" );
1297 equals( count2, 0, "Got live foo.bar" );
1299 count1 = 0, count2 = 0;
1301 jQuery("#liveSpan1").trigger("foo.zed");
1302 equals( count1, 0, "Got live foo.zed" );
1303 equals( count2, 1, "Got live foo.zed" );
1306 count1 = 0, count2 = 0;
1308 jQuery("#liveSpan1").die("foo.zed");
1309 jQuery("#liveSpan1").trigger("foo.bar");
1311 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1312 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1314 count1 = 0, count2 = 0;
1316 jQuery("#liveSpan1").trigger("foo.zed");
1317 equals( count1, 0, "Got live foo.zed" );
1318 equals( count2, 0, "Got live foo.zed" );
1321 jQuery("#liveSpan1").die("foo.bar");
1323 count1 = 0, count2 = 0;
1325 jQuery("#liveSpan1").trigger("foo.bar");
1326 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1327 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1329 jQuery("#liveSpan1").trigger("foo.zed");
1330 equals( count1, 0, "Did not trigger foo.zed again" );
1331 equals( count2, 0, "Did not trigger foo.zed again" );
1334 test("live with change", function(){
1337 var selectChange = 0, checkboxChange = 0;
1339 var select = jQuery("select[name='S1']")
1340 select.live("change", function() {
1344 var checkbox = jQuery("#check2"),
1345 checkboxFunction = function(){
1348 checkbox.live("change", checkboxFunction);
1350 // test click on select
1352 // second click that changed it
1354 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1355 select.trigger("change");
1356 equals( selectChange, 1, "Change on click." );
1358 // test keys on select
1360 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1361 select.trigger("change");
1362 equals( selectChange, 1, "Change on keyup." );
1364 // test click on checkbox
1365 checkbox.trigger("change");
1366 equals( checkboxChange, 1, "Change on checkbox." );
1368 // test blur/focus on text
1369 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1370 text.live("change", function() {
1374 text.val(oldTextVal+"foo");
1375 text.trigger("change");
1376 equals( textChange, 1, "Change on text input." );
1378 text.val(oldTextVal);
1381 // test blur/focus on password
1382 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1383 password.live("change", function() {
1387 password.val(oldPasswordVal + "foo");
1388 password.trigger("change");
1389 equals( passwordChange, 1, "Change on password input." );
1391 password.val(oldPasswordVal);
1392 password.die("change");
1394 // make sure die works
1398 select.die("change");
1399 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1400 select.trigger("change");
1401 equals( selectChange, 0, "Die on click works." );
1404 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1405 select.trigger("change");
1406 equals( selectChange, 0, "Die on keyup works." );
1408 // die specific checkbox
1409 checkbox.die("change", checkboxFunction);
1410 checkbox.trigger("change");
1411 equals( checkboxChange, 1, "Die on checkbox." );
1414 test("live with submit", function() {
1415 var count1 = 0, count2 = 0;
1417 jQuery("#testForm").live("submit", function(ev) {
1419 ev.preventDefault();
1422 jQuery("body").live("submit", function(ev) {
1424 ev.preventDefault();
1427 jQuery("#testForm input[name=sub1]").submit();
1428 equals( count1, 1, "Verify form submit." );
1429 equals( count2, 1, "Verify body submit." );
1431 jQuery("#testForm").die("submit");
1432 jQuery("body").die("submit");
1435 test("live with special events", function() {
1438 jQuery.event.special.foo = {
1439 setup: function( data, namespaces, handler ) {
1440 ok( true, "Setup run." );
1442 teardown: function( namespaces ) {
1443 ok( true, "Teardown run." );
1445 add: function( handleObj ) {
1446 ok( true, "Add run." );
1448 remove: function( handleObj ) {
1449 ok( true, "Remove run." );
1451 _default: function( event ) {
1452 ok( true, "Default run." );
1457 jQuery("#liveSpan1").live("foo.a", function(e){
1458 ok( true, "Handler 1 run." );
1462 jQuery("#liveSpan1").live("foo.b", function(e){
1463 ok( true, "Handler 2 run." );
1466 // Run: Handler 1, Handler 2, Default
1467 jQuery("#liveSpan1").trigger("foo");
1469 // Run: Handler 1, Default
1470 // TODO: Namespace doesn't trigger default (?)
1471 jQuery("#liveSpan1").trigger("foo.a");
1474 jQuery("#liveSpan1").die("foo.a");
1476 // Run: Handler 2, Default
1477 jQuery("#liveSpan1").trigger("foo");
1479 // Run: remove, teardown
1480 jQuery("#liveSpan1").die("foo");
1482 delete jQuery.event.special.foo;
1485 test(".delegate()/.undelegate()", function() {
1488 var submit = 0, div = 0, livea = 0, liveb = 0;
1490 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1491 jQuery("#body").delegate("div", "click", function(){ div++; });
1492 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1493 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1495 // Nothing should trigger on the body
1496 jQuery("body").trigger("click");
1497 equals( submit, 0, "Click on body" );
1498 equals( div, 0, "Click on body" );
1499 equals( livea, 0, "Click on body" );
1500 equals( liveb, 0, "Click on body" );
1502 // This should trigger two events
1503 submit = 0, div = 0, livea = 0, liveb = 0;
1504 jQuery("div#nothiddendiv").trigger("click");
1505 equals( submit, 0, "Click on div" );
1506 equals( div, 1, "Click on div" );
1507 equals( livea, 1, "Click on div" );
1508 equals( liveb, 0, "Click on div" );
1510 // This should trigger three events (w/ bubbling)
1511 submit = 0, div = 0, livea = 0, liveb = 0;
1512 jQuery("div#nothiddendivchild").trigger("click");
1513 equals( submit, 0, "Click on inner div" );
1514 equals( div, 2, "Click on inner div" );
1515 equals( livea, 1, "Click on inner div" );
1516 equals( liveb, 1, "Click on inner div" );
1518 // This should trigger one submit
1519 submit = 0, div = 0, livea = 0, liveb = 0;
1520 jQuery("div#nothiddendivchild").trigger("submit");
1521 equals( submit, 1, "Submit on div" );
1522 equals( div, 0, "Submit on div" );
1523 equals( livea, 0, "Submit on div" );
1524 equals( liveb, 0, "Submit on div" );
1526 // Make sure no other events were removed in the process
1527 submit = 0, div = 0, livea = 0, liveb = 0;
1528 jQuery("div#nothiddendivchild").trigger("click");
1529 equals( submit, 0, "undelegate Click on inner div" );
1530 equals( div, 2, "undelegate Click on inner div" );
1531 equals( livea, 1, "undelegate Click on inner div" );
1532 equals( liveb, 1, "undelegate Click on inner div" );
1534 // Now make sure that the removal works
1535 submit = 0, div = 0, livea = 0, liveb = 0;
1536 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1537 jQuery("div#nothiddendivchild").trigger("click");
1538 equals( submit, 0, "undelegate Click on inner div" );
1539 equals( div, 2, "undelegate Click on inner div" );
1540 equals( livea, 1, "undelegate Click on inner div" );
1541 equals( liveb, 0, "undelegate Click on inner div" );
1543 // Make sure that the click wasn't removed too early
1544 submit = 0, div = 0, livea = 0, liveb = 0;
1545 jQuery("div#nothiddendiv").trigger("click");
1546 equals( submit, 0, "undelegate Click on inner div" );
1547 equals( div, 1, "undelegate Click on inner div" );
1548 equals( livea, 1, "undelegate Click on inner div" );
1549 equals( liveb, 0, "undelegate Click on inner div" );
1551 // Make sure that stopPropgation doesn't stop live events
1552 submit = 0, div = 0, livea = 0, liveb = 0;
1553 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1554 jQuery("div#nothiddendivchild").trigger("click");
1555 equals( submit, 0, "stopPropagation Click on inner div" );
1556 equals( div, 1, "stopPropagation Click on inner div" );
1557 equals( livea, 0, "stopPropagation Click on inner div" );
1558 equals( liveb, 1, "stopPropagation Click on inner div" );
1560 // Make sure click events only fire with primary click
1561 submit = 0, div = 0, livea = 0, liveb = 0;
1562 var event = jQuery.Event("click");
1564 jQuery("div#nothiddendiv").trigger(event);
1566 equals( livea, 0, "delegate secondary click" );
1568 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1569 jQuery("#body").undelegate("div#nothiddendiv", "click");
1570 jQuery("#body").undelegate("div", "click");
1571 jQuery("#body").undelegate("div", "submit");
1573 // Test binding with a different context
1574 var clicked = 0, container = jQuery('#main')[0];
1575 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1576 jQuery("div").trigger('click');
1577 jQuery("#foo").trigger('click');
1578 jQuery("#main").trigger('click');
1579 jQuery("body").trigger('click');
1580 equals( clicked, 2, "delegate with a context" );
1582 // Make sure the event is actually stored on the context
1583 ok( jQuery._data(container, "events").live, "delegate with a context" );
1585 // Test unbinding with a different context
1586 jQuery("#main").undelegate("#foo", "click");
1587 jQuery("#foo").trigger('click');
1588 equals( clicked, 2, "undelegate with a context");
1590 // Test binding with event data
1591 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1592 jQuery("#foo").trigger("click");
1593 jQuery("#body").undelegate("#foo", "click");
1595 // Test binding with trigger data
1596 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1597 jQuery("#foo").trigger("click", true);
1598 jQuery("#body").undelegate("#foo", "click");
1600 // Test binding with different this object
1601 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1602 jQuery("#foo").trigger("click");
1603 jQuery("#body").undelegate("#foo", "click");
1605 // Test binding with different this object, event data, and trigger data
1606 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1607 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1608 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1609 equals( data, true, "delegate with with different this object, event data, and trigger data")
1610 }, { foo: "bar" }));
1611 jQuery("#foo").trigger("click", true);
1612 jQuery("#body").undelegate("#foo", "click");
1614 // Verify that return false prevents default action
1615 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1616 var hash = window.location.hash;
1617 jQuery("#anchor2").trigger("click");
1618 equals( window.location.hash, hash, "return false worked" );
1619 jQuery("#body").undelegate("#anchor2", "click");
1621 // Verify that .preventDefault() prevents default action
1622 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1623 var hash = window.location.hash;
1624 jQuery("#anchor2").trigger("click");
1625 equals( window.location.hash, hash, "e.preventDefault() worked" );
1626 jQuery("#body").undelegate("#anchor2", "click");
1628 // Test binding the same handler to multiple points
1630 function callback(){ called++; return false; }
1632 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1633 jQuery("#body").delegate("#anchor2", "click", callback);
1635 jQuery("#nothiddendiv").trigger("click");
1636 equals( called, 1, "Verify that only one click occurred." );
1639 jQuery("#anchor2").trigger("click");
1640 equals( called, 1, "Verify that only one click occurred." );
1642 // Make sure that only one callback is removed
1643 jQuery("#body").undelegate("#anchor2", "click", callback);
1646 jQuery("#nothiddendiv").trigger("click");
1647 equals( called, 1, "Verify that only one click occurred." );
1650 jQuery("#anchor2").trigger("click");
1651 equals( called, 0, "Verify that no click occurred." );
1653 // Make sure that it still works if the selector is the same,
1654 // but the event type is different
1655 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1658 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1661 jQuery("#nothiddendiv").trigger("click");
1662 equals( called, 0, "Verify that no click occurred." );
1665 jQuery("#nothiddendiv").trigger("foo");
1666 equals( called, 1, "Verify that one foo occurred." );
1669 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1671 // Make sure we don't loose the target by DOM modifications
1672 // after the bubble already reached the liveHandler
1673 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1675 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1676 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1678 jQuery("#nothiddendiv span").click();
1679 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1680 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1683 jQuery("#body").undelegate("#nothiddendivchild", "click");
1685 // Verify that .live() ocurs and cancel buble in the same order as
1686 // we would expect .bind() and .click() without delegation
1687 var lived = 0, livee = 0;
1689 // bind one pair in one order
1690 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1691 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1693 jQuery('span#liveSpan1 a').click();
1694 equals( lived, 1, "Verify that only one first handler occurred." );
1695 equals( livee, 0, "Verify that second handler doesn't." );
1697 // and one pair in inverse
1698 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1699 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1703 jQuery('span#liveSpan2 a').click();
1704 equals( lived, 1, "Verify that only one first handler occurred." );
1705 equals( livee, 0, "Verify that second handler doesn't." );
1708 jQuery("#body").undelegate("click");
1710 // Test this, target and currentTarget are correct
1711 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1712 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1713 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1714 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1717 jQuery('span#liveSpan1 a').click();
1719 jQuery("#body").undelegate('span#liveSpan1', 'click');
1721 // Work with deep selectors
1724 function clickB(){ livee++; }
1726 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1727 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1728 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1730 equals( livee, 0, "No clicks, deep selector." );
1733 jQuery("#nothiddendivchild").trigger("click");
1734 equals( livee, 2, "Click, deep selector." );
1737 jQuery("#nothiddendivchild").trigger("mouseover");
1738 equals( livee, 1, "Mouseover, deep selector." );
1740 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1743 jQuery("#nothiddendivchild").trigger("click");
1744 equals( livee, 2, "Click, deep selector." );
1747 jQuery("#nothiddendivchild").trigger("mouseover");
1748 equals( livee, 0, "Mouseover, deep selector." );
1750 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1753 jQuery("#nothiddendivchild").trigger("click");
1754 equals( livee, 1, "Click, deep selector." );
1756 jQuery("#body").undelegate("#nothiddendiv div", "click");
1759 test("undelegate all bound events", function(){
1763 var div = jQuery("#body");
1765 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1768 jQuery("div#nothiddendivchild").trigger("click");
1769 jQuery("div#nothiddendivchild").trigger("submit");
1771 equals( count, 0, "Make sure no events were triggered." );
1774 test("delegate with multiple events", function(){
1778 var div = jQuery("#body");
1780 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1782 jQuery("div#nothiddendivchild").trigger("click");
1783 jQuery("div#nothiddendivchild").trigger("submit");
1785 equals( count, 2, "Make sure both the click and submit were triggered." );
1787 jQuery("#body").undelegate();
1790 test("delegate with change", function(){
1793 var selectChange = 0, checkboxChange = 0;
1795 var select = jQuery("select[name='S1']");
1796 jQuery("#body").delegate("select[name='S1']", "change", function() {
1800 var checkbox = jQuery("#check2"),
1801 checkboxFunction = function(){
1804 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1806 // test click on select
1808 // second click that changed it
1810 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1811 select.trigger("change");
1812 equals( selectChange, 1, "Change on click." );
1814 // test keys on select
1816 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1817 select.trigger("change");
1818 equals( selectChange, 1, "Change on keyup." );
1820 // test click on checkbox
1821 checkbox.trigger("change");
1822 equals( checkboxChange, 1, "Change on checkbox." );
1824 // test blur/focus on text
1825 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1826 jQuery("#body").delegate("#name", "change", function() {
1830 text.val(oldTextVal+"foo");
1831 text.trigger("change");
1832 equals( textChange, 1, "Change on text input." );
1834 text.val(oldTextVal);
1835 jQuery("#body").die("change");
1837 // test blur/focus on password
1838 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1839 jQuery("#body").delegate("#name", "change", function() {
1843 password.val(oldPasswordVal + "foo");
1844 password.trigger("change");
1845 equals( passwordChange, 1, "Change on password input." );
1847 password.val(oldPasswordVal);
1848 jQuery("#body").undelegate("#name", "change");
1850 // make sure die works
1854 jQuery("#body").undelegate("select[name='S1']", "change");
1855 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1856 select.trigger("change");
1857 equals( selectChange, 0, "Die on click works." );
1860 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1861 select.trigger("change");
1862 equals( selectChange, 0, "Die on keyup works." );
1864 // die specific checkbox
1865 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1866 checkbox.trigger("change");
1867 equals( checkboxChange, 1, "Die on checkbox." );
1870 test("delegate with submit", function() {
1871 var count1 = 0, count2 = 0;
1873 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1875 ev.preventDefault();
1878 jQuery(document).delegate("body", "submit", function(ev) {
1880 ev.preventDefault();
1883 jQuery("#testForm input[name=sub1]").submit();
1884 equals( count1, 1, "Verify form submit." );
1885 equals( count2, 1, "Verify body submit." );
1887 jQuery("#body").undelegate();
1888 jQuery(document).undelegate();
1891 test("Non DOM element events", function() {
1896 jQuery(o).bind('nonelementobj', function(e) {
1897 ok( true, "Event on non-DOM object triggered" );
1900 jQuery(o).trigger('nonelementobj');
1903 test("window resize", function() {
1906 jQuery(window).unbind();
1908 jQuery(window).bind("resize", function(){
1909 ok( true, "Resize event fired." );
1910 }).resize().unbind("resize");
1912 ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
1915 test("focusin bubbles", function() {
1916 //create an input and focusin on it
1917 var input = jQuery("<input/>"), order = 0;
1919 input.prependTo("body");
1921 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1922 equals(1,order++,"focusin on the body second")
1925 input.bind("focusin.focusinBubblesTest",function(){
1926 equals(0,order++,"focusin on the element first")
1932 jQuery("body").unbind("focusin.focusinBubblesTest");
1936 test("jQuery(function($) {})", function() {
1938 jQuery(function($) {
1939 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");
1944 test("event properties", function() {
1946 jQuery("#simon1").click(function(event) {
1947 ok( event.timeStamp, "assert event.timeStamp is present" );