1 module("event", { teardown: moduleTeardown });
3 test("null or undefined handler", function() {
5 // Supports Fixes bug #7229
7 jQuery("#firstp").click(null);
8 ok(true, "Passing a null handler will not throw an exception");
12 jQuery("#firstp").click(undefined);
13 ok(true, "Passing an undefined handler will not throw an exception");
17 test("bind(), with data", function() {
19 var handler = function(event) {
20 ok( event.data, "bind() with data, check passed data exists" );
21 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
23 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
25 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
28 test("click(), with data", function() {
30 var handler = function(event) {
31 ok( event.data, "bind() with data, check passed data exists" );
32 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
34 jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
36 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
39 test("bind(), with data, trigger with data", function() {
41 var handler = function(event, data) {
42 ok( event.data, "check passed data exists" );
43 equals( event.data.foo, "bar", "Check value of passed data" );
44 ok( data, "Check trigger data" );
45 equals( data.bar, "foo", "Check value of trigger data" );
47 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
50 test("bind(), multiple events at once", function() {
54 var handler = function(event) {
55 if (event.type == "click")
57 else if (event.type == "mouseover")
58 mouseoverCounter += 1;
60 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
61 equals( clickCounter, 1, "bind() with multiple events at once" );
62 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
65 test("bind(), multiple events at once and namespaces", function() {
70 var div = jQuery("<div/>").bind("focusin.a", function(e) {
71 equals( e.type, cur, "Verify right single event was fired." );
75 div.trigger("focusin.a");
77 // manually clean up detached elements
80 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
81 equals( e.type, cur, "Verify right multi event was fired." );
82 equals( e.data, obj, "Make sure the data came in correctly." );
89 div.trigger("mouseover");
91 // manually clean up detached elements
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");
104 // manually clean up detached elements
108 test("bind(), namespace with special add", function() {
111 var div = jQuery("<div/>").bind("test", function(e) {
112 ok( true, "Test event fired." );
117 jQuery.event.special.test = {
118 _default: function(e) {
119 equals( this, document, "Make sure we're at the top of the chain." );
120 equals( e.type, "test", "And that we're still dealing with a test event." );
121 equals( e.target, div[0], "And that the target is correct." );
124 teardown: function(){
125 ok(true, "Teardown called.");
127 add: function( handleObj ) {
128 var handler = handleObj.handler;
129 handleObj.handler = function(e) {
131 handler.apply( this, arguments );
135 ok(true, "Remove called.");
139 div.bind("test.a", {x: 1}, function(e) {
140 ok( !!e.xyz, "Make sure that the data is getting passed through." );
141 equals( e.data.x, 1, "Make sure data is attached properly." );
144 div.bind("test.b", {x: 2}, function(e) {
145 ok( !!e.xyz, "Make sure that the data is getting passed through." );
146 equals( e.data.x, 2, "Make sure data is attached properly." );
153 div.trigger("test.a");
156 div.trigger("test.b");
161 div = jQuery("<div/>").bind("test", function(e) {
162 ok( true, "Test event fired." );
166 div.appendTo("#main").remove();
168 delete jQuery.event.special.test;
171 test("bind(), no data", function() {
173 var handler = function(event) {
174 ok ( !event.data, "Check that no data is added to the event object" );
176 jQuery("#firstp").bind("click", handler).trigger("click");
179 test("bind/one/unbind(Object)", function(){
182 var clickCounter = 0, mouseoverCounter = 0;
183 function handler(event) {
184 if (event.type == "click")
186 else if (event.type == "mouseover")
190 function handlerWithData(event) {
191 if (event.type == "click")
192 clickCounter += event.data;
193 else if (event.type == "mouseover")
194 mouseoverCounter += event.data;
198 $elem.trigger("click").trigger("mouseover");
201 var $elem = jQuery("#firstp")
209 click:handlerWithData,
210 mouseover:handlerWithData
215 equals( clickCounter, 3, "bind(Object)" );
216 equals( mouseoverCounter, 3, "bind(Object)" );
219 equals( clickCounter, 4, "bind(Object)" );
220 equals( mouseoverCounter, 4, "bind(Object)" );
222 jQuery("#firstp").unbind({
228 equals( clickCounter, 4, "bind(Object)" );
229 equals( mouseoverCounter, 4, "bind(Object)" );
232 test("live/die(Object), delegate/undelegate(String, Object)", function() {
235 var clickCounter = 0, mouseoverCounter = 0,
236 $p = jQuery("#firstp"), $a = $p.find("a:first");
239 click: function( event ) {
240 clickCounter += ( event.data || 1 );
242 mouseover: function( event ) {
243 mouseoverCounter += ( event.data || 1 );
248 $a.trigger("click").trigger("mouseover");
252 $p.delegate( "a", events, 2 );
255 equals( clickCounter, 3, "live/delegate" );
256 equals( mouseoverCounter, 3, "live/delegate" );
258 $p.undelegate( "a", events );
261 equals( clickCounter, 4, "undelegate" );
262 equals( mouseoverCounter, 4, "undelegate" );
267 equals( clickCounter, 4, "die" );
268 equals( mouseoverCounter, 4, "die" );
271 test("live/delegate immediate propagation", function() {
274 var $p = jQuery("#firstp"), $a = $p.find("a:first"), lastClick;
277 $a.live( "click", function(e) {
278 lastClick = "click1";
279 e.stopImmediatePropagation();
281 $a.live( "click", function(e) {
282 lastClick = "click2";
284 $a.trigger( "click" );
285 equals( lastClick, "click1", "live stopImmediatePropagation" );
289 $p.delegate( "a", "click", function(e) {
290 lastClick = "click1";
291 e.stopImmediatePropagation();
293 $p.delegate( "a", "click", function(e) {
294 lastClick = "click2";
296 $a.trigger( "click" );
297 equals( lastClick, "click1", "delegate stopImmediatePropagation" );
298 $p.undelegate( "click" );
301 test("bind/delegate bubbling, isDefaultPrevented", function() {
303 var $anchor2 = jQuery( "#anchor2" ),
304 $main = jQuery( "#main" ),
305 fakeClick = function($jq) {
306 // Use a native click so we don't get jQuery simulated bubbling
307 if ( document.createEvent ) {
308 var e = document.createEvent( 'MouseEvents' );
309 e.initEvent( "click", true, true );
310 $jq[0].dispatchEvent(e);
312 else if ( $jq[0].click ) {
313 $jq[0].click(); // IE
316 $anchor2.click(function(e) {
319 $main.delegate("#foo", "click", function(e) {
320 var orig = e.originalEvent;
322 if ( typeof(orig.defaultPrevented) === "boolean" || typeof(orig.returnValue) === "boolean" || orig.getPreventDefault ) {
323 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
326 // Opera < 11 doesn't implement any interface we can use, so give it a pass
327 ok( true, "isDefaultPrevented not supported by this browser, test skipped" );
330 fakeClick( $anchor2 );
331 $anchor2.unbind( "click" );
332 $main.undelegate( "click" );
333 $anchor2.click(function(e) {
334 // Let the default action occur
336 $main.delegate("#foo", "click", function(e) {
337 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
339 fakeClick( $anchor2 );
340 $anchor2.unbind( "click" );
341 $main.undelegate( "click" );
344 test("bind(), iframes", function() {
345 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
346 var doc = jQuery("#loadediframe").contents();
348 jQuery("div", doc).bind("click", function() {
349 ok( true, "Binding to element inside iframe" );
350 }).click().unbind('click');
353 test("bind(), trigger change on select", function() {
356 function selectOnChange(event) {
357 equals( event.data, counter++, "Event.data is not a global event object" );
359 jQuery("#form select").each(function(i){
360 jQuery(this).bind('change', i, selectOnChange);
361 }).trigger('change');
364 test("bind(), namespaced events, cloned events", 18, function() {
365 var firstp = jQuery( "#firstp" );
367 firstp.bind("custom.test",function(e){
368 ok(false, "Custom event triggered");
371 firstp.bind("click",function(e){
372 ok(true, "Normal click triggered");
373 equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" );
376 firstp.bind("click.test",function(e){
378 ok( true, "Namespaced click triggered" );
382 equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" );
385 //clone(true) element to verify events are cloned correctly
386 firstp = firstp.add( firstp.clone( true ).attr( "id", "firstp2" ).insertBefore( firstp ) );
388 // Trigger both bound fn (8)
389 firstp.trigger("click");
391 // Trigger one bound fn (4)
392 firstp.trigger("click.test");
394 // Remove only the one fn
395 firstp.unbind("click.test");
397 // Trigger the remaining fn (4)
398 firstp.trigger("click");
400 // Remove the remaining namespaced fn
401 firstp.unbind(".test");
403 // Try triggering the custom event (0)
404 firstp.trigger("custom");
406 // using contents will get comments regular, text, and comment nodes
407 jQuery("#nonnodes").contents().bind("tester", function () {
408 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
409 }).trigger("tester");
411 // Make sure events stick with appendTo'd elements (which are cloned) #2027
412 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("#main");
413 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
416 test("bind(), multi-namespaced events", function() {
428 function check(name, msg){
429 same(name, order.shift(), msg);
432 jQuery("#firstp").bind("custom.test",function(e){
433 check("custom.test", "Custom event triggered");
436 jQuery("#firstp").bind("custom.test2",function(e){
437 check("custom.test2", "Custom event triggered");
440 jQuery("#firstp").bind("click.test",function(e){
441 check("click.test", "Normal click triggered");
444 jQuery("#firstp").bind("click.test.abc",function(e){
445 check("click.test.abc", "Namespaced click triggered");
448 // Those would not trigger/unbind (#5303)
449 jQuery("#firstp").trigger("click.a.test");
450 jQuery("#firstp").unbind("click.a.test");
452 // Trigger both bound fn (1)
453 jQuery("#firstp").trigger("click.test.abc");
455 // Trigger one bound fn (1)
456 jQuery("#firstp").trigger("click.abc");
458 // Trigger two bound fn (2)
459 jQuery("#firstp").trigger("click.test");
461 // Remove only the one fn
462 jQuery("#firstp").unbind("click.abc");
464 // Trigger the remaining fn (1)
465 jQuery("#firstp").trigger("click");
467 // Remove the remaining fn
468 jQuery("#firstp").unbind(".test");
470 // Trigger the remaining fn (1)
471 jQuery("#firstp").trigger("custom");
474 test("bind(), with same function", function() {
477 var count = 0, func = function(){
481 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
482 jQuery("#liveHandlerOrder").trigger("foo.bar");
484 equals(count, 1, "Verify binding function with multiple namespaces." );
486 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
487 jQuery("#liveHandlerOrder").trigger("foo.bar");
489 equals(count, 1, "Verify that removing events still work." );
492 test("bind(), make sure order is maintained", function() {
495 var elem = jQuery("#firstp"), log = [], check = [];
497 for ( var i = 0; i < 100; i++ ) (function(i){
498 elem.bind( "click", function(){
505 elem.trigger("click");
507 equals( log.join(","), check.join(","), "Make sure order was maintained." );
509 elem.unbind("click");
512 test("bind(), with different this object", function() {
514 var thisObject = { myThis: true },
515 data = { myData: true },
516 handler1 = function( event ) {
517 equals( this, thisObject, "bind() with different this object" );
519 handler2 = function( event ) {
520 equals( this, thisObject, "bind() with different this object and data" );
521 equals( event.data, data, "bind() with different this object and data" );
525 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
526 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
528 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
531 test("bind(name, false), unbind(name, false)", function() {
535 jQuery("#main").bind("click", function(e){ main++; });
536 jQuery("#ap").trigger("click");
537 equals( main, 1, "Verify that the trigger happened correctly." );
540 jQuery("#ap").bind("click", false);
541 jQuery("#ap").trigger("click");
542 equals( main, 0, "Verify that no bubble happened." );
545 jQuery("#ap").unbind("click", false);
546 jQuery("#ap").trigger("click");
547 equals( main, 1, "Verify that the trigger happened correctly." );
549 // manually clean up events from elements outside the fixture
550 jQuery("#main").unbind("click");
553 test("bind()/trigger()/unbind() on plain object", function() {
558 // Make sure it doesn't complain when no events are found
559 jQuery(obj).trigger("test");
561 // Make sure it doesn't complain when no events are found
562 jQuery(obj).unbind("test");
566 ok( true, "Custom event run." );
569 ok( true, "Custom submit event run." );
573 var events = jQuery._data(obj, "events");
574 ok( events, "Object has events bound." );
575 equals( obj.events, undefined, "Events object on plain objects is not events" );
576 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
577 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
580 jQuery(obj).trigger("test");
581 jQuery(obj).trigger("submit");
583 jQuery(obj).unbind("test");
584 jQuery(obj).unbind("submit");
587 jQuery(obj).trigger("test");
589 // Make sure it doesn't complain when no events are found
590 jQuery(obj).unbind("test");
592 equals( obj && obj[ jQuery.expando ] &&
593 obj[ jQuery.expando ][ jQuery.expando ] &&
594 obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
597 test("unbind(type)", function() {
600 var $elem = jQuery("#firstp"),
604 ok( false, message );
607 message = "unbind passing function";
608 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
610 message = "unbind all from event";
611 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
613 message = "unbind all";
614 $elem.bind('error1', error).unbind().triggerHandler('error1');
616 message = "unbind many with function";
617 $elem.bind('error1 error2',error)
618 .unbind('error1 error2', error )
619 .trigger('error1').triggerHandler('error2');
621 message = "unbind many"; // #3538
622 $elem.bind('error1 error2',error)
623 .unbind('error1 error2')
624 .trigger('error1').triggerHandler('error2');
626 message = "unbind without a type or handler";
627 $elem.bind("error1 error2.test",error)
629 .trigger("error1").triggerHandler("error2");
632 test("unbind(eventObject)", function() {
635 var $elem = jQuery("#firstp"),
638 function assert( expected ){
640 $elem.trigger('foo').triggerHandler('bar');
641 equals( num, expected, "Check the right handlers are triggered" );
645 // This handler shouldn't be unbound
646 .bind('foo', function(){
649 .bind('foo', function(e){
654 .bind('bar', function(){
668 test("hover()", function() {
670 handler1 = function( event ) { ++times; },
671 handler2 = function( event ) { ++times; };
674 .hover(handler1, handler2)
675 .mouseenter().mouseleave()
676 .unbind("mouseenter", handler1)
677 .unbind("mouseleave", handler2)
679 .mouseenter().mouseleave()
680 .unbind("mouseenter mouseleave", handler1)
681 .mouseenter().mouseleave();
683 equals( times, 4, "hover handlers fired" );
686 test("mouseover triggers mouseenter", function() {
690 elem = jQuery("<a />");
691 elem.mouseenter(function () {
694 elem.trigger('mouseover');
695 equals(count, 1, "make sure mouseover triggers a mouseenter" );
700 test("trigger() shortcuts", function() {
703 var elem = jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL');
704 elem.find('a').bind('click', function() {
705 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
706 equals( close.length, 0, "Context element does not exist, length must be zero" );
707 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
711 // manually clean up detached elements
714 jQuery("#check1").click(function() {
715 ok( true, "click event handler for checkbox gets fired twice, see #815" );
719 jQuery('#firstp')[0].onclick = function(event) {
722 jQuery('#firstp').click();
723 equals( counter, 1, "Check that click, triggers onclick event handler also" );
725 var clickCounter = 0;
726 jQuery('#simon1')[0].onclick = function(event) {
729 jQuery('#simon1').click();
730 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
732 elem = jQuery('<img />').load(function(){
733 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
736 // manually clean up detached elements
740 test("trigger() bubbling", function() {
743 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
745 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
746 jQuery("html").bind("click", function(e){ html++; });
747 jQuery("body").bind("click", function(e){ body++; });
748 jQuery("#main").bind("click", function(e){ main++; });
749 jQuery("#ap").bind("click", function(){ ap++; return false; });
751 jQuery("html").trigger("click");
752 equals( doc, 1, "HTML bubble" );
753 equals( html, 1, "HTML bubble" );
755 jQuery("body").trigger("click");
756 equals( doc, 2, "Body bubble" );
757 equals( html, 2, "Body bubble" );
758 equals( body, 1, "Body bubble" );
760 jQuery("#main").trigger("click");
761 equals( doc, 3, "Main bubble" );
762 equals( html, 3, "Main bubble" );
763 equals( body, 2, "Main bubble" );
764 equals( main, 1, "Main bubble" );
766 jQuery("#ap").trigger("click");
767 equals( doc, 3, "ap bubble" );
768 equals( html, 3, "ap bubble" );
769 equals( body, 2, "ap bubble" );
770 equals( main, 1, "ap bubble" );
771 equals( ap, 1, "ap bubble" );
773 // manually clean up events from elements outside the fixture
774 jQuery(document).unbind("click");
775 jQuery("html, body, #main").unbind("click");
778 test("trigger(type, [data], [fn])", function() {
781 var handler = function(event, a, b, c) {
782 equals( event.type, "click", "check passed data" );
783 equals( a, 1, "check passed data" );
784 equals( b, "2", "check passed data" );
785 equals( c, "abc", "check passed data" );
789 var $elem = jQuery("#firstp");
791 // Simulate a "native" click
792 $elem[0].click = function(){
793 ok( true, "Native call was triggered" );
796 // Triggers handlrs and native
798 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
800 // Simulate a "native" click
801 $elem[0].click = function(){
802 ok( false, "Native call was triggered" );
805 // Trigger only the handlers (no native)
807 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
811 jQuery('#form input:first').hide().trigger('focus');
815 ok( pass, "Trigger focus on hidden element" );
819 jQuery('#main table:first').bind('test:test', function(){}).trigger('test:test');
823 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
825 var form = jQuery("<form action=''></form>").appendTo("body");
827 // Make sure it can be prevented locally
828 form.submit(function(){
829 ok( true, "Local bind still works." );
834 form.trigger("submit");
836 form.unbind("submit");
838 jQuery(document).submit(function(){
839 ok( true, "Make sure bubble works up to document." );
844 form.trigger("submit");
846 jQuery(document).unbind("submit");
851 test("jQuery.Event.currentTarget", function(){
854 test("trigger(eventObject, [data], [fn])", function() {
857 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
858 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
860 var event = jQuery.Event("noNew");
861 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
862 equals( event.type, "noNew", "Verify its type" );
864 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
865 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
866 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
868 event.preventDefault();
869 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
870 event.stopPropagation();
871 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
873 event.isPropagationStopped = function(){ return false };
874 event.stopImmediatePropagation();
875 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
876 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
878 $parent.bind('foo',function(e){
880 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
881 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
882 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
883 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
886 // test with an event object
887 event = new jQuery.Event("foo");
888 event.secret = 'boo!';
889 $child.trigger(event);
891 // test with a literal object
892 $child.trigger({type:'foo', secret:'boo!'});
897 ok( false, "This assertion shouldn't be reached");
900 $parent.bind('foo', error );
902 $child.bind('foo',function(e, a, b, c ){
903 equals( arguments.length, 4, "Check arguments length");
904 equals( a, 1, "Check first custom argument");
905 equals( b, 2, "Check second custom argument");
906 equals( c, 3, "Check third custom argument");
908 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
909 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
910 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
913 e.stopImmediatePropagation();
918 // We should add this back in when we want to test the order
919 // in which event handlers are iterated.
920 //$child.bind('foo', error );
922 event = new jQuery.Event("foo");
923 $child.trigger( event, [1,2,3] ).unbind();
924 equals( event.result, "result", "Check event.result attribute");
926 // Will error if it bubbles
927 $child.triggerHandler('foo');
930 $parent.unbind().remove();
933 test("jQuery.Event.currentTarget", function(){
937 $elem = jQuery('<button>a</button>').click(function(e){
938 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
942 $elem.trigger('click');
948 test("toggle(Function, Function, ...)", function() {
952 fn1 = function(e) { count++; },
953 fn2 = function(e) { count--; },
954 preventDefault = function(e) { e.preventDefault() },
955 link = jQuery('#mark');
956 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
957 equals( count, 1, "Check for toggle(fn, fn)" );
959 jQuery("#firstp").toggle(function () {
960 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
961 }, function() {}).trigger("click", [ 1, 2, 3 ]);
964 jQuery("#simon1").one("click", function() {
965 ok( true, "Execute event only once" );
966 jQuery(this).toggle(function() {
967 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
969 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
972 }).click().click().click();
987 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
989 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
991 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
993 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
995 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
997 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
999 $div.unbind('click',fns[0]);
1000 var data = jQuery._data( $div[0], 'events' );
1001 ok( !data, "Unbinding one function from toggle unbinds them all");
1003 // manually clean up detached elements
1006 // Test Multi-Toggles
1008 $div = jQuery("<div/>");
1009 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
1011 same( a, [1], "Check that a click worked." );
1013 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
1015 same( a, [1,2], "Check that a click worked with a second toggle." );
1016 same( b, [1], "Check that a click worked with a second toggle." );
1019 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
1020 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
1022 // manually clean up detached elements
1026 test(".live()/.die()", function() {
1029 var submit = 0, div = 0, livea = 0, liveb = 0;
1031 jQuery("div").live("submit", function(){ submit++; return false; });
1032 jQuery("div").live("click", function(){ div++; });
1033 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
1034 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
1036 // Nothing should trigger on the body
1037 jQuery("body").trigger("click");
1038 equals( submit, 0, "Click on body" );
1039 equals( div, 0, "Click on body" );
1040 equals( livea, 0, "Click on body" );
1041 equals( liveb, 0, "Click on body" );
1043 // This should trigger two events
1044 submit = 0, div = 0, livea = 0, liveb = 0;
1045 jQuery("div#nothiddendiv").trigger("click");
1046 equals( submit, 0, "Click on div" );
1047 equals( div, 1, "Click on div" );
1048 equals( livea, 1, "Click on div" );
1049 equals( liveb, 0, "Click on div" );
1051 // This should trigger three events (w/ bubbling)
1052 submit = 0, div = 0, livea = 0, liveb = 0;
1053 jQuery("div#nothiddendivchild").trigger("click");
1054 equals( submit, 0, "Click on inner div" );
1055 equals( div, 2, "Click on inner div" );
1056 equals( livea, 1, "Click on inner div" );
1057 equals( liveb, 1, "Click on inner div" );
1059 // This should trigger one submit
1060 submit = 0, div = 0, livea = 0, liveb = 0;
1061 jQuery("div#nothiddendivchild").trigger("submit");
1062 equals( submit, 1, "Submit on div" );
1063 equals( div, 0, "Submit on div" );
1064 equals( livea, 0, "Submit on div" );
1065 equals( liveb, 0, "Submit on div" );
1067 // Make sure no other events were removed in the process
1068 submit = 0, div = 0, livea = 0, liveb = 0;
1069 jQuery("div#nothiddendivchild").trigger("click");
1070 equals( submit, 0, "die Click on inner div" );
1071 equals( div, 2, "die Click on inner div" );
1072 equals( livea, 1, "die Click on inner div" );
1073 equals( liveb, 1, "die Click on inner div" );
1075 // Now make sure that the removal works
1076 submit = 0, div = 0, livea = 0, liveb = 0;
1077 jQuery("div#nothiddendivchild").die("click");
1078 jQuery("div#nothiddendivchild").trigger("click");
1079 equals( submit, 0, "die Click on inner div" );
1080 equals( div, 2, "die Click on inner div" );
1081 equals( livea, 1, "die Click on inner div" );
1082 equals( liveb, 0, "die Click on inner div" );
1084 // Make sure that the click wasn't removed too early
1085 submit = 0, div = 0, livea = 0, liveb = 0;
1086 jQuery("div#nothiddendiv").trigger("click");
1087 equals( submit, 0, "die Click on inner div" );
1088 equals( div, 1, "die Click on inner div" );
1089 equals( livea, 1, "die Click on inner div" );
1090 equals( liveb, 0, "die Click on inner div" );
1092 // Make sure that stopPropgation doesn't stop live events
1093 submit = 0, div = 0, livea = 0, liveb = 0;
1094 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1095 jQuery("div#nothiddendivchild").trigger("click");
1096 equals( submit, 0, "stopPropagation Click on inner div" );
1097 equals( div, 1, "stopPropagation Click on inner div" );
1098 equals( livea, 0, "stopPropagation Click on inner div" );
1099 equals( liveb, 1, "stopPropagation Click on inner div" );
1101 // Make sure click events only fire with primary click
1102 submit = 0, div = 0, livea = 0, liveb = 0;
1103 var event = jQuery.Event("click");
1105 jQuery("div#nothiddendiv").trigger(event);
1107 equals( livea, 0, "live secondary click" );
1109 jQuery("div#nothiddendivchild").die("click");
1110 jQuery("div#nothiddendiv").die("click");
1111 jQuery("div").die("click");
1112 jQuery("div").die("submit");
1114 // Test binding with a different context
1115 var clicked = 0, container = jQuery('#main')[0];
1116 jQuery("#foo", container).live("click", function(e){ clicked++; });
1117 jQuery("div").trigger('click');
1118 jQuery("#foo").trigger('click');
1119 jQuery("#main").trigger('click');
1120 jQuery("body").trigger('click');
1121 equals( clicked, 2, "live with a context" );
1123 // Make sure the event is actually stored on the context
1124 ok( jQuery._data(container, "events").live, "live with a context" );
1126 // Test unbinding with a different context
1127 jQuery("#foo", container).die("click");
1128 jQuery("#foo").trigger('click');
1129 equals( clicked, 2, "die with a context");
1131 // Test binding with event data
1132 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1133 jQuery("#foo").trigger("click").die("click");
1135 // Test binding with trigger data
1136 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1137 jQuery("#foo").trigger("click", true).die("click");
1139 // Test binding with different this object
1140 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1141 jQuery("#foo").trigger("click").die("click");
1143 // Test binding with different this object, event data, and trigger data
1144 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1145 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1146 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1147 equals( data, true, "live with with different this object, event data, and trigger data")
1148 }, { foo: "bar" }));
1149 jQuery("#foo").trigger("click", true).die("click");
1151 // Verify that return false prevents default action
1152 jQuery("#anchor2").live("click", function(){ return false; });
1153 var hash = window.location.hash;
1154 jQuery("#anchor2").trigger("click");
1155 equals( window.location.hash, hash, "return false worked" );
1156 jQuery("#anchor2").die("click");
1158 // Verify that .preventDefault() prevents default action
1159 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1160 var hash = window.location.hash;
1161 jQuery("#anchor2").trigger("click");
1162 equals( window.location.hash, hash, "e.preventDefault() worked" );
1163 jQuery("#anchor2").die("click");
1165 // Test binding the same handler to multiple points
1167 function callback(){ called++; return false; }
1169 jQuery("#nothiddendiv").live("click", callback);
1170 jQuery("#anchor2").live("click", callback);
1172 jQuery("#nothiddendiv").trigger("click");
1173 equals( called, 1, "Verify that only one click occurred." );
1176 jQuery("#anchor2").trigger("click");
1177 equals( called, 1, "Verify that only one click occurred." );
1179 // Make sure that only one callback is removed
1180 jQuery("#anchor2").die("click", callback);
1183 jQuery("#nothiddendiv").trigger("click");
1184 equals( called, 1, "Verify that only one click occurred." );
1187 jQuery("#anchor2").trigger("click");
1188 equals( called, 0, "Verify that no click occurred." );
1190 // Make sure that it still works if the selector is the same,
1191 // but the event type is different
1192 jQuery("#nothiddendiv").live("foo", callback);
1195 jQuery("#nothiddendiv").die("click", callback);
1198 jQuery("#nothiddendiv").trigger("click");
1199 equals( called, 0, "Verify that no click occurred." );
1202 jQuery("#nothiddendiv").trigger("foo");
1203 equals( called, 1, "Verify that one foo occurred." );
1206 jQuery("#nothiddendiv").die("foo", callback);
1208 // Make sure we don't loose the target by DOM modifications
1209 // after the bubble already reached the liveHandler
1210 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1212 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1213 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1215 jQuery("#nothiddendiv span").click();
1216 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1217 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1220 jQuery("#nothiddendivchild").die("click");
1222 // Verify that .live() ocurs and cancel buble in the same order as
1223 // we would expect .bind() and .click() without delegation
1224 var lived = 0, livee = 0;
1226 // bind one pair in one order
1227 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1228 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1230 jQuery('span#liveSpan1 a').click();
1231 equals( lived, 1, "Verify that only one first handler occurred." );
1232 equals( livee, 0, "Verify that second handler doesn't." );
1234 // and one pair in inverse
1235 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1236 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1240 jQuery('span#liveSpan2 a').click();
1241 equals( lived, 1, "Verify that only one first handler occurred." );
1242 equals( livee, 0, "Verify that second handler doesn't." );
1245 jQuery("span#liveSpan1 a").die("click")
1246 jQuery("span#liveSpan1").die("click");
1247 jQuery("span#liveSpan2 a").die("click");
1248 jQuery("span#liveSpan2").die("click");
1250 // Test this, target and currentTarget are correct
1251 jQuery('span#liveSpan1').live('click', function(e){
1252 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1253 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1254 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1257 jQuery('span#liveSpan1 a').click();
1259 jQuery('span#liveSpan1').die('click');
1261 // Work with deep selectors
1264 function clickB(){ livee++; }
1266 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1267 jQuery("#nothiddendiv div").live("click", clickB);
1268 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1270 equals( livee, 0, "No clicks, deep selector." );
1273 jQuery("#nothiddendivchild").trigger("click");
1274 equals( livee, 2, "Click, deep selector." );
1277 jQuery("#nothiddendivchild").trigger("mouseover");
1278 equals( livee, 1, "Mouseover, deep selector." );
1280 jQuery("#nothiddendiv div").die("mouseover");
1283 jQuery("#nothiddendivchild").trigger("click");
1284 equals( livee, 2, "Click, deep selector." );
1287 jQuery("#nothiddendivchild").trigger("mouseover");
1288 equals( livee, 0, "Mouseover, deep selector." );
1290 jQuery("#nothiddendiv div").die("click", clickB);
1293 jQuery("#nothiddendivchild").trigger("click");
1294 equals( livee, 1, "Click, deep selector." );
1296 jQuery("#nothiddendiv div").die("click");
1298 jQuery("#nothiddendiv div").live("blur", function(){
1299 ok( true, "Live div trigger blur." );
1302 jQuery("#nothiddendiv div").trigger("blur");
1304 jQuery("#nothiddendiv div").die("blur");
1307 test("die all bound events", function(){
1311 var div = jQuery("div#nothiddendivchild");
1313 div.live("click submit", function(){ count++; });
1316 div.trigger("click");
1317 div.trigger("submit");
1319 equals( count, 0, "Make sure no events were triggered." );
1322 test("live with multiple events", function(){
1326 var div = jQuery("div#nothiddendivchild");
1328 div.live("click submit", function(){ count++; });
1330 div.trigger("click");
1331 div.trigger("submit");
1333 equals( count, 2, "Make sure both the click and submit were triggered." );
1335 // manually clean up events from elements outside the fixture
1339 test("live with namespaces", function(){
1342 var count1 = 0, count2 = 0;
1344 jQuery("#liveSpan1").live("foo.bar", function(e){
1348 jQuery("#liveSpan1").live("foo.zed", function(e){
1352 jQuery("#liveSpan1").trigger("foo.bar");
1353 equals( count1, 1, "Got live foo.bar" );
1354 equals( count2, 0, "Got live foo.bar" );
1356 count1 = 0, count2 = 0;
1358 jQuery("#liveSpan1").trigger("foo.zed");
1359 equals( count1, 0, "Got live foo.zed" );
1360 equals( count2, 1, "Got live foo.zed" );
1363 count1 = 0, count2 = 0;
1365 jQuery("#liveSpan1").die("foo.zed");
1366 jQuery("#liveSpan1").trigger("foo.bar");
1368 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1369 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1371 count1 = 0, count2 = 0;
1373 jQuery("#liveSpan1").trigger("foo.zed");
1374 equals( count1, 0, "Got live foo.zed" );
1375 equals( count2, 0, "Got live foo.zed" );
1378 jQuery("#liveSpan1").die("foo.bar");
1380 count1 = 0, count2 = 0;
1382 jQuery("#liveSpan1").trigger("foo.bar");
1383 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1384 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1386 jQuery("#liveSpan1").trigger("foo.zed");
1387 equals( count1, 0, "Did not trigger foo.zed again" );
1388 equals( count2, 0, "Did not trigger foo.zed again" );
1391 test("live with change", function(){
1394 var selectChange = 0, checkboxChange = 0;
1396 var select = jQuery("select[name='S1']")
1397 select.live("change", function() {
1401 var checkbox = jQuery("#check2"),
1402 checkboxFunction = function(){
1405 checkbox.live("change", checkboxFunction);
1407 // test click on select
1409 // second click that changed it
1411 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1412 select.trigger("change");
1413 equals( selectChange, 1, "Change on click." );
1415 // test keys on select
1417 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1418 select.trigger("change");
1419 equals( selectChange, 1, "Change on keyup." );
1421 // test click on checkbox
1422 checkbox.trigger("change");
1423 equals( checkboxChange, 1, "Change on checkbox." );
1425 // test blur/focus on text
1426 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1427 text.live("change", function() {
1431 text.val(oldTextVal+"foo");
1432 text.trigger("change");
1433 equals( textChange, 1, "Change on text input." );
1435 text.val(oldTextVal);
1438 // test blur/focus on password
1439 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1440 password.live("change", function() {
1444 password.val(oldPasswordVal + "foo");
1445 password.trigger("change");
1446 equals( passwordChange, 1, "Change on password input." );
1448 password.val(oldPasswordVal);
1449 password.die("change");
1451 // make sure die works
1455 select.die("change");
1456 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1457 select.trigger("change");
1458 equals( selectChange, 0, "Die on click works." );
1461 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1462 select.trigger("change");
1463 equals( selectChange, 0, "Die on keyup works." );
1465 // die specific checkbox
1466 checkbox.die("change", checkboxFunction);
1467 checkbox.trigger("change");
1468 equals( checkboxChange, 1, "Die on checkbox." );
1471 test("live with submit", function() {
1474 var count1 = 0, count2 = 0;
1476 jQuery("#testForm").live("submit", function(ev) {
1478 ev.preventDefault();
1481 jQuery("body").live("submit", function(ev) {
1483 ev.preventDefault();
1486 jQuery("#testForm input[name=sub1]").submit();
1487 equals( count1, 1, "Verify form submit." );
1488 equals( count2, 1, "Verify body submit." );
1490 jQuery("#testForm input[name=sub1]").live("click", function(ev) {
1491 ok( true, "cancelling submit still calls click handler" );
1494 jQuery("#testForm input[name=sub1]")[0].click();
1495 equals( count1, 2, "Verify form submit." );
1496 equals( count2, 2, "Verify body submit." );
1498 jQuery("#testForm").die("submit");
1499 jQuery("#testForm input[name=sub1]").die("click");
1500 jQuery("body").die("submit");
1503 test("live with special events", function() {
1506 jQuery.event.special.foo = {
1507 setup: function( data, namespaces, handler ) {
1508 ok( true, "Setup run." );
1510 teardown: function( namespaces ) {
1511 ok( true, "Teardown run." );
1513 add: function( handleObj ) {
1514 ok( true, "Add run." );
1516 remove: function( handleObj ) {
1517 ok( true, "Remove run." );
1519 _default: function( event ) {
1520 ok( true, "Default run." );
1525 jQuery("#liveSpan1").live("foo.a", function(e){
1526 ok( true, "Handler 1 run." );
1530 jQuery("#liveSpan1").live("foo.b", function(e){
1531 ok( true, "Handler 2 run." );
1534 // Run: Handler 1, Handler 2, Default
1535 jQuery("#liveSpan1").trigger("foo");
1537 // Run: Handler 1, Default
1538 // TODO: Namespace doesn't trigger default (?)
1539 jQuery("#liveSpan1").trigger("foo.a");
1542 jQuery("#liveSpan1").die("foo.a");
1544 // Run: Handler 2, Default
1545 jQuery("#liveSpan1").trigger("foo");
1547 // Run: remove, teardown
1548 jQuery("#liveSpan1").die("foo");
1550 delete jQuery.event.special.foo;
1553 test(".delegate()/.undelegate()", function() {
1556 var submit = 0, div = 0, livea = 0, liveb = 0;
1558 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1559 jQuery("#body").delegate("div", "click", function(){ div++; });
1560 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1561 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1563 // Nothing should trigger on the body
1564 jQuery("body").trigger("click");
1565 equals( submit, 0, "Click on body" );
1566 equals( div, 0, "Click on body" );
1567 equals( livea, 0, "Click on body" );
1568 equals( liveb, 0, "Click on body" );
1570 // This should trigger two events
1571 submit = 0, div = 0, livea = 0, liveb = 0;
1572 jQuery("div#nothiddendiv").trigger("click");
1573 equals( submit, 0, "Click on div" );
1574 equals( div, 1, "Click on div" );
1575 equals( livea, 1, "Click on div" );
1576 equals( liveb, 0, "Click on div" );
1578 // This should trigger three events (w/ bubbling)
1579 submit = 0, div = 0, livea = 0, liveb = 0;
1580 jQuery("div#nothiddendivchild").trigger("click");
1581 equals( submit, 0, "Click on inner div" );
1582 equals( div, 2, "Click on inner div" );
1583 equals( livea, 1, "Click on inner div" );
1584 equals( liveb, 1, "Click on inner div" );
1586 // This should trigger one submit
1587 submit = 0, div = 0, livea = 0, liveb = 0;
1588 jQuery("div#nothiddendivchild").trigger("submit");
1589 equals( submit, 1, "Submit on div" );
1590 equals( div, 0, "Submit on div" );
1591 equals( livea, 0, "Submit on div" );
1592 equals( liveb, 0, "Submit on div" );
1594 // Make sure no other events were removed in the process
1595 submit = 0, div = 0, livea = 0, liveb = 0;
1596 jQuery("div#nothiddendivchild").trigger("click");
1597 equals( submit, 0, "undelegate Click on inner div" );
1598 equals( div, 2, "undelegate Click on inner div" );
1599 equals( livea, 1, "undelegate Click on inner div" );
1600 equals( liveb, 1, "undelegate Click on inner div" );
1602 // Now make sure that the removal works
1603 submit = 0, div = 0, livea = 0, liveb = 0;
1604 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1605 jQuery("div#nothiddendivchild").trigger("click");
1606 equals( submit, 0, "undelegate Click on inner div" );
1607 equals( div, 2, "undelegate Click on inner div" );
1608 equals( livea, 1, "undelegate Click on inner div" );
1609 equals( liveb, 0, "undelegate Click on inner div" );
1611 // Make sure that the click wasn't removed too early
1612 submit = 0, div = 0, livea = 0, liveb = 0;
1613 jQuery("div#nothiddendiv").trigger("click");
1614 equals( submit, 0, "undelegate Click on inner div" );
1615 equals( div, 1, "undelegate Click on inner div" );
1616 equals( livea, 1, "undelegate Click on inner div" );
1617 equals( liveb, 0, "undelegate Click on inner div" );
1619 // Make sure that stopPropgation doesn't stop live events
1620 submit = 0, div = 0, livea = 0, liveb = 0;
1621 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1622 jQuery("div#nothiddendivchild").trigger("click");
1623 equals( submit, 0, "stopPropagation Click on inner div" );
1624 equals( div, 1, "stopPropagation Click on inner div" );
1625 equals( livea, 0, "stopPropagation Click on inner div" );
1626 equals( liveb, 1, "stopPropagation Click on inner div" );
1628 // Make sure click events only fire with primary click
1629 submit = 0, div = 0, livea = 0, liveb = 0;
1630 var event = jQuery.Event("click");
1632 jQuery("div#nothiddendiv").trigger(event);
1634 equals( livea, 0, "delegate secondary click" );
1636 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1637 jQuery("#body").undelegate("div#nothiddendiv", "click");
1638 jQuery("#body").undelegate("div", "click");
1639 jQuery("#body").undelegate("div", "submit");
1641 // Test binding with a different context
1642 var clicked = 0, container = jQuery('#main')[0];
1643 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1644 jQuery("div").trigger('click');
1645 jQuery("#foo").trigger('click');
1646 jQuery("#main").trigger('click');
1647 jQuery("body").trigger('click');
1648 equals( clicked, 2, "delegate with a context" );
1650 // Make sure the event is actually stored on the context
1651 ok( jQuery._data(container, "events").live, "delegate with a context" );
1653 // Test unbinding with a different context
1654 jQuery("#main").undelegate("#foo", "click");
1655 jQuery("#foo").trigger('click');
1656 equals( clicked, 2, "undelegate with a context");
1658 // Test binding with event data
1659 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1660 jQuery("#foo").trigger("click");
1661 jQuery("#body").undelegate("#foo", "click");
1663 // Test binding with trigger data
1664 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1665 jQuery("#foo").trigger("click", true);
1666 jQuery("#body").undelegate("#foo", "click");
1668 // Test binding with different this object
1669 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1670 jQuery("#foo").trigger("click");
1671 jQuery("#body").undelegate("#foo", "click");
1673 // Test binding with different this object, event data, and trigger data
1674 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1675 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1676 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1677 equals( data, true, "delegate with with different this object, event data, and trigger data")
1678 }, { foo: "bar" }));
1679 jQuery("#foo").trigger("click", true);
1680 jQuery("#body").undelegate("#foo", "click");
1682 // Verify that return false prevents default action
1683 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1684 var hash = window.location.hash;
1685 jQuery("#anchor2").trigger("click");
1686 equals( window.location.hash, hash, "return false worked" );
1687 jQuery("#body").undelegate("#anchor2", "click");
1689 // Verify that .preventDefault() prevents default action
1690 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1691 var hash = window.location.hash;
1692 jQuery("#anchor2").trigger("click");
1693 equals( window.location.hash, hash, "e.preventDefault() worked" );
1694 jQuery("#body").undelegate("#anchor2", "click");
1696 // Test binding the same handler to multiple points
1698 function callback(){ called++; return false; }
1700 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1701 jQuery("#body").delegate("#anchor2", "click", callback);
1703 jQuery("#nothiddendiv").trigger("click");
1704 equals( called, 1, "Verify that only one click occurred." );
1707 jQuery("#anchor2").trigger("click");
1708 equals( called, 1, "Verify that only one click occurred." );
1710 // Make sure that only one callback is removed
1711 jQuery("#body").undelegate("#anchor2", "click", callback);
1714 jQuery("#nothiddendiv").trigger("click");
1715 equals( called, 1, "Verify that only one click occurred." );
1718 jQuery("#anchor2").trigger("click");
1719 equals( called, 0, "Verify that no click occurred." );
1721 // Make sure that it still works if the selector is the same,
1722 // but the event type is different
1723 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1726 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1729 jQuery("#nothiddendiv").trigger("click");
1730 equals( called, 0, "Verify that no click occurred." );
1733 jQuery("#nothiddendiv").trigger("foo");
1734 equals( called, 1, "Verify that one foo occurred." );
1737 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1739 // Make sure we don't loose the target by DOM modifications
1740 // after the bubble already reached the liveHandler
1741 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1743 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1744 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1746 jQuery("#nothiddendiv span").click();
1747 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1748 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1751 jQuery("#body").undelegate("#nothiddendivchild", "click");
1753 // Verify that .live() ocurs and cancel buble in the same order as
1754 // we would expect .bind() and .click() without delegation
1755 var lived = 0, livee = 0;
1757 // bind one pair in one order
1758 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1759 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1761 jQuery('span#liveSpan1 a').click();
1762 equals( lived, 1, "Verify that only one first handler occurred." );
1763 equals( livee, 0, "Verify that second handler doesn't." );
1765 // and one pair in inverse
1766 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1767 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1771 jQuery('span#liveSpan2 a').click();
1772 equals( lived, 1, "Verify that only one first handler occurred." );
1773 equals( livee, 0, "Verify that second handler doesn't." );
1776 jQuery("#body").undelegate("click");
1778 // Test this, target and currentTarget are correct
1779 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1780 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1781 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1782 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1785 jQuery('span#liveSpan1 a').click();
1787 jQuery("#body").undelegate('span#liveSpan1', 'click');
1789 // Work with deep selectors
1792 function clickB(){ livee++; }
1794 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1795 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1796 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1798 equals( livee, 0, "No clicks, deep selector." );
1801 jQuery("#nothiddendivchild").trigger("click");
1802 equals( livee, 2, "Click, deep selector." );
1805 jQuery("#nothiddendivchild").trigger("mouseover");
1806 equals( livee, 1, "Mouseover, deep selector." );
1808 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1811 jQuery("#nothiddendivchild").trigger("click");
1812 equals( livee, 2, "Click, deep selector." );
1815 jQuery("#nothiddendivchild").trigger("mouseover");
1816 equals( livee, 0, "Mouseover, deep selector." );
1818 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1821 jQuery("#nothiddendivchild").trigger("click");
1822 equals( livee, 1, "Click, deep selector." );
1824 jQuery("#body").undelegate("#nothiddendiv div", "click");
1827 test("undelegate all bound events", function(){
1831 var div = jQuery("#body");
1833 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1836 jQuery("div#nothiddendivchild").trigger("click");
1837 jQuery("div#nothiddendivchild").trigger("submit");
1839 equals( count, 0, "Make sure no events were triggered." );
1842 test("delegate with multiple events", function(){
1846 var div = jQuery("#body");
1848 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1850 jQuery("div#nothiddendivchild").trigger("click");
1851 jQuery("div#nothiddendivchild").trigger("submit");
1853 equals( count, 2, "Make sure both the click and submit were triggered." );
1855 jQuery("#body").undelegate();
1858 test("delegate with change", function(){
1861 var selectChange = 0, checkboxChange = 0;
1863 var select = jQuery("select[name='S1']");
1864 jQuery("#body").delegate("select[name='S1']", "change", function() {
1868 var checkbox = jQuery("#check2"),
1869 checkboxFunction = function(){
1872 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1874 // test click on select
1876 // second click that changed it
1878 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1879 select.trigger("change");
1880 equals( selectChange, 1, "Change on click." );
1882 // test keys on select
1884 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1885 select.trigger("change");
1886 equals( selectChange, 1, "Change on keyup." );
1888 // test click on checkbox
1889 checkbox.trigger("change");
1890 equals( checkboxChange, 1, "Change on checkbox." );
1892 // test blur/focus on text
1893 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1894 jQuery("#body").delegate("#name", "change", function() {
1898 text.val(oldTextVal+"foo");
1899 text.trigger("change");
1900 equals( textChange, 1, "Change on text input." );
1902 text.val(oldTextVal);
1903 jQuery("#body").die("change");
1905 // test blur/focus on password
1906 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1907 jQuery("#body").delegate("#name", "change", function() {
1911 password.val(oldPasswordVal + "foo");
1912 password.trigger("change");
1913 equals( passwordChange, 1, "Change on password input." );
1915 password.val(oldPasswordVal);
1916 jQuery("#body").undelegate("#name", "change");
1918 // make sure die works
1922 jQuery("#body").undelegate("select[name='S1']", "change");
1923 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1924 select.trigger("change");
1925 equals( selectChange, 0, "Die on click works." );
1928 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1929 select.trigger("change");
1930 equals( selectChange, 0, "Die on keyup works." );
1932 // die specific checkbox
1933 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1934 checkbox.trigger("change");
1935 equals( checkboxChange, 1, "Die on checkbox." );
1938 test("delegate with submit", function() {
1939 var count1 = 0, count2 = 0;
1941 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1943 ev.preventDefault();
1946 jQuery(document).delegate("body", "submit", function(ev) {
1948 ev.preventDefault();
1951 jQuery("#testForm input[name=sub1]").submit();
1952 equals( count1, 1, "Verify form submit." );
1953 equals( count2, 1, "Verify body submit." );
1955 jQuery("#body").undelegate();
1956 jQuery(document).undelegate();
1959 test("Non DOM element events", function() {
1964 jQuery(o).bind('nonelementobj', function(e) {
1965 ok( true, "Event on non-DOM object triggered" );
1968 jQuery(o).trigger('nonelementobj');
1971 test("window resize", function() {
1974 jQuery(window).unbind();
1976 jQuery(window).bind("resize", function(){
1977 ok( true, "Resize event fired." );
1978 }).resize().unbind("resize");
1980 ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
1983 test("focusin bubbles", function() {
1986 var input = jQuery( '<input type="text" />' ).prependTo( "body" ),
1989 jQuery( "body" ).bind( "focusin.focusinBubblesTest", function(){
1990 equals( 1, order++, "focusin on the body second" );
1993 input.bind( "focusin.focusinBubblesTest", function(){
1994 equals( 0, order++, "focusin on the element first" );
1999 // jQuery trigger, which calls DOM focus
2002 input.trigger( "focus" );
2005 jQuery( "body" ).unbind( "focusin.focusinBubblesTest" );
2009 test("jQuery(function($) {})", function() {
2011 jQuery(function($) {
2012 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");
2017 test("event properties", function() {
2019 jQuery("#simon1").click(function(event) {
2020 ok( event.timeStamp, "assert event.timeStamp is present" );