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(), iframes", function() {
299 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
300 var doc = jQuery("#loadediframe").contents();
302 jQuery("div", doc).bind("click", function() {
303 ok( true, "Binding to element inside iframe" );
304 }).click().unbind('click');
307 test("bind(), trigger change on select", function() {
310 function selectOnChange(event) {
311 equals( event.data, counter++, "Event.data is not a global event object" );
313 jQuery("#form select").each(function(i){
314 jQuery(this).bind('change', i, selectOnChange);
315 }).trigger('change');
318 test("bind(), namespaced events, cloned events", function() {
321 jQuery("#firstp").bind("custom.test",function(e){
322 ok(true, "Custom event triggered");
325 jQuery("#firstp").bind("click",function(e){
326 ok(true, "Normal click triggered");
329 jQuery("#firstp").bind("click.test",function(e){
330 ok(true, "Namespaced click triggered");
333 // Trigger both bound fn (2)
334 jQuery("#firstp").trigger("click");
336 // Trigger one bound fn (1)
337 jQuery("#firstp").trigger("click.test");
339 // Remove only the one fn
340 jQuery("#firstp").unbind("click.test");
342 // Trigger the remaining fn (1)
343 jQuery("#firstp").trigger("click");
345 // Remove the remaining fn
346 jQuery("#firstp").unbind(".test");
348 // Trigger the remaining fn (0)
349 jQuery("#firstp").trigger("custom");
351 // using contents will get comments regular, text, and comment nodes
352 jQuery("#nonnodes").contents().bind("tester", function () {
353 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
354 }).trigger("tester");
356 // Make sure events stick with appendTo'd elements (which are cloned) #2027
357 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
358 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
361 test("bind(), multi-namespaced events", function() {
373 function check(name, msg){
374 same(name, order.shift(), msg);
377 jQuery("#firstp").bind("custom.test",function(e){
378 check("custom.test", "Custom event triggered");
381 jQuery("#firstp").bind("custom.test2",function(e){
382 check("custom.test2", "Custom event triggered");
385 jQuery("#firstp").bind("click.test",function(e){
386 check("click.test", "Normal click triggered");
389 jQuery("#firstp").bind("click.test.abc",function(e){
390 check("click.test.abc", "Namespaced click triggered");
393 // Those would not trigger/unbind (#5303)
394 jQuery("#firstp").trigger("click.a.test");
395 jQuery("#firstp").unbind("click.a.test");
397 // Trigger both bound fn (1)
398 jQuery("#firstp").trigger("click.test.abc");
400 // Trigger one bound fn (1)
401 jQuery("#firstp").trigger("click.abc");
403 // Trigger two bound fn (2)
404 jQuery("#firstp").trigger("click.test");
406 // Remove only the one fn
407 jQuery("#firstp").unbind("click.abc");
409 // Trigger the remaining fn (1)
410 jQuery("#firstp").trigger("click");
412 // Remove the remaining fn
413 jQuery("#firstp").unbind(".test");
415 // Trigger the remaining fn (1)
416 jQuery("#firstp").trigger("custom");
419 test("bind(), with same function", function() {
422 var count = 0 , func = function(){
426 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
427 jQuery("#liveHandlerOrder").trigger("foo.bar");
429 equals(count, 1, "Verify binding function with multiple namespaces." );
431 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
432 jQuery("#liveHandlerOrder").trigger("foo.bar");
434 equals(count, 1, "Verify that removing events still work." );
437 test("bind(), make sure order is maintained", function() {
440 var elem = jQuery("#firstp"), log = [], check = [];
442 for ( var i = 0; i < 100; i++ ) (function(i){
443 elem.bind( "click", function(){
450 elem.trigger("click");
452 equals( log.join(","), check.join(","), "Make sure order was maintained." );
454 elem.unbind("click");
457 test("bind(), with different this object", function() {
459 var thisObject = { myThis: true },
460 data = { myData: true },
461 handler1 = function( event ) {
462 equals( this, thisObject, "bind() with different this object" );
464 handler2 = function( event ) {
465 equals( this, thisObject, "bind() with different this object and data" );
466 equals( event.data, data, "bind() with different this object and data" );
470 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
471 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
473 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
476 test("bind(name, false), unbind(name, false)", function() {
480 jQuery("#main").bind("click", function(e){ main++; });
481 jQuery("#ap").trigger("click");
482 equals( main, 1, "Verify that the trigger happened correctly." );
485 jQuery("#ap").bind("click", false);
486 jQuery("#ap").trigger("click");
487 equals( main, 0, "Verify that no bubble happened." );
490 jQuery("#ap").unbind("click", false);
491 jQuery("#ap").trigger("click");
492 equals( main, 1, "Verify that the trigger happened correctly." );
495 test("bind()/trigger()/unbind() on plain object", function() {
500 // Make sure it doesn't complain when no events are found
501 jQuery(obj).trigger("test");
503 // Make sure it doesn't complain when no events are found
504 jQuery(obj).unbind("test");
508 ok( true, "Custom event run." );
511 ok( true, "Custom submit event run." );
515 var events = jQuery(obj).data("__events__");
516 ok( events, "Object has events bound." );
517 equals( obj.events, undefined, "Events object on plain objects is not events" );
518 equals( typeof events, "function", "'events' expando is a function on plain objects." );
519 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
520 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
523 jQuery(obj).trigger("test");
524 jQuery(obj).trigger("submit");
526 jQuery(obj).unbind("test");
527 jQuery(obj).unbind("submit");
530 jQuery(obj).trigger("test");
532 // Make sure it doesn't complain when no events are found
533 jQuery(obj).unbind("test");
535 equals( obj.__events__, undefined, "Make sure events object is removed" );
538 test("unbind(type)", function() {
541 var $elem = jQuery("#firstp"),
545 ok( false, message );
548 message = "unbind passing function";
549 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
551 message = "unbind all from event";
552 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
554 message = "unbind all";
555 $elem.bind('error1', error).unbind().triggerHandler('error1');
557 message = "unbind many with function";
558 $elem.bind('error1 error2',error)
559 .unbind('error1 error2', error )
560 .trigger('error1').triggerHandler('error2');
562 message = "unbind many"; // #3538
563 $elem.bind('error1 error2',error)
564 .unbind('error1 error2')
565 .trigger('error1').triggerHandler('error2');
567 message = "unbind without a type or handler";
568 $elem.bind("error1 error2.test",error)
570 .trigger("error1").triggerHandler("error2");
573 test("unbind(eventObject)", function() {
576 var $elem = jQuery("#firstp"),
579 function assert( expected ){
581 $elem.trigger('foo').triggerHandler('bar');
582 equals( num, expected, "Check the right handlers are triggered" );
586 // This handler shouldn't be unbound
587 .bind('foo', function(){
590 .bind('foo', function(e){
595 .bind('bar', function(){
609 test("hover()", function() {
611 handler1 = function( event ) { ++times; },
612 handler2 = function( event ) { ++times; };
615 .hover(handler1, handler2)
616 .mouseenter().mouseleave()
617 .unbind("mouseenter", handler1)
618 .unbind("mouseleave", handler2)
620 .mouseenter().mouseleave()
621 .unbind("mouseenter mouseleave", handler1)
622 .mouseenter().mouseleave();
624 equals( times, 4, "hover handlers fired" );
627 test("trigger() shortcuts", function() {
629 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
630 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
631 equals( close.length, 0, "Context element does not exist, length must be zero" );
632 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
636 jQuery("#check1").click(function() {
637 ok( true, "click event handler for checkbox gets fired twice, see #815" );
641 jQuery('#firstp')[0].onclick = function(event) {
644 jQuery('#firstp').click();
645 equals( counter, 1, "Check that click, triggers onclick event handler also" );
647 var clickCounter = 0;
648 jQuery('#simon1')[0].onclick = function(event) {
651 jQuery('#simon1').click();
652 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
654 jQuery('<img />').load(function(){
655 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
659 test("trigger() bubbling", function() {
662 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
664 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
665 jQuery("html").bind("click", function(e){ html++; });
666 jQuery("body").bind("click", function(e){ body++; });
667 jQuery("#main").bind("click", function(e){ main++; });
668 jQuery("#ap").bind("click", function(){ ap++; return false; });
670 jQuery("html").trigger("click");
671 equals( doc, 1, "HTML bubble" );
672 equals( html, 1, "HTML bubble" );
674 jQuery("body").trigger("click");
675 equals( doc, 2, "Body bubble" );
676 equals( html, 2, "Body bubble" );
677 equals( body, 1, "Body bubble" );
679 jQuery("#main").trigger("click");
680 equals( doc, 3, "Main bubble" );
681 equals( html, 3, "Main bubble" );
682 equals( body, 2, "Main bubble" );
683 equals( main, 1, "Main bubble" );
685 jQuery("#ap").trigger("click");
686 equals( doc, 3, "ap bubble" );
687 equals( html, 3, "ap bubble" );
688 equals( body, 2, "ap bubble" );
689 equals( main, 1, "ap bubble" );
690 equals( ap, 1, "ap bubble" );
693 test("trigger(type, [data], [fn])", function() {
696 var handler = function(event, a, b, c) {
697 equals( event.type, "click", "check passed data" );
698 equals( a, 1, "check passed data" );
699 equals( b, "2", "check passed data" );
700 equals( c, "abc", "check passed data" );
704 var $elem = jQuery("#firstp");
706 // Simulate a "native" click
707 $elem[0].click = function(){
708 ok( true, "Native call was triggered" );
711 // Triggers handlrs and native
713 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
715 // Simulate a "native" click
716 $elem[0].click = function(){
717 ok( false, "Native call was triggered" );
720 // Trigger only the handlers (no native)
722 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
726 jQuery('#form input:first').hide().trigger('focus');
730 ok( pass, "Trigger focus on hidden element" );
734 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
738 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
740 var form = jQuery("<form action=''></form>").appendTo("body");
742 // Make sure it can be prevented locally
743 form.submit(function(){
744 ok( true, "Local bind still works." );
749 form.trigger("submit");
751 form.unbind("submit");
753 jQuery(document).submit(function(){
754 ok( true, "Make sure bubble works up to document." );
759 form.trigger("submit");
761 jQuery(document).unbind("submit");
766 test("jQuery.Event.currentTarget", function(){
769 test("trigger(eventObject, [data], [fn])", function() {
772 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
773 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
775 var event = jQuery.Event("noNew");
776 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
777 equals( event.type, "noNew", "Verify its type" );
779 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
780 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
781 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
783 event.preventDefault();
784 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
785 event.stopPropagation();
786 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
788 event.isPropagationStopped = function(){ return false };
789 event.stopImmediatePropagation();
790 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
791 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
793 $parent.bind('foo',function(e){
795 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
796 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
797 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
798 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
801 // test with an event object
802 event = new jQuery.Event("foo");
803 event.secret = 'boo!';
804 $child.trigger(event);
806 // test with a literal object
807 $child.trigger({type:'foo', secret:'boo!'});
812 ok( false, "This assertion shouldn't be reached");
815 $parent.bind('foo', error );
817 $child.bind('foo',function(e, a, b, c ){
818 equals( arguments.length, 4, "Check arguments length");
819 equals( a, 1, "Check first custom argument");
820 equals( b, 2, "Check second custom argument");
821 equals( c, 3, "Check third custom argument");
823 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
824 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
825 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
828 e.stopImmediatePropagation();
833 // We should add this back in when we want to test the order
834 // in which event handlers are iterated.
835 //$child.bind('foo', error );
837 event = new jQuery.Event("foo");
838 $child.trigger( event, [1,2,3] ).unbind();
839 equals( event.result, "result", "Check event.result attribute");
841 // Will error if it bubbles
842 $child.triggerHandler('foo');
845 $parent.unbind().remove();
848 test("jQuery.Event.currentTarget", function(){
852 $elem = jQuery('<button>a</button>').click(function(e){
853 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
857 $elem.trigger('click');
863 test("toggle(Function, Function, ...)", function() {
867 fn1 = function(e) { count++; },
868 fn2 = function(e) { count--; },
869 preventDefault = function(e) { e.preventDefault() },
870 link = jQuery('#mark');
871 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
872 equals( count, 1, "Check for toggle(fn, fn)" );
874 jQuery("#firstp").toggle(function () {
875 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
876 }, function() {}).trigger("click", [ 1, 2, 3 ]);
879 jQuery("#simon1").one("click", function() {
880 ok( true, "Execute event only once" );
881 jQuery(this).toggle(function() {
882 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
884 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
887 }).click().click().click();
902 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
904 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
906 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
908 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
910 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
912 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
914 $div.unbind('click',fns[0]);
915 var data = jQuery.data( $div[0], 'events' );
916 ok( !data, "Unbinding one function from toggle unbinds them all");
918 // Test Multi-Toggles
920 $div = jQuery("<div/>");
921 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
923 same( a, [1], "Check that a click worked." );
925 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
927 same( a, [1,2], "Check that a click worked with a second toggle." );
928 same( b, [1], "Check that a click worked with a second toggle." );
931 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
932 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
935 test(".live()/.die()", function() {
938 var submit = 0, div = 0, livea = 0, liveb = 0;
940 jQuery("div").live("submit", function(){ submit++; return false; });
941 jQuery("div").live("click", function(){ div++; });
942 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
943 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
945 // Nothing should trigger on the body
946 jQuery("body").trigger("click");
947 equals( submit, 0, "Click on body" );
948 equals( div, 0, "Click on body" );
949 equals( livea, 0, "Click on body" );
950 equals( liveb, 0, "Click on body" );
952 // This should trigger two events
953 submit = 0, div = 0, livea = 0, liveb = 0;
954 jQuery("div#nothiddendiv").trigger("click");
955 equals( submit, 0, "Click on div" );
956 equals( div, 1, "Click on div" );
957 equals( livea, 1, "Click on div" );
958 equals( liveb, 0, "Click on div" );
960 // This should trigger three events (w/ bubbling)
961 submit = 0, div = 0, livea = 0, liveb = 0;
962 jQuery("div#nothiddendivchild").trigger("click");
963 equals( submit, 0, "Click on inner div" );
964 equals( div, 2, "Click on inner div" );
965 equals( livea, 1, "Click on inner div" );
966 equals( liveb, 1, "Click on inner div" );
968 // This should trigger one submit
969 submit = 0, div = 0, livea = 0, liveb = 0;
970 jQuery("div#nothiddendivchild").trigger("submit");
971 equals( submit, 1, "Submit on div" );
972 equals( div, 0, "Submit on div" );
973 equals( livea, 0, "Submit on div" );
974 equals( liveb, 0, "Submit on div" );
976 // Make sure no other events were removed in the process
977 submit = 0, div = 0, livea = 0, liveb = 0;
978 jQuery("div#nothiddendivchild").trigger("click");
979 equals( submit, 0, "die Click on inner div" );
980 equals( div, 2, "die Click on inner div" );
981 equals( livea, 1, "die Click on inner div" );
982 equals( liveb, 1, "die Click on inner div" );
984 // Now make sure that the removal works
985 submit = 0, div = 0, livea = 0, liveb = 0;
986 jQuery("div#nothiddendivchild").die("click");
987 jQuery("div#nothiddendivchild").trigger("click");
988 equals( submit, 0, "die Click on inner div" );
989 equals( div, 2, "die Click on inner div" );
990 equals( livea, 1, "die Click on inner div" );
991 equals( liveb, 0, "die Click on inner div" );
993 // Make sure that the click wasn't removed too early
994 submit = 0, div = 0, livea = 0, liveb = 0;
995 jQuery("div#nothiddendiv").trigger("click");
996 equals( submit, 0, "die Click on inner div" );
997 equals( div, 1, "die Click on inner div" );
998 equals( livea, 1, "die Click on inner div" );
999 equals( liveb, 0, "die Click on inner div" );
1001 // Make sure that stopPropgation doesn't stop live events
1002 submit = 0, div = 0, livea = 0, liveb = 0;
1003 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1004 jQuery("div#nothiddendivchild").trigger("click");
1005 equals( submit, 0, "stopPropagation Click on inner div" );
1006 equals( div, 1, "stopPropagation Click on inner div" );
1007 equals( livea, 0, "stopPropagation Click on inner div" );
1008 equals( liveb, 1, "stopPropagation Click on inner div" );
1010 // Make sure click events only fire with primary click
1011 submit = 0, div = 0, livea = 0, liveb = 0;
1012 var event = jQuery.Event("click");
1014 jQuery("div#nothiddendiv").trigger(event);
1016 equals( livea, 0, "live secondary click" );
1018 jQuery("div#nothiddendivchild").die("click");
1019 jQuery("div#nothiddendiv").die("click");
1020 jQuery("div").die("click");
1021 jQuery("div").die("submit");
1023 // Test binding with a different context
1024 var clicked = 0, container = jQuery('#main')[0];
1025 jQuery("#foo", container).live("click", function(e){ clicked++; });
1026 jQuery("div").trigger('click');
1027 jQuery("#foo").trigger('click');
1028 jQuery("#main").trigger('click');
1029 jQuery("body").trigger('click');
1030 equals( clicked, 2, "live with a context" );
1032 // Make sure the event is actually stored on the context
1033 ok( jQuery.data(container, "events").live, "live with a context" );
1035 // Test unbinding with a different context
1036 jQuery("#foo", container).die("click");
1037 jQuery("#foo").trigger('click');
1038 equals( clicked, 2, "die with a context");
1040 // Test binding with event data
1041 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1042 jQuery("#foo").trigger("click").die("click");
1044 // Test binding with trigger data
1045 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1046 jQuery("#foo").trigger("click", true).die("click");
1048 // Test binding with different this object
1049 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1050 jQuery("#foo").trigger("click").die("click");
1052 // Test binding with different this object, event data, and trigger data
1053 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1054 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1055 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1056 equals( data, true, "live with with different this object, event data, and trigger data")
1057 }, { foo: "bar" }));
1058 jQuery("#foo").trigger("click", true).die("click");
1060 // Verify that return false prevents default action
1061 jQuery("#anchor2").live("click", function(){ return false; });
1062 var hash = window.location.hash;
1063 jQuery("#anchor2").trigger("click");
1064 equals( window.location.hash, hash, "return false worked" );
1065 jQuery("#anchor2").die("click");
1067 // Verify that .preventDefault() prevents default action
1068 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1069 var hash = window.location.hash;
1070 jQuery("#anchor2").trigger("click");
1071 equals( window.location.hash, hash, "e.preventDefault() worked" );
1072 jQuery("#anchor2").die("click");
1074 // Test binding the same handler to multiple points
1076 function callback(){ called++; return false; }
1078 jQuery("#nothiddendiv").live("click", callback);
1079 jQuery("#anchor2").live("click", callback);
1081 jQuery("#nothiddendiv").trigger("click");
1082 equals( called, 1, "Verify that only one click occurred." );
1085 jQuery("#anchor2").trigger("click");
1086 equals( called, 1, "Verify that only one click occurred." );
1088 // Make sure that only one callback is removed
1089 jQuery("#anchor2").die("click", callback);
1092 jQuery("#nothiddendiv").trigger("click");
1093 equals( called, 1, "Verify that only one click occurred." );
1096 jQuery("#anchor2").trigger("click");
1097 equals( called, 0, "Verify that no click occurred." );
1099 // Make sure that it still works if the selector is the same,
1100 // but the event type is different
1101 jQuery("#nothiddendiv").live("foo", callback);
1104 jQuery("#nothiddendiv").die("click", callback);
1107 jQuery("#nothiddendiv").trigger("click");
1108 equals( called, 0, "Verify that no click occurred." );
1111 jQuery("#nothiddendiv").trigger("foo");
1112 equals( called, 1, "Verify that one foo occurred." );
1115 jQuery("#nothiddendiv").die("foo", callback);
1117 // Make sure we don't loose the target by DOM modifications
1118 // after the bubble already reached the liveHandler
1119 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1121 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1122 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1124 jQuery("#nothiddendiv span").click();
1125 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1126 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1129 jQuery("#nothiddendivchild").die("click");
1131 // Verify that .live() ocurs and cancel buble in the same order as
1132 // we would expect .bind() and .click() without delegation
1133 var lived = 0, livee = 0;
1135 // bind one pair in one order
1136 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1137 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1139 jQuery('span#liveSpan1 a').click();
1140 equals( lived, 1, "Verify that only one first handler occurred." );
1141 equals( livee, 0, "Verify that second handler doesn't." );
1143 // and one pair in inverse
1144 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1145 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1149 jQuery('span#liveSpan2 a').click();
1150 equals( lived, 1, "Verify that only one first handler occurred." );
1151 equals( livee, 0, "Verify that second handler doesn't." );
1154 jQuery("span#liveSpan1 a").die("click")
1155 jQuery("span#liveSpan1").die("click");
1156 jQuery("span#liveSpan2 a").die("click");
1157 jQuery("span#liveSpan2").die("click");
1159 // Test this, target and currentTarget are correct
1160 jQuery('span#liveSpan1').live('click', function(e){
1161 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1162 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1163 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1166 jQuery('span#liveSpan1 a').click();
1168 jQuery('span#liveSpan1').die('click');
1170 // Work with deep selectors
1173 function clickB(){ livee++; }
1175 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1176 jQuery("#nothiddendiv div").live("click", clickB);
1177 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1179 equals( livee, 0, "No clicks, deep selector." );
1182 jQuery("#nothiddendivchild").trigger("click");
1183 equals( livee, 2, "Click, deep selector." );
1186 jQuery("#nothiddendivchild").trigger("mouseover");
1187 equals( livee, 1, "Mouseover, deep selector." );
1189 jQuery("#nothiddendiv div").die("mouseover");
1192 jQuery("#nothiddendivchild").trigger("click");
1193 equals( livee, 2, "Click, deep selector." );
1196 jQuery("#nothiddendivchild").trigger("mouseover");
1197 equals( livee, 0, "Mouseover, deep selector." );
1199 jQuery("#nothiddendiv div").die("click", clickB);
1202 jQuery("#nothiddendivchild").trigger("click");
1203 equals( livee, 1, "Click, deep selector." );
1205 jQuery("#nothiddendiv div").die("click");
1207 jQuery("#nothiddendiv div").live("blur", function(){
1208 ok( true, "Live div trigger blur." );
1211 jQuery("#nothiddendiv div").trigger("blur");
1213 jQuery("#nothiddendiv div").die("blur");
1216 test("die all bound events", function(){
1220 var div = jQuery("div#nothiddendivchild");
1222 div.live("click submit", function(){ count++; });
1225 div.trigger("click");
1226 div.trigger("submit");
1228 equals( count, 0, "Make sure no events were triggered." );
1231 test("live with multiple events", function(){
1235 var div = jQuery("div#nothiddendivchild");
1237 div.live("click submit", function(){ count++; });
1239 div.trigger("click");
1240 div.trigger("submit");
1242 equals( count, 2, "Make sure both the click and submit were triggered." );
1245 test("live with namespaces", function(){
1248 var count1 = 0, count2 = 0;
1250 jQuery("#liveSpan1").live("foo.bar", function(e){
1254 jQuery("#liveSpan1").live("foo.zed", function(e){
1258 jQuery("#liveSpan1").trigger("foo.bar");
1259 equals( count1, 1, "Got live foo.bar" );
1260 equals( count2, 0, "Got live foo.bar" );
1262 count1 = 0, count2 = 0;
1264 jQuery("#liveSpan1").trigger("foo.zed");
1265 equals( count1, 0, "Got live foo.zed" );
1266 equals( count2, 1, "Got live foo.zed" );
1269 count1 = 0, count2 = 0;
1271 jQuery("#liveSpan1").die("foo.zed");
1272 jQuery("#liveSpan1").trigger("foo.bar");
1274 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1275 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1277 count1 = 0, count2 = 0;
1279 jQuery("#liveSpan1").trigger("foo.zed");
1280 equals( count1, 0, "Got live foo.zed" );
1281 equals( count2, 0, "Got live foo.zed" );
1284 jQuery("#liveSpan1").die("foo.bar");
1286 count1 = 0, count2 = 0;
1288 jQuery("#liveSpan1").trigger("foo.bar");
1289 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1290 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1292 jQuery("#liveSpan1").trigger("foo.zed");
1293 equals( count1, 0, "Did not trigger foo.zed again" );
1294 equals( count2, 0, "Did not trigger foo.zed again" );
1297 test("live with change", function(){
1300 var selectChange = 0, checkboxChange = 0;
1302 var select = jQuery("select[name='S1']")
1303 select.live("change", function() {
1307 var checkbox = jQuery("#check2"),
1308 checkboxFunction = function(){
1311 checkbox.live("change", checkboxFunction);
1313 // test click on select
1315 // second click that changed it
1317 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1318 select.trigger("change");
1319 equals( selectChange, 1, "Change on click." );
1321 // test keys on select
1323 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1324 select.trigger("change");
1325 equals( selectChange, 1, "Change on keyup." );
1327 // test click on checkbox
1328 checkbox.trigger("change");
1329 equals( checkboxChange, 1, "Change on checkbox." );
1331 // test blur/focus on text
1332 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1333 text.live("change", function() {
1337 text.val(oldTextVal+"foo");
1338 text.trigger("change");
1339 equals( textChange, 1, "Change on text input." );
1341 text.val(oldTextVal);
1344 // test blur/focus on password
1345 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1346 password.live("change", function() {
1350 password.val(oldPasswordVal + "foo");
1351 password.trigger("change");
1352 equals( passwordChange, 1, "Change on password input." );
1354 password.val(oldPasswordVal);
1355 password.die("change");
1357 // make sure die works
1361 select.die("change");
1362 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1363 select.trigger("change");
1364 equals( selectChange, 0, "Die on click works." );
1367 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1368 select.trigger("change");
1369 equals( selectChange, 0, "Die on keyup works." );
1371 // die specific checkbox
1372 checkbox.die("change", checkboxFunction);
1373 checkbox.trigger("change");
1374 equals( checkboxChange, 1, "Die on checkbox." );
1377 test("live with submit", function() {
1378 var count1 = 0, count2 = 0;
1380 jQuery("#testForm").live("submit", function(ev) {
1382 ev.preventDefault();
1385 jQuery("body").live("submit", function(ev) {
1387 ev.preventDefault();
1390 jQuery("#testForm input[name=sub1]").submit();
1391 equals( count1, 1, "Verify form submit." );
1392 equals( count2, 1, "Verify body submit." );
1394 jQuery("#testForm").die("submit");
1395 jQuery("body").die("submit");
1398 test("live with special events", function() {
1401 jQuery.event.special.foo = {
1402 setup: function( data, namespaces, handler ) {
1403 ok( true, "Setup run." );
1405 teardown: function( namespaces ) {
1406 ok( true, "Teardown run." );
1408 add: function( handleObj ) {
1409 ok( true, "Add run." );
1411 remove: function( handleObj ) {
1412 ok( true, "Remove run." );
1414 _default: function( event ) {
1415 ok( true, "Default run." );
1420 jQuery("#liveSpan1").live("foo.a", function(e){
1421 ok( true, "Handler 1 run." );
1425 jQuery("#liveSpan1").live("foo.b", function(e){
1426 ok( true, "Handler 2 run." );
1429 // Run: Handler 1, Handler 2, Default
1430 jQuery("#liveSpan1").trigger("foo");
1432 // Run: Handler 1, Default
1433 // TODO: Namespace doesn't trigger default (?)
1434 jQuery("#liveSpan1").trigger("foo.a");
1437 jQuery("#liveSpan1").die("foo.a");
1439 // Run: Handler 2, Default
1440 jQuery("#liveSpan1").trigger("foo");
1442 // Run: remove, teardown
1443 jQuery("#liveSpan1").die("foo");
1445 delete jQuery.event.special.foo;
1448 test(".delegate()/.undelegate()", function() {
1451 var submit = 0, div = 0, livea = 0, liveb = 0;
1453 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1454 jQuery("#body").delegate("div", "click", function(){ div++; });
1455 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1456 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1458 // Nothing should trigger on the body
1459 jQuery("body").trigger("click");
1460 equals( submit, 0, "Click on body" );
1461 equals( div, 0, "Click on body" );
1462 equals( livea, 0, "Click on body" );
1463 equals( liveb, 0, "Click on body" );
1465 // This should trigger two events
1466 submit = 0, div = 0, livea = 0, liveb = 0;
1467 jQuery("div#nothiddendiv").trigger("click");
1468 equals( submit, 0, "Click on div" );
1469 equals( div, 1, "Click on div" );
1470 equals( livea, 1, "Click on div" );
1471 equals( liveb, 0, "Click on div" );
1473 // This should trigger three events (w/ bubbling)
1474 submit = 0, div = 0, livea = 0, liveb = 0;
1475 jQuery("div#nothiddendivchild").trigger("click");
1476 equals( submit, 0, "Click on inner div" );
1477 equals( div, 2, "Click on inner div" );
1478 equals( livea, 1, "Click on inner div" );
1479 equals( liveb, 1, "Click on inner div" );
1481 // This should trigger one submit
1482 submit = 0, div = 0, livea = 0, liveb = 0;
1483 jQuery("div#nothiddendivchild").trigger("submit");
1484 equals( submit, 1, "Submit on div" );
1485 equals( div, 0, "Submit on div" );
1486 equals( livea, 0, "Submit on div" );
1487 equals( liveb, 0, "Submit on div" );
1489 // Make sure no other events were removed in the process
1490 submit = 0, div = 0, livea = 0, liveb = 0;
1491 jQuery("div#nothiddendivchild").trigger("click");
1492 equals( submit, 0, "undelegate Click on inner div" );
1493 equals( div, 2, "undelegate Click on inner div" );
1494 equals( livea, 1, "undelegate Click on inner div" );
1495 equals( liveb, 1, "undelegate Click on inner div" );
1497 // Now make sure that the removal works
1498 submit = 0, div = 0, livea = 0, liveb = 0;
1499 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1500 jQuery("div#nothiddendivchild").trigger("click");
1501 equals( submit, 0, "undelegate Click on inner div" );
1502 equals( div, 2, "undelegate Click on inner div" );
1503 equals( livea, 1, "undelegate Click on inner div" );
1504 equals( liveb, 0, "undelegate Click on inner div" );
1506 // Make sure that the click wasn't removed too early
1507 submit = 0, div = 0, livea = 0, liveb = 0;
1508 jQuery("div#nothiddendiv").trigger("click");
1509 equals( submit, 0, "undelegate Click on inner div" );
1510 equals( div, 1, "undelegate Click on inner div" );
1511 equals( livea, 1, "undelegate Click on inner div" );
1512 equals( liveb, 0, "undelegate Click on inner div" );
1514 // Make sure that stopPropgation doesn't stop live events
1515 submit = 0, div = 0, livea = 0, liveb = 0;
1516 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1517 jQuery("div#nothiddendivchild").trigger("click");
1518 equals( submit, 0, "stopPropagation Click on inner div" );
1519 equals( div, 1, "stopPropagation Click on inner div" );
1520 equals( livea, 0, "stopPropagation Click on inner div" );
1521 equals( liveb, 1, "stopPropagation Click on inner div" );
1523 // Make sure click events only fire with primary click
1524 submit = 0, div = 0, livea = 0, liveb = 0;
1525 var event = jQuery.Event("click");
1527 jQuery("div#nothiddendiv").trigger(event);
1529 equals( livea, 0, "delegate secondary click" );
1531 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1532 jQuery("#body").undelegate("div#nothiddendiv", "click");
1533 jQuery("#body").undelegate("div", "click");
1534 jQuery("#body").undelegate("div", "submit");
1536 // Test binding with a different context
1537 var clicked = 0, container = jQuery('#main')[0];
1538 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1539 jQuery("div").trigger('click');
1540 jQuery("#foo").trigger('click');
1541 jQuery("#main").trigger('click');
1542 jQuery("body").trigger('click');
1543 equals( clicked, 2, "delegate with a context" );
1545 // Make sure the event is actually stored on the context
1546 ok( jQuery.data(container, "events").live, "delegate with a context" );
1548 // Test unbinding with a different context
1549 jQuery("#main").undelegate("#foo", "click");
1550 jQuery("#foo").trigger('click');
1551 equals( clicked, 2, "undelegate with a context");
1553 // Test binding with event data
1554 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1555 jQuery("#foo").trigger("click");
1556 jQuery("#body").undelegate("#foo", "click");
1558 // Test binding with trigger data
1559 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1560 jQuery("#foo").trigger("click", true);
1561 jQuery("#body").undelegate("#foo", "click");
1563 // Test binding with different this object
1564 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1565 jQuery("#foo").trigger("click");
1566 jQuery("#body").undelegate("#foo", "click");
1568 // Test binding with different this object, event data, and trigger data
1569 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1570 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1571 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1572 equals( data, true, "delegate with with different this object, event data, and trigger data")
1573 }, { foo: "bar" }));
1574 jQuery("#foo").trigger("click", true);
1575 jQuery("#body").undelegate("#foo", "click");
1577 // Verify that return false prevents default action
1578 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1579 var hash = window.location.hash;
1580 jQuery("#anchor2").trigger("click");
1581 equals( window.location.hash, hash, "return false worked" );
1582 jQuery("#body").undelegate("#anchor2", "click");
1584 // Verify that .preventDefault() prevents default action
1585 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1586 var hash = window.location.hash;
1587 jQuery("#anchor2").trigger("click");
1588 equals( window.location.hash, hash, "e.preventDefault() worked" );
1589 jQuery("#body").undelegate("#anchor2", "click");
1591 // Test binding the same handler to multiple points
1593 function callback(){ called++; return false; }
1595 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1596 jQuery("#body").delegate("#anchor2", "click", callback);
1598 jQuery("#nothiddendiv").trigger("click");
1599 equals( called, 1, "Verify that only one click occurred." );
1602 jQuery("#anchor2").trigger("click");
1603 equals( called, 1, "Verify that only one click occurred." );
1605 // Make sure that only one callback is removed
1606 jQuery("#body").undelegate("#anchor2", "click", callback);
1609 jQuery("#nothiddendiv").trigger("click");
1610 equals( called, 1, "Verify that only one click occurred." );
1613 jQuery("#anchor2").trigger("click");
1614 equals( called, 0, "Verify that no click occurred." );
1616 // Make sure that it still works if the selector is the same,
1617 // but the event type is different
1618 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1621 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1624 jQuery("#nothiddendiv").trigger("click");
1625 equals( called, 0, "Verify that no click occurred." );
1628 jQuery("#nothiddendiv").trigger("foo");
1629 equals( called, 1, "Verify that one foo occurred." );
1632 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1634 // Make sure we don't loose the target by DOM modifications
1635 // after the bubble already reached the liveHandler
1636 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1638 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1639 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1641 jQuery("#nothiddendiv span").click();
1642 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1643 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1646 jQuery("#body").undelegate("#nothiddendivchild", "click");
1648 // Verify that .live() ocurs and cancel buble in the same order as
1649 // we would expect .bind() and .click() without delegation
1650 var lived = 0, livee = 0;
1652 // bind one pair in one order
1653 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1654 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1656 jQuery('span#liveSpan1 a').click();
1657 equals( lived, 1, "Verify that only one first handler occurred." );
1658 equals( livee, 0, "Verify that second handler doesn't." );
1660 // and one pair in inverse
1661 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1662 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1666 jQuery('span#liveSpan2 a').click();
1667 equals( lived, 1, "Verify that only one first handler occurred." );
1668 equals( livee, 0, "Verify that second handler doesn't." );
1671 jQuery("#body").undelegate("click");
1673 // Test this, target and currentTarget are correct
1674 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1675 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1676 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1677 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1680 jQuery('span#liveSpan1 a').click();
1682 jQuery("#body").undelegate('span#liveSpan1', 'click');
1684 // Work with deep selectors
1687 function clickB(){ livee++; }
1689 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1690 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1691 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1693 equals( livee, 0, "No clicks, deep selector." );
1696 jQuery("#nothiddendivchild").trigger("click");
1697 equals( livee, 2, "Click, deep selector." );
1700 jQuery("#nothiddendivchild").trigger("mouseover");
1701 equals( livee, 1, "Mouseover, deep selector." );
1703 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1706 jQuery("#nothiddendivchild").trigger("click");
1707 equals( livee, 2, "Click, deep selector." );
1710 jQuery("#nothiddendivchild").trigger("mouseover");
1711 equals( livee, 0, "Mouseover, deep selector." );
1713 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1716 jQuery("#nothiddendivchild").trigger("click");
1717 equals( livee, 1, "Click, deep selector." );
1719 jQuery("#body").undelegate("#nothiddendiv div", "click");
1722 test("undelegate all bound events", function(){
1726 var div = jQuery("#body");
1728 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1731 jQuery("div#nothiddendivchild").trigger("click");
1732 jQuery("div#nothiddendivchild").trigger("submit");
1734 equals( count, 0, "Make sure no events were triggered." );
1737 test("delegate with multiple events", function(){
1741 var div = jQuery("#body");
1743 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1745 jQuery("div#nothiddendivchild").trigger("click");
1746 jQuery("div#nothiddendivchild").trigger("submit");
1748 equals( count, 2, "Make sure both the click and submit were triggered." );
1750 jQuery("#body").undelegate();
1753 test("delegate with change", function(){
1756 var selectChange = 0, checkboxChange = 0;
1758 var select = jQuery("select[name='S1']");
1759 jQuery("#body").delegate("select[name='S1']", "change", function() {
1763 var checkbox = jQuery("#check2"),
1764 checkboxFunction = function(){
1767 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1769 // test click on select
1771 // second click that changed it
1773 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1774 select.trigger("change");
1775 equals( selectChange, 1, "Change on click." );
1777 // test keys on select
1779 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1780 select.trigger("change");
1781 equals( selectChange, 1, "Change on keyup." );
1783 // test click on checkbox
1784 checkbox.trigger("change");
1785 equals( checkboxChange, 1, "Change on checkbox." );
1787 // test blur/focus on text
1788 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1789 jQuery("#body").delegate("#name", "change", function() {
1793 text.val(oldTextVal+"foo");
1794 text.trigger("change");
1795 equals( textChange, 1, "Change on text input." );
1797 text.val(oldTextVal);
1798 jQuery("#body").die("change");
1800 // test blur/focus on password
1801 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1802 jQuery("#body").delegate("#name", "change", function() {
1806 password.val(oldPasswordVal + "foo");
1807 password.trigger("change");
1808 equals( passwordChange, 1, "Change on password input." );
1810 password.val(oldPasswordVal);
1811 jQuery("#body").undelegate("#name", "change");
1813 // make sure die works
1817 jQuery("#body").undelegate("select[name='S1']", "change");
1818 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1819 select.trigger("change");
1820 equals( selectChange, 0, "Die on click works." );
1823 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1824 select.trigger("change");
1825 equals( selectChange, 0, "Die on keyup works." );
1827 // die specific checkbox
1828 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1829 checkbox.trigger("change");
1830 equals( checkboxChange, 1, "Die on checkbox." );
1833 test("delegate with submit", function() {
1834 var count1 = 0, count2 = 0;
1836 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1838 ev.preventDefault();
1841 jQuery(document).delegate("body", "submit", function(ev) {
1843 ev.preventDefault();
1846 jQuery("#testForm input[name=sub1]").submit();
1847 equals( count1, 1, "Verify form submit." );
1848 equals( count2, 1, "Verify body submit." );
1850 jQuery("#body").undelegate();
1851 jQuery(document).undelegate();
1854 test("Non DOM element events", function() {
1859 jQuery(o).bind('nonelementobj', function(e) {
1860 ok( true, "Event on non-DOM object triggered" );
1863 jQuery(o).trigger('nonelementobj');
1866 test("window resize", function() {
1869 jQuery(window).unbind();
1871 jQuery(window).bind("resize", function(){
1872 ok( true, "Resize event fired." );
1873 }).resize().unbind("resize");
1875 ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1878 test("focusin bubbles", function() {
1879 //create an input and focusin on it
1880 var input = jQuery("<input/>"), order = 0;
1882 input.prependTo("body");
1884 jQuery("body").bind("focusin.focusinBubblesTest",function(){
1885 equals(1,order++,"focusin on the body second")
1888 input.bind("focusin.focusinBubblesTest",function(){
1889 equals(0,order++,"focusin on the element first")
1895 jQuery("body").unbind("focusin.focusinBubblesTest");
1899 test("jQuery(function($) {})", function() {
1901 jQuery(function($) {
1902 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");
1907 test("event properties", function() {
1909 jQuery("#simon1").click(function(event) {
1910 ok( event.timeStamp, "assert event.timeStamp is present" );