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 // 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 var orig = e.originalEvent;
318 if ( typeof(orig.defaultPrevented) === "boolean" || typeof(orig.returnValue) === "boolean" || orig.getPreventDefault ) {
319 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
322 // Opera < 11 doesn't implement any interface we can use, so give it a pass
323 ok( true, "isDefaultPrevented not supported by this browser, test skipped" );
326 fakeClick( $anchor2 );
327 $anchor2.unbind( "click" );
328 $main.undelegate( "click" );
329 $anchor2.click(function(e) {
330 // Let the default action occur
332 $main.delegate("#foo", "click", function(e) {
333 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
335 fakeClick( $anchor2 );
336 $anchor2.unbind( "click" );
337 $main.undelegate( "click" );
340 test("bind(), iframes", function() {
341 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
342 var doc = jQuery("#loadediframe").contents();
344 jQuery("div", doc).bind("click", function() {
345 ok( true, "Binding to element inside iframe" );
346 }).click().unbind('click');
349 test("bind(), trigger change on select", function() {
352 function selectOnChange(event) {
353 equals( event.data, counter++, "Event.data is not a global event object" );
355 jQuery("#form select").each(function(i){
356 jQuery(this).bind('change', i, selectOnChange);
357 }).trigger('change');
360 test("bind(), namespaced events, cloned events", function() {
363 jQuery("#firstp").bind("custom.test",function(e){
364 ok(true, "Custom event triggered");
367 jQuery("#firstp").bind("click",function(e){
368 ok(true, "Normal click triggered");
371 jQuery("#firstp").bind("click.test",function(e){
372 ok(true, "Namespaced click triggered");
375 // Trigger both bound fn (2)
376 jQuery("#firstp").trigger("click");
378 // Trigger one bound fn (1)
379 jQuery("#firstp").trigger("click.test");
381 // Remove only the one fn
382 jQuery("#firstp").unbind("click.test");
384 // Trigger the remaining fn (1)
385 jQuery("#firstp").trigger("click");
387 // Remove the remaining fn
388 jQuery("#firstp").unbind(".test");
390 // Trigger the remaining fn (0)
391 jQuery("#firstp").trigger("custom");
393 // using contents will get comments regular, text, and comment nodes
394 jQuery("#nonnodes").contents().bind("tester", function () {
395 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
396 }).trigger("tester");
398 // Make sure events stick with appendTo'd elements (which are cloned) #2027
399 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
400 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
403 test("bind(), multi-namespaced events", function() {
415 function check(name, msg){
416 same(name, order.shift(), msg);
419 jQuery("#firstp").bind("custom.test",function(e){
420 check("custom.test", "Custom event triggered");
423 jQuery("#firstp").bind("custom.test2",function(e){
424 check("custom.test2", "Custom event triggered");
427 jQuery("#firstp").bind("click.test",function(e){
428 check("click.test", "Normal click triggered");
431 jQuery("#firstp").bind("click.test.abc",function(e){
432 check("click.test.abc", "Namespaced click triggered");
435 // Those would not trigger/unbind (#5303)
436 jQuery("#firstp").trigger("click.a.test");
437 jQuery("#firstp").unbind("click.a.test");
439 // Trigger both bound fn (1)
440 jQuery("#firstp").trigger("click.test.abc");
442 // Trigger one bound fn (1)
443 jQuery("#firstp").trigger("click.abc");
445 // Trigger two bound fn (2)
446 jQuery("#firstp").trigger("click.test");
448 // Remove only the one fn
449 jQuery("#firstp").unbind("click.abc");
451 // Trigger the remaining fn (1)
452 jQuery("#firstp").trigger("click");
454 // Remove the remaining fn
455 jQuery("#firstp").unbind(".test");
457 // Trigger the remaining fn (1)
458 jQuery("#firstp").trigger("custom");
461 test("bind(), with same function", function() {
464 var count = 0 , func = function(){
468 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
469 jQuery("#liveHandlerOrder").trigger("foo.bar");
471 equals(count, 1, "Verify binding function with multiple namespaces." );
473 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
474 jQuery("#liveHandlerOrder").trigger("foo.bar");
476 equals(count, 1, "Verify that removing events still work." );
479 test("bind(), make sure order is maintained", function() {
482 var elem = jQuery("#firstp"), log = [], check = [];
484 for ( var i = 0; i < 100; i++ ) (function(i){
485 elem.bind( "click", function(){
492 elem.trigger("click");
494 equals( log.join(","), check.join(","), "Make sure order was maintained." );
496 elem.unbind("click");
499 test("bind(), with different this object", function() {
501 var thisObject = { myThis: true },
502 data = { myData: true },
503 handler1 = function( event ) {
504 equals( this, thisObject, "bind() with different this object" );
506 handler2 = function( event ) {
507 equals( this, thisObject, "bind() with different this object and data" );
508 equals( event.data, data, "bind() with different this object and data" );
512 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
513 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
515 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
518 test("bind(name, false), unbind(name, false)", function() {
522 jQuery("#main").bind("click", function(e){ main++; });
523 jQuery("#ap").trigger("click");
524 equals( main, 1, "Verify that the trigger happened correctly." );
527 jQuery("#ap").bind("click", false);
528 jQuery("#ap").trigger("click");
529 equals( main, 0, "Verify that no bubble happened." );
532 jQuery("#ap").unbind("click", false);
533 jQuery("#ap").trigger("click");
534 equals( main, 1, "Verify that the trigger happened correctly." );
537 test("bind()/trigger()/unbind() on plain object", function() {
542 // Make sure it doesn't complain when no events are found
543 jQuery(obj).trigger("test");
545 // Make sure it doesn't complain when no events are found
546 jQuery(obj).unbind("test");
550 ok( true, "Custom event run." );
553 ok( true, "Custom submit event run." );
557 var events = jQuery(obj).data("__events__");
558 ok( events, "Object has events bound." );
559 equals( obj.events, undefined, "Events object on plain objects is not events" );
560 equals( typeof events, "function", "'events' expando is a function on plain objects." );
561 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
562 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
565 jQuery(obj).trigger("test");
566 jQuery(obj).trigger("submit");
568 jQuery(obj).unbind("test");
569 jQuery(obj).unbind("submit");
572 jQuery(obj).trigger("test");
574 // Make sure it doesn't complain when no events are found
575 jQuery(obj).unbind("test");
577 equals( obj.__events__, undefined, "Make sure events object is removed" );
580 test("unbind(type)", function() {
583 var $elem = jQuery("#firstp"),
587 ok( false, message );
590 message = "unbind passing function";
591 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
593 message = "unbind all from event";
594 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
596 message = "unbind all";
597 $elem.bind('error1', error).unbind().triggerHandler('error1');
599 message = "unbind many with function";
600 $elem.bind('error1 error2',error)
601 .unbind('error1 error2', error )
602 .trigger('error1').triggerHandler('error2');
604 message = "unbind many"; // #3538
605 $elem.bind('error1 error2',error)
606 .unbind('error1 error2')
607 .trigger('error1').triggerHandler('error2');
609 message = "unbind without a type or handler";
610 $elem.bind("error1 error2.test",error)
612 .trigger("error1").triggerHandler("error2");
615 test("unbind(eventObject)", function() {
618 var $elem = jQuery("#firstp"),
621 function assert( expected ){
623 $elem.trigger('foo').triggerHandler('bar');
624 equals( num, expected, "Check the right handlers are triggered" );
628 // This handler shouldn't be unbound
629 .bind('foo', function(){
632 .bind('foo', function(e){
637 .bind('bar', function(){
651 test("hover()", function() {
653 handler1 = function( event ) { ++times; },
654 handler2 = function( event ) { ++times; };
657 .hover(handler1, handler2)
658 .mouseenter().mouseleave()
659 .unbind("mouseenter", handler1)
660 .unbind("mouseleave", handler2)
662 .mouseenter().mouseleave()
663 .unbind("mouseenter mouseleave", handler1)
664 .mouseenter().mouseleave();
666 equals( times, 4, "hover handlers fired" );
669 test("trigger() shortcuts", function() {
671 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
672 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
673 equals( close.length, 0, "Context element does not exist, length must be zero" );
674 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
678 jQuery("#check1").click(function() {
679 ok( true, "click event handler for checkbox gets fired twice, see #815" );
683 jQuery('#firstp')[0].onclick = function(event) {
686 jQuery('#firstp').click();
687 equals( counter, 1, "Check that click, triggers onclick event handler also" );
689 var clickCounter = 0;
690 jQuery('#simon1')[0].onclick = function(event) {
693 jQuery('#simon1').click();
694 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
696 jQuery('<img />').load(function(){
697 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
701 test("trigger() bubbling", function() {
704 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
706 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
707 jQuery("html").bind("click", function(e){ html++; });
708 jQuery("body").bind("click", function(e){ body++; });
709 jQuery("#main").bind("click", function(e){ main++; });
710 jQuery("#ap").bind("click", function(){ ap++; return false; });
712 jQuery("html").trigger("click");
713 equals( doc, 1, "HTML bubble" );
714 equals( html, 1, "HTML bubble" );
716 jQuery("body").trigger("click");
717 equals( doc, 2, "Body bubble" );
718 equals( html, 2, "Body bubble" );
719 equals( body, 1, "Body bubble" );
721 jQuery("#main").trigger("click");
722 equals( doc, 3, "Main bubble" );
723 equals( html, 3, "Main bubble" );
724 equals( body, 2, "Main bubble" );
725 equals( main, 1, "Main bubble" );
727 jQuery("#ap").trigger("click");
728 equals( doc, 3, "ap bubble" );
729 equals( html, 3, "ap bubble" );
730 equals( body, 2, "ap bubble" );
731 equals( main, 1, "ap bubble" );
732 equals( ap, 1, "ap bubble" );
735 test("trigger(type, [data], [fn])", function() {
738 var handler = function(event, a, b, c) {
739 equals( event.type, "click", "check passed data" );
740 equals( a, 1, "check passed data" );
741 equals( b, "2", "check passed data" );
742 equals( c, "abc", "check passed data" );
746 var $elem = jQuery("#firstp");
748 // Simulate a "native" click
749 $elem[0].click = function(){
750 ok( true, "Native call was triggered" );
753 // Triggers handlrs and native
755 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
757 // Simulate a "native" click
758 $elem[0].click = function(){
759 ok( false, "Native call was triggered" );
762 // Trigger only the handlers (no native)
764 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
768 jQuery('#form input:first').hide().trigger('focus');
772 ok( pass, "Trigger focus on hidden element" );
776 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
780 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
782 var form = jQuery("<form action=''></form>").appendTo("body");
784 // Make sure it can be prevented locally
785 form.submit(function(){
786 ok( true, "Local bind still works." );
791 form.trigger("submit");
793 form.unbind("submit");
795 jQuery(document).submit(function(){
796 ok( true, "Make sure bubble works up to document." );
801 form.trigger("submit");
803 jQuery(document).unbind("submit");
808 test("jQuery.Event.currentTarget", function(){
811 test("trigger(eventObject, [data], [fn])", function() {
814 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
815 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
817 var event = jQuery.Event("noNew");
818 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
819 equals( event.type, "noNew", "Verify its type" );
821 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
822 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
823 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
825 event.preventDefault();
826 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
827 event.stopPropagation();
828 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
830 event.isPropagationStopped = function(){ return false };
831 event.stopImmediatePropagation();
832 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
833 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
835 $parent.bind('foo',function(e){
837 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
838 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
839 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
840 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
843 // test with an event object
844 event = new jQuery.Event("foo");
845 event.secret = 'boo!';
846 $child.trigger(event);
848 // test with a literal object
849 $child.trigger({type:'foo', secret:'boo!'});
854 ok( false, "This assertion shouldn't be reached");
857 $parent.bind('foo', error );
859 $child.bind('foo',function(e, a, b, c ){
860 equals( arguments.length, 4, "Check arguments length");
861 equals( a, 1, "Check first custom argument");
862 equals( b, 2, "Check second custom argument");
863 equals( c, 3, "Check third custom argument");
865 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
866 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
867 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
870 e.stopImmediatePropagation();
875 // We should add this back in when we want to test the order
876 // in which event handlers are iterated.
877 //$child.bind('foo', error );
879 event = new jQuery.Event("foo");
880 $child.trigger( event, [1,2,3] ).unbind();
881 equals( event.result, "result", "Check event.result attribute");
883 // Will error if it bubbles
884 $child.triggerHandler('foo');
887 $parent.unbind().remove();
890 test("jQuery.Event.currentTarget", function(){
894 $elem = jQuery('<button>a</button>').click(function(e){
895 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
899 $elem.trigger('click');
905 test("toggle(Function, Function, ...)", function() {
909 fn1 = function(e) { count++; },
910 fn2 = function(e) { count--; },
911 preventDefault = function(e) { e.preventDefault() },
912 link = jQuery('#mark');
913 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
914 equals( count, 1, "Check for toggle(fn, fn)" );
916 jQuery("#firstp").toggle(function () {
917 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
918 }, function() {}).trigger("click", [ 1, 2, 3 ]);
921 jQuery("#simon1").one("click", function() {
922 ok( true, "Execute event only once" );
923 jQuery(this).toggle(function() {
924 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
926 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
929 }).click().click().click();
944 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
946 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
948 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
950 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
952 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
954 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
956 $div.unbind('click',fns[0]);
957 var data = jQuery.data( $div[0], 'events' );
958 ok( !data, "Unbinding one function from toggle unbinds them all");
960 // Test Multi-Toggles
962 $div = jQuery("<div/>");
963 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
965 same( a, [1], "Check that a click worked." );
967 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
969 same( a, [1,2], "Check that a click worked with a second toggle." );
970 same( b, [1], "Check that a click worked with a second toggle." );
973 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
974 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
977 test(".live()/.die()", function() {
980 var submit = 0, div = 0, livea = 0, liveb = 0;
982 jQuery("div").live("submit", function(){ submit++; return false; });
983 jQuery("div").live("click", function(){ div++; });
984 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
985 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
987 // Nothing should trigger on the body
988 jQuery("body").trigger("click");
989 equals( submit, 0, "Click on body" );
990 equals( div, 0, "Click on body" );
991 equals( livea, 0, "Click on body" );
992 equals( liveb, 0, "Click on body" );
994 // This should trigger two events
995 submit = 0, div = 0, livea = 0, liveb = 0;
996 jQuery("div#nothiddendiv").trigger("click");
997 equals( submit, 0, "Click on div" );
998 equals( div, 1, "Click on div" );
999 equals( livea, 1, "Click on div" );
1000 equals( liveb, 0, "Click on div" );
1002 // This should trigger three events (w/ bubbling)
1003 submit = 0, div = 0, livea = 0, liveb = 0;
1004 jQuery("div#nothiddendivchild").trigger("click");
1005 equals( submit, 0, "Click on inner div" );
1006 equals( div, 2, "Click on inner div" );
1007 equals( livea, 1, "Click on inner div" );
1008 equals( liveb, 1, "Click on inner div" );
1010 // This should trigger one submit
1011 submit = 0, div = 0, livea = 0, liveb = 0;
1012 jQuery("div#nothiddendivchild").trigger("submit");
1013 equals( submit, 1, "Submit on div" );
1014 equals( div, 0, "Submit on div" );
1015 equals( livea, 0, "Submit on div" );
1016 equals( liveb, 0, "Submit on div" );
1018 // Make sure no other events were removed in the process
1019 submit = 0, div = 0, livea = 0, liveb = 0;
1020 jQuery("div#nothiddendivchild").trigger("click");
1021 equals( submit, 0, "die Click on inner div" );
1022 equals( div, 2, "die Click on inner div" );
1023 equals( livea, 1, "die Click on inner div" );
1024 equals( liveb, 1, "die Click on inner div" );
1026 // Now make sure that the removal works
1027 submit = 0, div = 0, livea = 0, liveb = 0;
1028 jQuery("div#nothiddendivchild").die("click");
1029 jQuery("div#nothiddendivchild").trigger("click");
1030 equals( submit, 0, "die Click on inner div" );
1031 equals( div, 2, "die Click on inner div" );
1032 equals( livea, 1, "die Click on inner div" );
1033 equals( liveb, 0, "die Click on inner div" );
1035 // Make sure that the click wasn't removed too early
1036 submit = 0, div = 0, livea = 0, liveb = 0;
1037 jQuery("div#nothiddendiv").trigger("click");
1038 equals( submit, 0, "die Click on inner div" );
1039 equals( div, 1, "die Click on inner div" );
1040 equals( livea, 1, "die Click on inner div" );
1041 equals( liveb, 0, "die Click on inner div" );
1043 // Make sure that stopPropgation doesn't stop live events
1044 submit = 0, div = 0, livea = 0, liveb = 0;
1045 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1046 jQuery("div#nothiddendivchild").trigger("click");
1047 equals( submit, 0, "stopPropagation Click on inner div" );
1048 equals( div, 1, "stopPropagation Click on inner div" );
1049 equals( livea, 0, "stopPropagation Click on inner div" );
1050 equals( liveb, 1, "stopPropagation Click on inner div" );
1052 // Make sure click events only fire with primary click
1053 submit = 0, div = 0, livea = 0, liveb = 0;
1054 var event = jQuery.Event("click");
1056 jQuery("div#nothiddendiv").trigger(event);
1058 equals( livea, 0, "live secondary click" );
1060 jQuery("div#nothiddendivchild").die("click");
1061 jQuery("div#nothiddendiv").die("click");
1062 jQuery("div").die("click");
1063 jQuery("div").die("submit");
1065 // Test binding with a different context
1066 var clicked = 0, container = jQuery('#main')[0];
1067 jQuery("#foo", container).live("click", function(e){ clicked++; });
1068 jQuery("div").trigger('click');
1069 jQuery("#foo").trigger('click');
1070 jQuery("#main").trigger('click');
1071 jQuery("body").trigger('click');
1072 equals( clicked, 2, "live with a context" );
1074 // Make sure the event is actually stored on the context
1075 ok( jQuery.data(container, "events").live, "live with a context" );
1077 // Test unbinding with a different context
1078 jQuery("#foo", container).die("click");
1079 jQuery("#foo").trigger('click');
1080 equals( clicked, 2, "die with a context");
1082 // Test binding with event data
1083 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1084 jQuery("#foo").trigger("click").die("click");
1086 // Test binding with trigger data
1087 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1088 jQuery("#foo").trigger("click", true).die("click");
1090 // Test binding with different this object
1091 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1092 jQuery("#foo").trigger("click").die("click");
1094 // Test binding with different this object, event data, and trigger data
1095 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1096 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1097 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1098 equals( data, true, "live with with different this object, event data, and trigger data")
1099 }, { foo: "bar" }));
1100 jQuery("#foo").trigger("click", true).die("click");
1102 // Verify that return false prevents default action
1103 jQuery("#anchor2").live("click", function(){ return false; });
1104 var hash = window.location.hash;
1105 jQuery("#anchor2").trigger("click");
1106 equals( window.location.hash, hash, "return false worked" );
1107 jQuery("#anchor2").die("click");
1109 // Verify that .preventDefault() prevents default action
1110 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1111 var hash = window.location.hash;
1112 jQuery("#anchor2").trigger("click");
1113 equals( window.location.hash, hash, "e.preventDefault() worked" );
1114 jQuery("#anchor2").die("click");
1116 // Test binding the same handler to multiple points
1118 function callback(){ called++; return false; }
1120 jQuery("#nothiddendiv").live("click", callback);
1121 jQuery("#anchor2").live("click", callback);
1123 jQuery("#nothiddendiv").trigger("click");
1124 equals( called, 1, "Verify that only one click occurred." );
1127 jQuery("#anchor2").trigger("click");
1128 equals( called, 1, "Verify that only one click occurred." );
1130 // Make sure that only one callback is removed
1131 jQuery("#anchor2").die("click", callback);
1134 jQuery("#nothiddendiv").trigger("click");
1135 equals( called, 1, "Verify that only one click occurred." );
1138 jQuery("#anchor2").trigger("click");
1139 equals( called, 0, "Verify that no click occurred." );
1141 // Make sure that it still works if the selector is the same,
1142 // but the event type is different
1143 jQuery("#nothiddendiv").live("foo", callback);
1146 jQuery("#nothiddendiv").die("click", callback);
1149 jQuery("#nothiddendiv").trigger("click");
1150 equals( called, 0, "Verify that no click occurred." );
1153 jQuery("#nothiddendiv").trigger("foo");
1154 equals( called, 1, "Verify that one foo occurred." );
1157 jQuery("#nothiddendiv").die("foo", callback);
1159 // Make sure we don't loose the target by DOM modifications
1160 // after the bubble already reached the liveHandler
1161 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1163 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1164 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1166 jQuery("#nothiddendiv span").click();
1167 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1168 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1171 jQuery("#nothiddendivchild").die("click");
1173 // Verify that .live() ocurs and cancel buble in the same order as
1174 // we would expect .bind() and .click() without delegation
1175 var lived = 0, livee = 0;
1177 // bind one pair in one order
1178 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1179 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1181 jQuery('span#liveSpan1 a').click();
1182 equals( lived, 1, "Verify that only one first handler occurred." );
1183 equals( livee, 0, "Verify that second handler doesn't." );
1185 // and one pair in inverse
1186 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1187 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1191 jQuery('span#liveSpan2 a').click();
1192 equals( lived, 1, "Verify that only one first handler occurred." );
1193 equals( livee, 0, "Verify that second handler doesn't." );
1196 jQuery("span#liveSpan1 a").die("click")
1197 jQuery("span#liveSpan1").die("click");
1198 jQuery("span#liveSpan2 a").die("click");
1199 jQuery("span#liveSpan2").die("click");
1201 // Test this, target and currentTarget are correct
1202 jQuery('span#liveSpan1').live('click', function(e){
1203 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1204 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1205 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1208 jQuery('span#liveSpan1 a').click();
1210 jQuery('span#liveSpan1').die('click');
1212 // Work with deep selectors
1215 function clickB(){ livee++; }
1217 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1218 jQuery("#nothiddendiv div").live("click", clickB);
1219 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1221 equals( livee, 0, "No clicks, deep selector." );
1224 jQuery("#nothiddendivchild").trigger("click");
1225 equals( livee, 2, "Click, deep selector." );
1228 jQuery("#nothiddendivchild").trigger("mouseover");
1229 equals( livee, 1, "Mouseover, deep selector." );
1231 jQuery("#nothiddendiv div").die("mouseover");
1234 jQuery("#nothiddendivchild").trigger("click");
1235 equals( livee, 2, "Click, deep selector." );
1238 jQuery("#nothiddendivchild").trigger("mouseover");
1239 equals( livee, 0, "Mouseover, deep selector." );
1241 jQuery("#nothiddendiv div").die("click", clickB);
1244 jQuery("#nothiddendivchild").trigger("click");
1245 equals( livee, 1, "Click, deep selector." );
1247 jQuery("#nothiddendiv div").die("click");
1249 jQuery("#nothiddendiv div").live("blur", function(){
1250 ok( true, "Live div trigger blur." );
1253 jQuery("#nothiddendiv div").trigger("blur");
1255 jQuery("#nothiddendiv div").die("blur");
1258 test("die all bound events", function(){
1262 var div = jQuery("div#nothiddendivchild");
1264 div.live("click submit", function(){ count++; });
1267 div.trigger("click");
1268 div.trigger("submit");
1270 equals( count, 0, "Make sure no events were triggered." );
1273 test("live with multiple events", function(){
1277 var div = jQuery("div#nothiddendivchild");
1279 div.live("click submit", function(){ count++; });
1281 div.trigger("click");
1282 div.trigger("submit");
1284 equals( count, 2, "Make sure both the click and submit were triggered." );
1287 test("live with namespaces", function(){
1290 var count1 = 0, count2 = 0;
1292 jQuery("#liveSpan1").live("foo.bar", function(e){
1296 jQuery("#liveSpan1").live("foo.zed", function(e){
1300 jQuery("#liveSpan1").trigger("foo.bar");
1301 equals( count1, 1, "Got live foo.bar" );
1302 equals( count2, 0, "Got live foo.bar" );
1304 count1 = 0, count2 = 0;
1306 jQuery("#liveSpan1").trigger("foo.zed");
1307 equals( count1, 0, "Got live foo.zed" );
1308 equals( count2, 1, "Got live foo.zed" );
1311 count1 = 0, count2 = 0;
1313 jQuery("#liveSpan1").die("foo.zed");
1314 jQuery("#liveSpan1").trigger("foo.bar");
1316 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1317 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1319 count1 = 0, count2 = 0;
1321 jQuery("#liveSpan1").trigger("foo.zed");
1322 equals( count1, 0, "Got live foo.zed" );
1323 equals( count2, 0, "Got live foo.zed" );
1326 jQuery("#liveSpan1").die("foo.bar");
1328 count1 = 0, count2 = 0;
1330 jQuery("#liveSpan1").trigger("foo.bar");
1331 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1332 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1334 jQuery("#liveSpan1").trigger("foo.zed");
1335 equals( count1, 0, "Did not trigger foo.zed again" );
1336 equals( count2, 0, "Did not trigger foo.zed again" );
1339 test("live with change", function(){
1342 var selectChange = 0, checkboxChange = 0;
1344 var select = jQuery("select[name='S1']")
1345 select.live("change", function() {
1349 var checkbox = jQuery("#check2"),
1350 checkboxFunction = function(){
1353 checkbox.live("change", checkboxFunction);
1355 // test click on select
1357 // second click that changed it
1359 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1360 select.trigger("change");
1361 equals( selectChange, 1, "Change on click." );
1363 // test keys on select
1365 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1366 select.trigger("change");
1367 equals( selectChange, 1, "Change on keyup." );
1369 // test click on checkbox
1370 checkbox.trigger("change");
1371 equals( checkboxChange, 1, "Change on checkbox." );
1373 // test blur/focus on text
1374 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1375 text.live("change", function() {
1379 text.val(oldTextVal+"foo");
1380 text.trigger("change");
1381 equals( textChange, 1, "Change on text input." );
1383 text.val(oldTextVal);
1386 // test blur/focus on password
1387 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1388 password.live("change", function() {
1392 password.val(oldPasswordVal + "foo");
1393 password.trigger("change");
1394 equals( passwordChange, 1, "Change on password input." );
1396 password.val(oldPasswordVal);
1397 password.die("change");
1399 // make sure die works
1403 select.die("change");
1404 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1405 select.trigger("change");
1406 equals( selectChange, 0, "Die on click works." );
1409 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1410 select.trigger("change");
1411 equals( selectChange, 0, "Die on keyup works." );
1413 // die specific checkbox
1414 checkbox.die("change", checkboxFunction);
1415 checkbox.trigger("change");
1416 equals( checkboxChange, 1, "Die on checkbox." );
1419 test("live with submit", function() {
1420 var count1 = 0, count2 = 0;
1422 jQuery("#testForm").live("submit", function(ev) {
1424 ev.preventDefault();
1427 jQuery("body").live("submit", function(ev) {
1429 ev.preventDefault();
1432 jQuery("#testForm input[name=sub1]").submit();
1433 equals( count1, 1, "Verify form submit." );
1434 equals( count2, 1, "Verify body submit." );
1436 jQuery("#testForm").die("submit");
1437 jQuery("body").die("submit");
1440 test("live with special events", function() {
1443 jQuery.event.special.foo = {
1444 setup: function( data, namespaces, handler ) {
1445 ok( true, "Setup run." );
1447 teardown: function( namespaces ) {
1448 ok( true, "Teardown run." );
1450 add: function( handleObj ) {
1451 ok( true, "Add run." );
1453 remove: function( handleObj ) {
1454 ok( true, "Remove run." );
1456 _default: function( event ) {
1457 ok( true, "Default run." );
1462 jQuery("#liveSpan1").live("foo.a", function(e){
1463 ok( true, "Handler 1 run." );
1467 jQuery("#liveSpan1").live("foo.b", function(e){
1468 ok( true, "Handler 2 run." );
1471 // Run: Handler 1, Handler 2, Default
1472 jQuery("#liveSpan1").trigger("foo");
1474 // Run: Handler 1, Default
1475 // TODO: Namespace doesn't trigger default (?)
1476 jQuery("#liveSpan1").trigger("foo.a");
1479 jQuery("#liveSpan1").die("foo.a");
1481 // Run: Handler 2, Default
1482 jQuery("#liveSpan1").trigger("foo");
1484 // Run: remove, teardown
1485 jQuery("#liveSpan1").die("foo");
1487 delete jQuery.event.special.foo;
1490 test(".delegate()/.undelegate()", function() {
1493 var submit = 0, div = 0, livea = 0, liveb = 0;
1495 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1496 jQuery("#body").delegate("div", "click", function(){ div++; });
1497 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1498 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1500 // Nothing should trigger on the body
1501 jQuery("body").trigger("click");
1502 equals( submit, 0, "Click on body" );
1503 equals( div, 0, "Click on body" );
1504 equals( livea, 0, "Click on body" );
1505 equals( liveb, 0, "Click on body" );
1507 // This should trigger two events
1508 submit = 0, div = 0, livea = 0, liveb = 0;
1509 jQuery("div#nothiddendiv").trigger("click");
1510 equals( submit, 0, "Click on div" );
1511 equals( div, 1, "Click on div" );
1512 equals( livea, 1, "Click on div" );
1513 equals( liveb, 0, "Click on div" );
1515 // This should trigger three events (w/ bubbling)
1516 submit = 0, div = 0, livea = 0, liveb = 0;
1517 jQuery("div#nothiddendivchild").trigger("click");
1518 equals( submit, 0, "Click on inner div" );
1519 equals( div, 2, "Click on inner div" );
1520 equals( livea, 1, "Click on inner div" );
1521 equals( liveb, 1, "Click on inner div" );
1523 // This should trigger one submit
1524 submit = 0, div = 0, livea = 0, liveb = 0;
1525 jQuery("div#nothiddendivchild").trigger("submit");
1526 equals( submit, 1, "Submit on div" );
1527 equals( div, 0, "Submit on div" );
1528 equals( livea, 0, "Submit on div" );
1529 equals( liveb, 0, "Submit on div" );
1531 // Make sure no other events were removed in the process
1532 submit = 0, div = 0, livea = 0, liveb = 0;
1533 jQuery("div#nothiddendivchild").trigger("click");
1534 equals( submit, 0, "undelegate Click on inner div" );
1535 equals( div, 2, "undelegate Click on inner div" );
1536 equals( livea, 1, "undelegate Click on inner div" );
1537 equals( liveb, 1, "undelegate Click on inner div" );
1539 // Now make sure that the removal works
1540 submit = 0, div = 0, livea = 0, liveb = 0;
1541 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1542 jQuery("div#nothiddendivchild").trigger("click");
1543 equals( submit, 0, "undelegate Click on inner div" );
1544 equals( div, 2, "undelegate Click on inner div" );
1545 equals( livea, 1, "undelegate Click on inner div" );
1546 equals( liveb, 0, "undelegate Click on inner div" );
1548 // Make sure that the click wasn't removed too early
1549 submit = 0, div = 0, livea = 0, liveb = 0;
1550 jQuery("div#nothiddendiv").trigger("click");
1551 equals( submit, 0, "undelegate Click on inner div" );
1552 equals( div, 1, "undelegate Click on inner div" );
1553 equals( livea, 1, "undelegate Click on inner div" );
1554 equals( liveb, 0, "undelegate Click on inner div" );
1556 // Make sure that stopPropgation doesn't stop live events
1557 submit = 0, div = 0, livea = 0, liveb = 0;
1558 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1559 jQuery("div#nothiddendivchild").trigger("click");
1560 equals( submit, 0, "stopPropagation Click on inner div" );
1561 equals( div, 1, "stopPropagation Click on inner div" );
1562 equals( livea, 0, "stopPropagation Click on inner div" );
1563 equals( liveb, 1, "stopPropagation Click on inner div" );
1565 // Make sure click events only fire with primary click
1566 submit = 0, div = 0, livea = 0, liveb = 0;
1567 var event = jQuery.Event("click");
1569 jQuery("div#nothiddendiv").trigger(event);
1571 equals( livea, 0, "delegate secondary click" );
1573 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1574 jQuery("#body").undelegate("div#nothiddendiv", "click");
1575 jQuery("#body").undelegate("div", "click");
1576 jQuery("#body").undelegate("div", "submit");
1578 // Test binding with a different context
1579 var clicked = 0, container = jQuery('#main')[0];
1580 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1581 jQuery("div").trigger('click');
1582 jQuery("#foo").trigger('click');
1583 jQuery("#main").trigger('click');
1584 jQuery("body").trigger('click');
1585 equals( clicked, 2, "delegate with a context" );
1587 // Make sure the event is actually stored on the context
1588 ok( jQuery.data(container, "events").live, "delegate with a context" );
1590 // Test unbinding with a different context
1591 jQuery("#main").undelegate("#foo", "click");
1592 jQuery("#foo").trigger('click');
1593 equals( clicked, 2, "undelegate with a context");
1595 // Test binding with event data
1596 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1597 jQuery("#foo").trigger("click");
1598 jQuery("#body").undelegate("#foo", "click");
1600 // Test binding with trigger data
1601 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1602 jQuery("#foo").trigger("click", true);
1603 jQuery("#body").undelegate("#foo", "click");
1605 // Test binding with different this object
1606 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1607 jQuery("#foo").trigger("click");
1608 jQuery("#body").undelegate("#foo", "click");
1610 // Test binding with different this object, event data, and trigger data
1611 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1612 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1613 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1614 equals( data, true, "delegate with with different this object, event data, and trigger data")
1615 }, { foo: "bar" }));
1616 jQuery("#foo").trigger("click", true);
1617 jQuery("#body").undelegate("#foo", "click");
1619 // Verify that return false prevents default action
1620 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1621 var hash = window.location.hash;
1622 jQuery("#anchor2").trigger("click");
1623 equals( window.location.hash, hash, "return false worked" );
1624 jQuery("#body").undelegate("#anchor2", "click");
1626 // Verify that .preventDefault() prevents default action
1627 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1628 var hash = window.location.hash;
1629 jQuery("#anchor2").trigger("click");
1630 equals( window.location.hash, hash, "e.preventDefault() worked" );
1631 jQuery("#body").undelegate("#anchor2", "click");
1633 // Test binding the same handler to multiple points
1635 function callback(){ called++; return false; }
1637 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1638 jQuery("#body").delegate("#anchor2", "click", callback);
1640 jQuery("#nothiddendiv").trigger("click");
1641 equals( called, 1, "Verify that only one click occurred." );
1644 jQuery("#anchor2").trigger("click");
1645 equals( called, 1, "Verify that only one click occurred." );
1647 // Make sure that only one callback is removed
1648 jQuery("#body").undelegate("#anchor2", "click", callback);
1651 jQuery("#nothiddendiv").trigger("click");
1652 equals( called, 1, "Verify that only one click occurred." );
1655 jQuery("#anchor2").trigger("click");
1656 equals( called, 0, "Verify that no click occurred." );
1658 // Make sure that it still works if the selector is the same,
1659 // but the event type is different
1660 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1663 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1666 jQuery("#nothiddendiv").trigger("click");
1667 equals( called, 0, "Verify that no click occurred." );
1670 jQuery("#nothiddendiv").trigger("foo");
1671 equals( called, 1, "Verify that one foo occurred." );
1674 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1676 // Make sure we don't loose the target by DOM modifications
1677 // after the bubble already reached the liveHandler
1678 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1680 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1681 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1683 jQuery("#nothiddendiv span").click();
1684 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1685 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1688 jQuery("#body").undelegate("#nothiddendivchild", "click");
1690 // Verify that .live() ocurs and cancel buble in the same order as
1691 // we would expect .bind() and .click() without delegation
1692 var lived = 0, livee = 0;
1694 // bind one pair in one order
1695 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1696 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1698 jQuery('span#liveSpan1 a').click();
1699 equals( lived, 1, "Verify that only one first handler occurred." );
1700 equals( livee, 0, "Verify that second handler doesn't." );
1702 // and one pair in inverse
1703 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1704 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1708 jQuery('span#liveSpan2 a').click();
1709 equals( lived, 1, "Verify that only one first handler occurred." );
1710 equals( livee, 0, "Verify that second handler doesn't." );
1713 jQuery("#body").undelegate("click");
1715 // Test this, target and currentTarget are correct
1716 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1717 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1718 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1719 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1722 jQuery('span#liveSpan1 a').click();
1724 jQuery("#body").undelegate('span#liveSpan1', 'click');
1726 // Work with deep selectors
1729 function clickB(){ livee++; }
1731 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1732 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1733 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1735 equals( livee, 0, "No clicks, deep selector." );
1738 jQuery("#nothiddendivchild").trigger("click");
1739 equals( livee, 2, "Click, deep selector." );
1742 jQuery("#nothiddendivchild").trigger("mouseover");
1743 equals( livee, 1, "Mouseover, deep selector." );
1745 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1748 jQuery("#nothiddendivchild").trigger("click");
1749 equals( livee, 2, "Click, deep selector." );
1752 jQuery("#nothiddendivchild").trigger("mouseover");
1753 equals( livee, 0, "Mouseover, deep selector." );
1755 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1758 jQuery("#nothiddendivchild").trigger("click");
1759 equals( livee, 1, "Click, deep selector." );
1761 jQuery("#body").undelegate("#nothiddendiv div", "click");
1764 test("undelegate all bound events", function(){
1768 var div = jQuery("#body");
1770 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1773 jQuery("div#nothiddendivchild").trigger("click");
1774 jQuery("div#nothiddendivchild").trigger("submit");
1776 equals( count, 0, "Make sure no events were triggered." );
1779 test("delegate with multiple events", function(){
1783 var div = jQuery("#body");
1785 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1787 jQuery("div#nothiddendivchild").trigger("click");
1788 jQuery("div#nothiddendivchild").trigger("submit");
1790 equals( count, 2, "Make sure both the click and submit were triggered." );
1792 jQuery("#body").undelegate();
1795 test("delegate with change", function(){
1798 var selectChange = 0, checkboxChange = 0;
1800 var select = jQuery("select[name='S1']");
1801 jQuery("#body").delegate("select[name='S1']", "change", function() {
1805 var checkbox = jQuery("#check2"),
1806 checkboxFunction = function(){
1809 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1811 // test click on select
1813 // second click that changed it
1815 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1816 select.trigger("change");
1817 equals( selectChange, 1, "Change on click." );
1819 // test keys on select
1821 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1822 select.trigger("change");
1823 equals( selectChange, 1, "Change on keyup." );
1825 // test click on checkbox
1826 checkbox.trigger("change");
1827 equals( checkboxChange, 1, "Change on checkbox." );
1829 // test blur/focus on text
1830 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1831 jQuery("#body").delegate("#name", "change", function() {
1835 text.val(oldTextVal+"foo");
1836 text.trigger("change");
1837 equals( textChange, 1, "Change on text input." );
1839 text.val(oldTextVal);
1840 jQuery("#body").die("change");
1842 // test blur/focus on password
1843 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1844 jQuery("#body").delegate("#name", "change", function() {
1848 password.val(oldPasswordVal + "foo");
1849 password.trigger("change");
1850 equals( passwordChange, 1, "Change on password input." );
1852 password.val(oldPasswordVal);
1853 jQuery("#body").undelegate("#name", "change");
1855 // make sure die works
1859 jQuery("#body").undelegate("select[name='S1']", "change");
1860 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1861 select.trigger("change");
1862 equals( selectChange, 0, "Die on click works." );
1865 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1866 select.trigger("change");
1867 equals( selectChange, 0, "Die on keyup works." );
1869 // die specific checkbox
1870 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1871 checkbox.trigger("change");
1872 equals( checkboxChange, 1, "Die on checkbox." );
1875 test("delegate with submit", function() {
1876 var count1 = 0, count2 = 0;
1878 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1880 ev.preventDefault();
1883 jQuery(document).delegate("body", "submit", function(ev) {
1885 ev.preventDefault();
1888 jQuery("#testForm input[name=sub1]").submit();
1889 equals( count1, 1, "Verify form submit." );
1890 equals( count2, 1, "Verify body submit." );
1892 jQuery("#body").undelegate();
1893 jQuery(document).undelegate();
1896 test("Non DOM element events", function() {
1901 jQuery(o).bind('nonelementobj', function(e) {
1902 ok( true, "Event on non-DOM object triggered" );
1905 jQuery(o).trigger('nonelementobj');
1908 test("window resize", function() {
1911 jQuery(window).unbind();
1913 jQuery(window).bind("resize", function(){
1914 ok( true, "Resize event fired." );
1915 }).resize().unbind("resize");
1917 ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1920 test("focusin bubbles", function() {
1921 //create an input and focusin on it
1922 var input = jQuery("<input/>"), order = 0;
1924 input.prependTo("body");
1926 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1927 equals(1,order++,"focusin on the body second")
1930 input.bind("focusin.focusinBubblesTest",function(){
1931 equals(0,order++,"focusin on the element first")
1937 jQuery("body").unbind("focusin.focusinBubblesTest");
1941 test("jQuery(function($) {})", function() {
1943 jQuery(function($) {
1944 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");
1949 test("event properties", function() {
1951 jQuery("#simon1").click(function(event) {
1952 ok( event.timeStamp, "assert event.timeStamp is present" );