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", function() {
300 var $anchor2 = jQuery( "#anchor2" ),
301 $main = jQuery( "#main" ),
302 fakeClick = function($jq) {
303 // Prefer a native click so we don't get jQuery simulated bubbling
304 if ( $jq[0].click ) {
305 $jq[0].click(); // IE
307 else if ( document.createEvent ) {
308 var e = document.createEvent( 'MouseEvents' );
309 e.initEvent( "click", true, true );
310 $jq[0].dispatchEvent(e);
316 $anchor2.click(function(e) {
319 $main.delegate("#foo", "click", function(e) {
320 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
322 fakeClick( $anchor2 );
323 $anchor2.unbind( "click" );
324 $main.undelegate( "click" );
325 $anchor2.click(function(e) {
326 // Let the default action occur
328 $main.delegate("#foo", "click", function(e) {
329 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
331 fakeClick( $anchor2 );
332 $anchor2.unbind( "click" );
333 $main.undelegate( "click" );
336 test("bind(), iframes", function() {
337 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
338 var doc = jQuery("#loadediframe").contents();
340 jQuery("div", doc).bind("click", function() {
341 ok( true, "Binding to element inside iframe" );
342 }).click().unbind('click');
345 test("bind(), trigger change on select", function() {
348 function selectOnChange(event) {
349 equals( event.data, counter++, "Event.data is not a global event object" );
351 jQuery("#form select").each(function(i){
352 jQuery(this).bind('change', i, selectOnChange);
353 }).trigger('change');
356 test("bind(), namespaced events, cloned events", function() {
359 jQuery("#firstp").bind("custom.test",function(e){
360 ok(true, "Custom event triggered");
363 jQuery("#firstp").bind("click",function(e){
364 ok(true, "Normal click triggered");
367 jQuery("#firstp").bind("click.test",function(e){
368 ok(true, "Namespaced click triggered");
371 // Trigger both bound fn (2)
372 jQuery("#firstp").trigger("click");
374 // Trigger one bound fn (1)
375 jQuery("#firstp").trigger("click.test");
377 // Remove only the one fn
378 jQuery("#firstp").unbind("click.test");
380 // Trigger the remaining fn (1)
381 jQuery("#firstp").trigger("click");
383 // Remove the remaining fn
384 jQuery("#firstp").unbind(".test");
386 // Trigger the remaining fn (0)
387 jQuery("#firstp").trigger("custom");
389 // using contents will get comments regular, text, and comment nodes
390 jQuery("#nonnodes").contents().bind("tester", function () {
391 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
392 }).trigger("tester");
394 // Make sure events stick with appendTo'd elements (which are cloned) #2027
395 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
396 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
399 test("bind(), multi-namespaced events", function() {
411 function check(name, msg){
412 same(name, order.shift(), msg);
415 jQuery("#firstp").bind("custom.test",function(e){
416 check("custom.test", "Custom event triggered");
419 jQuery("#firstp").bind("custom.test2",function(e){
420 check("custom.test2", "Custom event triggered");
423 jQuery("#firstp").bind("click.test",function(e){
424 check("click.test", "Normal click triggered");
427 jQuery("#firstp").bind("click.test.abc",function(e){
428 check("click.test.abc", "Namespaced click triggered");
431 // Those would not trigger/unbind (#5303)
432 jQuery("#firstp").trigger("click.a.test");
433 jQuery("#firstp").unbind("click.a.test");
435 // Trigger both bound fn (1)
436 jQuery("#firstp").trigger("click.test.abc");
438 // Trigger one bound fn (1)
439 jQuery("#firstp").trigger("click.abc");
441 // Trigger two bound fn (2)
442 jQuery("#firstp").trigger("click.test");
444 // Remove only the one fn
445 jQuery("#firstp").unbind("click.abc");
447 // Trigger the remaining fn (1)
448 jQuery("#firstp").trigger("click");
450 // Remove the remaining fn
451 jQuery("#firstp").unbind(".test");
453 // Trigger the remaining fn (1)
454 jQuery("#firstp").trigger("custom");
457 test("bind(), with same function", function() {
460 var count = 0 , func = function(){
464 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
465 jQuery("#liveHandlerOrder").trigger("foo.bar");
467 equals(count, 1, "Verify binding function with multiple namespaces." );
469 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
470 jQuery("#liveHandlerOrder").trigger("foo.bar");
472 equals(count, 1, "Verify that removing events still work." );
475 test("bind(), make sure order is maintained", function() {
478 var elem = jQuery("#firstp"), log = [], check = [];
480 for ( var i = 0; i < 100; i++ ) (function(i){
481 elem.bind( "click", function(){
488 elem.trigger("click");
490 equals( log.join(","), check.join(","), "Make sure order was maintained." );
492 elem.unbind("click");
495 test("bind(), with different this object", function() {
497 var thisObject = { myThis: true },
498 data = { myData: true },
499 handler1 = function( event ) {
500 equals( this, thisObject, "bind() with different this object" );
502 handler2 = function( event ) {
503 equals( this, thisObject, "bind() with different this object and data" );
504 equals( event.data, data, "bind() with different this object and data" );
508 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
509 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
511 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
514 test("bind(name, false), unbind(name, false)", function() {
518 jQuery("#main").bind("click", function(e){ main++; });
519 jQuery("#ap").trigger("click");
520 equals( main, 1, "Verify that the trigger happened correctly." );
523 jQuery("#ap").bind("click", false);
524 jQuery("#ap").trigger("click");
525 equals( main, 0, "Verify that no bubble happened." );
528 jQuery("#ap").unbind("click", false);
529 jQuery("#ap").trigger("click");
530 equals( main, 1, "Verify that the trigger happened correctly." );
533 test("bind()/trigger()/unbind() on plain object", function() {
538 // Make sure it doesn't complain when no events are found
539 jQuery(obj).trigger("test");
541 // Make sure it doesn't complain when no events are found
542 jQuery(obj).unbind("test");
546 ok( true, "Custom event run." );
549 ok( true, "Custom submit event run." );
553 var events = jQuery(obj).data("__events__");
554 ok( events, "Object has events bound." );
555 equals( obj.events, undefined, "Events object on plain objects is not events" );
556 equals( typeof events, "function", "'events' expando is a function on plain objects." );
557 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
558 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
561 jQuery(obj).trigger("test");
562 jQuery(obj).trigger("submit");
564 jQuery(obj).unbind("test");
565 jQuery(obj).unbind("submit");
568 jQuery(obj).trigger("test");
570 // Make sure it doesn't complain when no events are found
571 jQuery(obj).unbind("test");
573 equals( obj.__events__, undefined, "Make sure events object is removed" );
576 test("unbind(type)", function() {
579 var $elem = jQuery("#firstp"),
583 ok( false, message );
586 message = "unbind passing function";
587 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
589 message = "unbind all from event";
590 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
592 message = "unbind all";
593 $elem.bind('error1', error).unbind().triggerHandler('error1');
595 message = "unbind many with function";
596 $elem.bind('error1 error2',error)
597 .unbind('error1 error2', error )
598 .trigger('error1').triggerHandler('error2');
600 message = "unbind many"; // #3538
601 $elem.bind('error1 error2',error)
602 .unbind('error1 error2')
603 .trigger('error1').triggerHandler('error2');
605 message = "unbind without a type or handler";
606 $elem.bind("error1 error2.test",error)
608 .trigger("error1").triggerHandler("error2");
611 test("unbind(eventObject)", function() {
614 var $elem = jQuery("#firstp"),
617 function assert( expected ){
619 $elem.trigger('foo').triggerHandler('bar');
620 equals( num, expected, "Check the right handlers are triggered" );
624 // This handler shouldn't be unbound
625 .bind('foo', function(){
628 .bind('foo', function(e){
633 .bind('bar', function(){
647 test("hover()", function() {
649 handler1 = function( event ) { ++times; },
650 handler2 = function( event ) { ++times; };
653 .hover(handler1, handler2)
654 .mouseenter().mouseleave()
655 .unbind("mouseenter", handler1)
656 .unbind("mouseleave", handler2)
658 .mouseenter().mouseleave()
659 .unbind("mouseenter mouseleave", handler1)
660 .mouseenter().mouseleave();
662 equals( times, 4, "hover handlers fired" );
665 test("trigger() shortcuts", function() {
667 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
668 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
669 equals( close.length, 0, "Context element does not exist, length must be zero" );
670 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
674 jQuery("#check1").click(function() {
675 ok( true, "click event handler for checkbox gets fired twice, see #815" );
679 jQuery('#firstp')[0].onclick = function(event) {
682 jQuery('#firstp').click();
683 equals( counter, 1, "Check that click, triggers onclick event handler also" );
685 var clickCounter = 0;
686 jQuery('#simon1')[0].onclick = function(event) {
689 jQuery('#simon1').click();
690 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
692 jQuery('<img />').load(function(){
693 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
697 test("trigger() bubbling", function() {
700 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
702 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
703 jQuery("html").bind("click", function(e){ html++; });
704 jQuery("body").bind("click", function(e){ body++; });
705 jQuery("#main").bind("click", function(e){ main++; });
706 jQuery("#ap").bind("click", function(){ ap++; return false; });
708 jQuery("html").trigger("click");
709 equals( doc, 1, "HTML bubble" );
710 equals( html, 1, "HTML bubble" );
712 jQuery("body").trigger("click");
713 equals( doc, 2, "Body bubble" );
714 equals( html, 2, "Body bubble" );
715 equals( body, 1, "Body bubble" );
717 jQuery("#main").trigger("click");
718 equals( doc, 3, "Main bubble" );
719 equals( html, 3, "Main bubble" );
720 equals( body, 2, "Main bubble" );
721 equals( main, 1, "Main bubble" );
723 jQuery("#ap").trigger("click");
724 equals( doc, 3, "ap bubble" );
725 equals( html, 3, "ap bubble" );
726 equals( body, 2, "ap bubble" );
727 equals( main, 1, "ap bubble" );
728 equals( ap, 1, "ap bubble" );
731 test("trigger(type, [data], [fn])", function() {
734 var handler = function(event, a, b, c) {
735 equals( event.type, "click", "check passed data" );
736 equals( a, 1, "check passed data" );
737 equals( b, "2", "check passed data" );
738 equals( c, "abc", "check passed data" );
742 var $elem = jQuery("#firstp");
744 // Simulate a "native" click
745 $elem[0].click = function(){
746 ok( true, "Native call was triggered" );
749 // Triggers handlrs and native
751 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
753 // Simulate a "native" click
754 $elem[0].click = function(){
755 ok( false, "Native call was triggered" );
758 // Trigger only the handlers (no native)
760 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
764 jQuery('#form input:first').hide().trigger('focus');
768 ok( pass, "Trigger focus on hidden element" );
772 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
776 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
778 var form = jQuery("<form action=''></form>").appendTo("body");
780 // Make sure it can be prevented locally
781 form.submit(function(){
782 ok( true, "Local bind still works." );
787 form.trigger("submit");
789 form.unbind("submit");
791 jQuery(document).submit(function(){
792 ok( true, "Make sure bubble works up to document." );
797 form.trigger("submit");
799 jQuery(document).unbind("submit");
804 test("jQuery.Event.currentTarget", function(){
807 test("trigger(eventObject, [data], [fn])", function() {
810 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
811 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
813 var event = jQuery.Event("noNew");
814 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
815 equals( event.type, "noNew", "Verify its type" );
817 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
818 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
819 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
821 event.preventDefault();
822 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
823 event.stopPropagation();
824 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
826 event.isPropagationStopped = function(){ return false };
827 event.stopImmediatePropagation();
828 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
829 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
831 $parent.bind('foo',function(e){
833 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
834 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
835 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
836 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
839 // test with an event object
840 event = new jQuery.Event("foo");
841 event.secret = 'boo!';
842 $child.trigger(event);
844 // test with a literal object
845 $child.trigger({type:'foo', secret:'boo!'});
850 ok( false, "This assertion shouldn't be reached");
853 $parent.bind('foo', error );
855 $child.bind('foo',function(e, a, b, c ){
856 equals( arguments.length, 4, "Check arguments length");
857 equals( a, 1, "Check first custom argument");
858 equals( b, 2, "Check second custom argument");
859 equals( c, 3, "Check third custom argument");
861 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
862 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
863 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
866 e.stopImmediatePropagation();
871 // We should add this back in when we want to test the order
872 // in which event handlers are iterated.
873 //$child.bind('foo', error );
875 event = new jQuery.Event("foo");
876 $child.trigger( event, [1,2,3] ).unbind();
877 equals( event.result, "result", "Check event.result attribute");
879 // Will error if it bubbles
880 $child.triggerHandler('foo');
883 $parent.unbind().remove();
886 test("jQuery.Event.currentTarget", function(){
890 $elem = jQuery('<button>a</button>').click(function(e){
891 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
895 $elem.trigger('click');
901 test("toggle(Function, Function, ...)", function() {
905 fn1 = function(e) { count++; },
906 fn2 = function(e) { count--; },
907 preventDefault = function(e) { e.preventDefault() },
908 link = jQuery('#mark');
909 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
910 equals( count, 1, "Check for toggle(fn, fn)" );
912 jQuery("#firstp").toggle(function () {
913 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
914 }, function() {}).trigger("click", [ 1, 2, 3 ]);
917 jQuery("#simon1").one("click", function() {
918 ok( true, "Execute event only once" );
919 jQuery(this).toggle(function() {
920 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
922 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
925 }).click().click().click();
940 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
942 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
944 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
946 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
948 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
950 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
952 $div.unbind('click',fns[0]);
953 var data = jQuery.data( $div[0], 'events' );
954 ok( !data, "Unbinding one function from toggle unbinds them all");
956 // Test Multi-Toggles
958 $div = jQuery("<div/>");
959 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
961 same( a, [1], "Check that a click worked." );
963 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
965 same( a, [1,2], "Check that a click worked with a second toggle." );
966 same( b, [1], "Check that a click worked with a second toggle." );
969 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
970 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
973 test(".live()/.die()", function() {
976 var submit = 0, div = 0, livea = 0, liveb = 0;
978 jQuery("div").live("submit", function(){ submit++; return false; });
979 jQuery("div").live("click", function(){ div++; });
980 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
981 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
983 // Nothing should trigger on the body
984 jQuery("body").trigger("click");
985 equals( submit, 0, "Click on body" );
986 equals( div, 0, "Click on body" );
987 equals( livea, 0, "Click on body" );
988 equals( liveb, 0, "Click on body" );
990 // This should trigger two events
991 submit = 0, div = 0, livea = 0, liveb = 0;
992 jQuery("div#nothiddendiv").trigger("click");
993 equals( submit, 0, "Click on div" );
994 equals( div, 1, "Click on div" );
995 equals( livea, 1, "Click on div" );
996 equals( liveb, 0, "Click on div" );
998 // This should trigger three events (w/ bubbling)
999 submit = 0, div = 0, livea = 0, liveb = 0;
1000 jQuery("div#nothiddendivchild").trigger("click");
1001 equals( submit, 0, "Click on inner div" );
1002 equals( div, 2, "Click on inner div" );
1003 equals( livea, 1, "Click on inner div" );
1004 equals( liveb, 1, "Click on inner div" );
1006 // This should trigger one submit
1007 submit = 0, div = 0, livea = 0, liveb = 0;
1008 jQuery("div#nothiddendivchild").trigger("submit");
1009 equals( submit, 1, "Submit on div" );
1010 equals( div, 0, "Submit on div" );
1011 equals( livea, 0, "Submit on div" );
1012 equals( liveb, 0, "Submit on div" );
1014 // Make sure no other events were removed in the process
1015 submit = 0, div = 0, livea = 0, liveb = 0;
1016 jQuery("div#nothiddendivchild").trigger("click");
1017 equals( submit, 0, "die Click on inner div" );
1018 equals( div, 2, "die Click on inner div" );
1019 equals( livea, 1, "die Click on inner div" );
1020 equals( liveb, 1, "die Click on inner div" );
1022 // Now make sure that the removal works
1023 submit = 0, div = 0, livea = 0, liveb = 0;
1024 jQuery("div#nothiddendivchild").die("click");
1025 jQuery("div#nothiddendivchild").trigger("click");
1026 equals( submit, 0, "die Click on inner div" );
1027 equals( div, 2, "die Click on inner div" );
1028 equals( livea, 1, "die Click on inner div" );
1029 equals( liveb, 0, "die Click on inner div" );
1031 // Make sure that the click wasn't removed too early
1032 submit = 0, div = 0, livea = 0, liveb = 0;
1033 jQuery("div#nothiddendiv").trigger("click");
1034 equals( submit, 0, "die Click on inner div" );
1035 equals( div, 1, "die Click on inner div" );
1036 equals( livea, 1, "die Click on inner div" );
1037 equals( liveb, 0, "die Click on inner div" );
1039 // Make sure that stopPropgation doesn't stop live events
1040 submit = 0, div = 0, livea = 0, liveb = 0;
1041 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1042 jQuery("div#nothiddendivchild").trigger("click");
1043 equals( submit, 0, "stopPropagation Click on inner div" );
1044 equals( div, 1, "stopPropagation Click on inner div" );
1045 equals( livea, 0, "stopPropagation Click on inner div" );
1046 equals( liveb, 1, "stopPropagation Click on inner div" );
1048 // Make sure click events only fire with primary click
1049 submit = 0, div = 0, livea = 0, liveb = 0;
1050 var event = jQuery.Event("click");
1052 jQuery("div#nothiddendiv").trigger(event);
1054 equals( livea, 0, "live secondary click" );
1056 jQuery("div#nothiddendivchild").die("click");
1057 jQuery("div#nothiddendiv").die("click");
1058 jQuery("div").die("click");
1059 jQuery("div").die("submit");
1061 // Test binding with a different context
1062 var clicked = 0, container = jQuery('#main')[0];
1063 jQuery("#foo", container).live("click", function(e){ clicked++; });
1064 jQuery("div").trigger('click');
1065 jQuery("#foo").trigger('click');
1066 jQuery("#main").trigger('click');
1067 jQuery("body").trigger('click');
1068 equals( clicked, 2, "live with a context" );
1070 // Make sure the event is actually stored on the context
1071 ok( jQuery.data(container, "events").live, "live with a context" );
1073 // Test unbinding with a different context
1074 jQuery("#foo", container).die("click");
1075 jQuery("#foo").trigger('click');
1076 equals( clicked, 2, "die with a context");
1078 // Test binding with event data
1079 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1080 jQuery("#foo").trigger("click").die("click");
1082 // Test binding with trigger data
1083 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1084 jQuery("#foo").trigger("click", true).die("click");
1086 // Test binding with different this object
1087 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1088 jQuery("#foo").trigger("click").die("click");
1090 // Test binding with different this object, event data, and trigger data
1091 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1092 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1093 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1094 equals( data, true, "live with with different this object, event data, and trigger data")
1095 }, { foo: "bar" }));
1096 jQuery("#foo").trigger("click", true).die("click");
1098 // Verify that return false prevents default action
1099 jQuery("#anchor2").live("click", function(){ return false; });
1100 var hash = window.location.hash;
1101 jQuery("#anchor2").trigger("click");
1102 equals( window.location.hash, hash, "return false worked" );
1103 jQuery("#anchor2").die("click");
1105 // Verify that .preventDefault() prevents default action
1106 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1107 var hash = window.location.hash;
1108 jQuery("#anchor2").trigger("click");
1109 equals( window.location.hash, hash, "e.preventDefault() worked" );
1110 jQuery("#anchor2").die("click");
1112 // Test binding the same handler to multiple points
1114 function callback(){ called++; return false; }
1116 jQuery("#nothiddendiv").live("click", callback);
1117 jQuery("#anchor2").live("click", callback);
1119 jQuery("#nothiddendiv").trigger("click");
1120 equals( called, 1, "Verify that only one click occurred." );
1123 jQuery("#anchor2").trigger("click");
1124 equals( called, 1, "Verify that only one click occurred." );
1126 // Make sure that only one callback is removed
1127 jQuery("#anchor2").die("click", callback);
1130 jQuery("#nothiddendiv").trigger("click");
1131 equals( called, 1, "Verify that only one click occurred." );
1134 jQuery("#anchor2").trigger("click");
1135 equals( called, 0, "Verify that no click occurred." );
1137 // Make sure that it still works if the selector is the same,
1138 // but the event type is different
1139 jQuery("#nothiddendiv").live("foo", callback);
1142 jQuery("#nothiddendiv").die("click", callback);
1145 jQuery("#nothiddendiv").trigger("click");
1146 equals( called, 0, "Verify that no click occurred." );
1149 jQuery("#nothiddendiv").trigger("foo");
1150 equals( called, 1, "Verify that one foo occurred." );
1153 jQuery("#nothiddendiv").die("foo", callback);
1155 // Make sure we don't loose the target by DOM modifications
1156 // after the bubble already reached the liveHandler
1157 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1159 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1160 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1162 jQuery("#nothiddendiv span").click();
1163 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1164 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1167 jQuery("#nothiddendivchild").die("click");
1169 // Verify that .live() ocurs and cancel buble in the same order as
1170 // we would expect .bind() and .click() without delegation
1171 var lived = 0, livee = 0;
1173 // bind one pair in one order
1174 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1175 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1177 jQuery('span#liveSpan1 a').click();
1178 equals( lived, 1, "Verify that only one first handler occurred." );
1179 equals( livee, 0, "Verify that second handler doesn't." );
1181 // and one pair in inverse
1182 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1183 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1187 jQuery('span#liveSpan2 a').click();
1188 equals( lived, 1, "Verify that only one first handler occurred." );
1189 equals( livee, 0, "Verify that second handler doesn't." );
1192 jQuery("span#liveSpan1 a").die("click")
1193 jQuery("span#liveSpan1").die("click");
1194 jQuery("span#liveSpan2 a").die("click");
1195 jQuery("span#liveSpan2").die("click");
1197 // Test this, target and currentTarget are correct
1198 jQuery('span#liveSpan1').live('click', function(e){
1199 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1200 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1201 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1204 jQuery('span#liveSpan1 a').click();
1206 jQuery('span#liveSpan1').die('click');
1208 // Work with deep selectors
1211 function clickB(){ livee++; }
1213 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1214 jQuery("#nothiddendiv div").live("click", clickB);
1215 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1217 equals( livee, 0, "No clicks, deep selector." );
1220 jQuery("#nothiddendivchild").trigger("click");
1221 equals( livee, 2, "Click, deep selector." );
1224 jQuery("#nothiddendivchild").trigger("mouseover");
1225 equals( livee, 1, "Mouseover, deep selector." );
1227 jQuery("#nothiddendiv div").die("mouseover");
1230 jQuery("#nothiddendivchild").trigger("click");
1231 equals( livee, 2, "Click, deep selector." );
1234 jQuery("#nothiddendivchild").trigger("mouseover");
1235 equals( livee, 0, "Mouseover, deep selector." );
1237 jQuery("#nothiddendiv div").die("click", clickB);
1240 jQuery("#nothiddendivchild").trigger("click");
1241 equals( livee, 1, "Click, deep selector." );
1243 jQuery("#nothiddendiv div").die("click");
1245 jQuery("#nothiddendiv div").live("blur", function(){
1246 ok( true, "Live div trigger blur." );
1249 jQuery("#nothiddendiv div").trigger("blur");
1251 jQuery("#nothiddendiv div").die("blur");
1254 test("die all bound events", function(){
1258 var div = jQuery("div#nothiddendivchild");
1260 div.live("click submit", function(){ count++; });
1263 div.trigger("click");
1264 div.trigger("submit");
1266 equals( count, 0, "Make sure no events were triggered." );
1269 test("live with multiple events", function(){
1273 var div = jQuery("div#nothiddendivchild");
1275 div.live("click submit", function(){ count++; });
1277 div.trigger("click");
1278 div.trigger("submit");
1280 equals( count, 2, "Make sure both the click and submit were triggered." );
1283 test("live with namespaces", function(){
1286 var count1 = 0, count2 = 0;
1288 jQuery("#liveSpan1").live("foo.bar", function(e){
1292 jQuery("#liveSpan1").live("foo.zed", function(e){
1296 jQuery("#liveSpan1").trigger("foo.bar");
1297 equals( count1, 1, "Got live foo.bar" );
1298 equals( count2, 0, "Got live foo.bar" );
1300 count1 = 0, count2 = 0;
1302 jQuery("#liveSpan1").trigger("foo.zed");
1303 equals( count1, 0, "Got live foo.zed" );
1304 equals( count2, 1, "Got live foo.zed" );
1307 count1 = 0, count2 = 0;
1309 jQuery("#liveSpan1").die("foo.zed");
1310 jQuery("#liveSpan1").trigger("foo.bar");
1312 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1313 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1315 count1 = 0, count2 = 0;
1317 jQuery("#liveSpan1").trigger("foo.zed");
1318 equals( count1, 0, "Got live foo.zed" );
1319 equals( count2, 0, "Got live foo.zed" );
1322 jQuery("#liveSpan1").die("foo.bar");
1324 count1 = 0, count2 = 0;
1326 jQuery("#liveSpan1").trigger("foo.bar");
1327 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1328 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1330 jQuery("#liveSpan1").trigger("foo.zed");
1331 equals( count1, 0, "Did not trigger foo.zed again" );
1332 equals( count2, 0, "Did not trigger foo.zed again" );
1335 test("live with change", function(){
1338 var selectChange = 0, checkboxChange = 0;
1340 var select = jQuery("select[name='S1']")
1341 select.live("change", function() {
1345 var checkbox = jQuery("#check2"),
1346 checkboxFunction = function(){
1349 checkbox.live("change", checkboxFunction);
1351 // test click on select
1353 // second click that changed it
1355 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1356 select.trigger("change");
1357 equals( selectChange, 1, "Change on click." );
1359 // test keys on select
1361 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1362 select.trigger("change");
1363 equals( selectChange, 1, "Change on keyup." );
1365 // test click on checkbox
1366 checkbox.trigger("change");
1367 equals( checkboxChange, 1, "Change on checkbox." );
1369 // test blur/focus on text
1370 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1371 text.live("change", function() {
1375 text.val(oldTextVal+"foo");
1376 text.trigger("change");
1377 equals( textChange, 1, "Change on text input." );
1379 text.val(oldTextVal);
1382 // test blur/focus on password
1383 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1384 password.live("change", function() {
1388 password.val(oldPasswordVal + "foo");
1389 password.trigger("change");
1390 equals( passwordChange, 1, "Change on password input." );
1392 password.val(oldPasswordVal);
1393 password.die("change");
1395 // make sure die works
1399 select.die("change");
1400 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1401 select.trigger("change");
1402 equals( selectChange, 0, "Die on click works." );
1405 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1406 select.trigger("change");
1407 equals( selectChange, 0, "Die on keyup works." );
1409 // die specific checkbox
1410 checkbox.die("change", checkboxFunction);
1411 checkbox.trigger("change");
1412 equals( checkboxChange, 1, "Die on checkbox." );
1415 test("live with submit", function() {
1416 var count1 = 0, count2 = 0;
1418 jQuery("#testForm").live("submit", function(ev) {
1420 ev.preventDefault();
1423 jQuery("body").live("submit", function(ev) {
1425 ev.preventDefault();
1428 jQuery("#testForm input[name=sub1]").submit();
1429 equals( count1, 1, "Verify form submit." );
1430 equals( count2, 1, "Verify body submit." );
1432 jQuery("#testForm").die("submit");
1433 jQuery("body").die("submit");
1436 test("live with special events", function() {
1439 jQuery.event.special.foo = {
1440 setup: function( data, namespaces, handler ) {
1441 ok( true, "Setup run." );
1443 teardown: function( namespaces ) {
1444 ok( true, "Teardown run." );
1446 add: function( handleObj ) {
1447 ok( true, "Add run." );
1449 remove: function( handleObj ) {
1450 ok( true, "Remove run." );
1452 _default: function( event ) {
1453 ok( true, "Default run." );
1458 jQuery("#liveSpan1").live("foo.a", function(e){
1459 ok( true, "Handler 1 run." );
1463 jQuery("#liveSpan1").live("foo.b", function(e){
1464 ok( true, "Handler 2 run." );
1467 // Run: Handler 1, Handler 2, Default
1468 jQuery("#liveSpan1").trigger("foo");
1470 // Run: Handler 1, Default
1471 // TODO: Namespace doesn't trigger default (?)
1472 jQuery("#liveSpan1").trigger("foo.a");
1475 jQuery("#liveSpan1").die("foo.a");
1477 // Run: Handler 2, Default
1478 jQuery("#liveSpan1").trigger("foo");
1480 // Run: remove, teardown
1481 jQuery("#liveSpan1").die("foo");
1483 delete jQuery.event.special.foo;
1486 test(".delegate()/.undelegate()", function() {
1489 var submit = 0, div = 0, livea = 0, liveb = 0;
1491 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1492 jQuery("#body").delegate("div", "click", function(){ div++; });
1493 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1494 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1496 // Nothing should trigger on the body
1497 jQuery("body").trigger("click");
1498 equals( submit, 0, "Click on body" );
1499 equals( div, 0, "Click on body" );
1500 equals( livea, 0, "Click on body" );
1501 equals( liveb, 0, "Click on body" );
1503 // This should trigger two events
1504 submit = 0, div = 0, livea = 0, liveb = 0;
1505 jQuery("div#nothiddendiv").trigger("click");
1506 equals( submit, 0, "Click on div" );
1507 equals( div, 1, "Click on div" );
1508 equals( livea, 1, "Click on div" );
1509 equals( liveb, 0, "Click on div" );
1511 // This should trigger three events (w/ bubbling)
1512 submit = 0, div = 0, livea = 0, liveb = 0;
1513 jQuery("div#nothiddendivchild").trigger("click");
1514 equals( submit, 0, "Click on inner div" );
1515 equals( div, 2, "Click on inner div" );
1516 equals( livea, 1, "Click on inner div" );
1517 equals( liveb, 1, "Click on inner div" );
1519 // This should trigger one submit
1520 submit = 0, div = 0, livea = 0, liveb = 0;
1521 jQuery("div#nothiddendivchild").trigger("submit");
1522 equals( submit, 1, "Submit on div" );
1523 equals( div, 0, "Submit on div" );
1524 equals( livea, 0, "Submit on div" );
1525 equals( liveb, 0, "Submit on div" );
1527 // Make sure no other events were removed in the process
1528 submit = 0, div = 0, livea = 0, liveb = 0;
1529 jQuery("div#nothiddendivchild").trigger("click");
1530 equals( submit, 0, "undelegate Click on inner div" );
1531 equals( div, 2, "undelegate Click on inner div" );
1532 equals( livea, 1, "undelegate Click on inner div" );
1533 equals( liveb, 1, "undelegate Click on inner div" );
1535 // Now make sure that the removal works
1536 submit = 0, div = 0, livea = 0, liveb = 0;
1537 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1538 jQuery("div#nothiddendivchild").trigger("click");
1539 equals( submit, 0, "undelegate Click on inner div" );
1540 equals( div, 2, "undelegate Click on inner div" );
1541 equals( livea, 1, "undelegate Click on inner div" );
1542 equals( liveb, 0, "undelegate Click on inner div" );
1544 // Make sure that the click wasn't removed too early
1545 submit = 0, div = 0, livea = 0, liveb = 0;
1546 jQuery("div#nothiddendiv").trigger("click");
1547 equals( submit, 0, "undelegate Click on inner div" );
1548 equals( div, 1, "undelegate Click on inner div" );
1549 equals( livea, 1, "undelegate Click on inner div" );
1550 equals( liveb, 0, "undelegate Click on inner div" );
1552 // Make sure that stopPropgation doesn't stop live events
1553 submit = 0, div = 0, livea = 0, liveb = 0;
1554 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1555 jQuery("div#nothiddendivchild").trigger("click");
1556 equals( submit, 0, "stopPropagation Click on inner div" );
1557 equals( div, 1, "stopPropagation Click on inner div" );
1558 equals( livea, 0, "stopPropagation Click on inner div" );
1559 equals( liveb, 1, "stopPropagation Click on inner div" );
1561 // Make sure click events only fire with primary click
1562 submit = 0, div = 0, livea = 0, liveb = 0;
1563 var event = jQuery.Event("click");
1565 jQuery("div#nothiddendiv").trigger(event);
1567 equals( livea, 0, "delegate secondary click" );
1569 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1570 jQuery("#body").undelegate("div#nothiddendiv", "click");
1571 jQuery("#body").undelegate("div", "click");
1572 jQuery("#body").undelegate("div", "submit");
1574 // Test binding with a different context
1575 var clicked = 0, container = jQuery('#main')[0];
1576 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1577 jQuery("div").trigger('click');
1578 jQuery("#foo").trigger('click');
1579 jQuery("#main").trigger('click');
1580 jQuery("body").trigger('click');
1581 equals( clicked, 2, "delegate with a context" );
1583 // Make sure the event is actually stored on the context
1584 ok( jQuery.data(container, "events").live, "delegate with a context" );
1586 // Test unbinding with a different context
1587 jQuery("#main").undelegate("#foo", "click");
1588 jQuery("#foo").trigger('click');
1589 equals( clicked, 2, "undelegate with a context");
1591 // Test binding with event data
1592 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1593 jQuery("#foo").trigger("click");
1594 jQuery("#body").undelegate("#foo", "click");
1596 // Test binding with trigger data
1597 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1598 jQuery("#foo").trigger("click", true);
1599 jQuery("#body").undelegate("#foo", "click");
1601 // Test binding with different this object
1602 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1603 jQuery("#foo").trigger("click");
1604 jQuery("#body").undelegate("#foo", "click");
1606 // Test binding with different this object, event data, and trigger data
1607 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1608 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1609 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1610 equals( data, true, "delegate with with different this object, event data, and trigger data")
1611 }, { foo: "bar" }));
1612 jQuery("#foo").trigger("click", true);
1613 jQuery("#body").undelegate("#foo", "click");
1615 // Verify that return false prevents default action
1616 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1617 var hash = window.location.hash;
1618 jQuery("#anchor2").trigger("click");
1619 equals( window.location.hash, hash, "return false worked" );
1620 jQuery("#body").undelegate("#anchor2", "click");
1622 // Verify that .preventDefault() prevents default action
1623 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1624 var hash = window.location.hash;
1625 jQuery("#anchor2").trigger("click");
1626 equals( window.location.hash, hash, "e.preventDefault() worked" );
1627 jQuery("#body").undelegate("#anchor2", "click");
1629 // Test binding the same handler to multiple points
1631 function callback(){ called++; return false; }
1633 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1634 jQuery("#body").delegate("#anchor2", "click", callback);
1636 jQuery("#nothiddendiv").trigger("click");
1637 equals( called, 1, "Verify that only one click occurred." );
1640 jQuery("#anchor2").trigger("click");
1641 equals( called, 1, "Verify that only one click occurred." );
1643 // Make sure that only one callback is removed
1644 jQuery("#body").undelegate("#anchor2", "click", callback);
1647 jQuery("#nothiddendiv").trigger("click");
1648 equals( called, 1, "Verify that only one click occurred." );
1651 jQuery("#anchor2").trigger("click");
1652 equals( called, 0, "Verify that no click occurred." );
1654 // Make sure that it still works if the selector is the same,
1655 // but the event type is different
1656 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1659 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1662 jQuery("#nothiddendiv").trigger("click");
1663 equals( called, 0, "Verify that no click occurred." );
1666 jQuery("#nothiddendiv").trigger("foo");
1667 equals( called, 1, "Verify that one foo occurred." );
1670 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1672 // Make sure we don't loose the target by DOM modifications
1673 // after the bubble already reached the liveHandler
1674 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1676 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1677 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1679 jQuery("#nothiddendiv span").click();
1680 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1681 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1684 jQuery("#body").undelegate("#nothiddendivchild", "click");
1686 // Verify that .live() ocurs and cancel buble in the same order as
1687 // we would expect .bind() and .click() without delegation
1688 var lived = 0, livee = 0;
1690 // bind one pair in one order
1691 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1692 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1694 jQuery('span#liveSpan1 a').click();
1695 equals( lived, 1, "Verify that only one first handler occurred." );
1696 equals( livee, 0, "Verify that second handler doesn't." );
1698 // and one pair in inverse
1699 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1700 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1704 jQuery('span#liveSpan2 a').click();
1705 equals( lived, 1, "Verify that only one first handler occurred." );
1706 equals( livee, 0, "Verify that second handler doesn't." );
1709 jQuery("#body").undelegate("click");
1711 // Test this, target and currentTarget are correct
1712 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1713 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1714 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1715 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1718 jQuery('span#liveSpan1 a').click();
1720 jQuery("#body").undelegate('span#liveSpan1', 'click');
1722 // Work with deep selectors
1725 function clickB(){ livee++; }
1727 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1728 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1729 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1731 equals( livee, 0, "No clicks, deep selector." );
1734 jQuery("#nothiddendivchild").trigger("click");
1735 equals( livee, 2, "Click, deep selector." );
1738 jQuery("#nothiddendivchild").trigger("mouseover");
1739 equals( livee, 1, "Mouseover, deep selector." );
1741 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1744 jQuery("#nothiddendivchild").trigger("click");
1745 equals( livee, 2, "Click, deep selector." );
1748 jQuery("#nothiddendivchild").trigger("mouseover");
1749 equals( livee, 0, "Mouseover, deep selector." );
1751 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1754 jQuery("#nothiddendivchild").trigger("click");
1755 equals( livee, 1, "Click, deep selector." );
1757 jQuery("#body").undelegate("#nothiddendiv div", "click");
1760 test("undelegate all bound events", function(){
1764 var div = jQuery("#body");
1766 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1769 jQuery("div#nothiddendivchild").trigger("click");
1770 jQuery("div#nothiddendivchild").trigger("submit");
1772 equals( count, 0, "Make sure no events were triggered." );
1775 test("delegate with multiple events", function(){
1779 var div = jQuery("#body");
1781 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1783 jQuery("div#nothiddendivchild").trigger("click");
1784 jQuery("div#nothiddendivchild").trigger("submit");
1786 equals( count, 2, "Make sure both the click and submit were triggered." );
1788 jQuery("#body").undelegate();
1791 test("delegate with change", function(){
1794 var selectChange = 0, checkboxChange = 0;
1796 var select = jQuery("select[name='S1']");
1797 jQuery("#body").delegate("select[name='S1']", "change", function() {
1801 var checkbox = jQuery("#check2"),
1802 checkboxFunction = function(){
1805 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1807 // test click on select
1809 // second click that changed it
1811 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1812 select.trigger("change");
1813 equals( selectChange, 1, "Change on click." );
1815 // test keys on select
1817 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1818 select.trigger("change");
1819 equals( selectChange, 1, "Change on keyup." );
1821 // test click on checkbox
1822 checkbox.trigger("change");
1823 equals( checkboxChange, 1, "Change on checkbox." );
1825 // test blur/focus on text
1826 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1827 jQuery("#body").delegate("#name", "change", function() {
1831 text.val(oldTextVal+"foo");
1832 text.trigger("change");
1833 equals( textChange, 1, "Change on text input." );
1835 text.val(oldTextVal);
1836 jQuery("#body").die("change");
1838 // test blur/focus on password
1839 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1840 jQuery("#body").delegate("#name", "change", function() {
1844 password.val(oldPasswordVal + "foo");
1845 password.trigger("change");
1846 equals( passwordChange, 1, "Change on password input." );
1848 password.val(oldPasswordVal);
1849 jQuery("#body").undelegate("#name", "change");
1851 // make sure die works
1855 jQuery("#body").undelegate("select[name='S1']", "change");
1856 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1857 select.trigger("change");
1858 equals( selectChange, 0, "Die on click works." );
1861 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1862 select.trigger("change");
1863 equals( selectChange, 0, "Die on keyup works." );
1865 // die specific checkbox
1866 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1867 checkbox.trigger("change");
1868 equals( checkboxChange, 1, "Die on checkbox." );
1871 test("delegate with submit", function() {
1872 var count1 = 0, count2 = 0;
1874 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1876 ev.preventDefault();
1879 jQuery(document).delegate("body", "submit", function(ev) {
1881 ev.preventDefault();
1884 jQuery("#testForm input[name=sub1]").submit();
1885 equals( count1, 1, "Verify form submit." );
1886 equals( count2, 1, "Verify body submit." );
1888 jQuery("#body").undelegate();
1889 jQuery(document).undelegate();
1892 test("Non DOM element events", function() {
1897 jQuery(o).bind('nonelementobj', function(e) {
1898 ok( true, "Event on non-DOM object triggered" );
1901 jQuery(o).trigger('nonelementobj');
1904 test("window resize", function() {
1907 jQuery(window).unbind();
1909 jQuery(window).bind("resize", function(){
1910 ok( true, "Resize event fired." );
1911 }).resize().unbind("resize");
1913 ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1916 test("focusin bubbles", function() {
1917 //create an input and focusin on it
1918 var input = jQuery("<input/>"), order = 0;
1920 input.prependTo("body");
1922 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1923 equals(1,order++,"focusin on the body second")
1926 input.bind("focusin.focusinBubblesTest",function(){
1927 equals(0,order++,"focusin on the element first")
1933 jQuery("body").unbind("focusin.focusinBubblesTest");
1937 test("jQuery(function($) {})", function() {
1939 jQuery(function($) {
1940 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");
1945 test("event properties", function() {
1947 jQuery("#simon1").click(function(event) {
1948 ok( event.timeStamp, "assert event.timeStamp is present" );