3 test("bind(), with data", function() {
5 var handler = function(event) {
6 ok( event.data, "bind() with data, check passed data exists" );
7 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
9 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
11 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
14 test("bind(), with data, trigger with data", function() {
16 var handler = function(event, data) {
17 ok( event.data, "check passed data exists" );
18 equals( event.data.foo, "bar", "Check value of passed data" );
19 ok( data, "Check trigger data" );
20 equals( data.bar, "foo", "Check value of trigger data" );
22 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
25 test("bind(), multiple events at once", function() {
29 var handler = function(event) {
30 if (event.type == "click")
32 else if (event.type == "mouseover")
33 mouseoverCounter += 1;
35 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
36 equals( clickCounter, 1, "bind() with multiple events at once" );
37 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
40 test("bind(), multiple events at once and namespaces", function() {
45 var div = jQuery("<div/>").bind("focusin.a", function(e) {
46 equals( e.type, cur, "Verify right single event was fired." );
50 div.trigger("focusin.a");
52 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
53 equals( e.type, cur, "Verify right multi event was fired." );
54 equals( e.data, obj, "Make sure the data came in correctly." );
61 div.trigger("mouseover");
63 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
64 equals( e.type, cur, "Verify right multi event was fired." );
68 div.trigger("focusin.a");
71 div.trigger("focusout.b");
74 test("bind(), namespace with special add", function() {
77 var div = jQuery("<div/>").bind("test", function(e) {
78 ok( true, "Test event fired." );
83 jQuery.event.special.test = {
84 _default: function(e) {
85 equals( this, document, "Make sure we're at the top of the chain." );
86 equals( e.type, "test", "And that we're still dealing with a test event." );
87 equals( e.target, div[0], "And that the target is correct." );
91 ok(true, "Teardown called.");
93 add: function( handleObj ) {
94 var handler = handleObj.handler;
95 handleObj.handler = function(e) {
97 handler.apply( this, arguments );
100 remove: function() {}
103 div.bind("test.a", {x: 1}, function(e) {
104 ok( !!e.xyz, "Make sure that the data is getting passed through." );
105 equals( e.data.x, 1, "Make sure data is attached properly." );
108 div.bind("test.b", {x: 2}, function(e) {
109 ok( !!e.xyz, "Make sure that the data is getting passed through." );
110 equals( e.data.x, 2, "Make sure data is attached properly." );
117 div.trigger("test.a");
120 div.trigger("test.b");
125 test("bind(), no data", function() {
127 var handler = function(event) {
128 ok ( !event.data, "Check that no data is added to the event object" );
130 jQuery("#firstp").bind("click", handler).trigger("click");
133 test("bind/one/unbind(Object)", function(){
136 var clickCounter = 0, mouseoverCounter = 0;
137 function handler(event) {
138 if (event.type == "click")
140 else if (event.type == "mouseover")
144 function handlerWithData(event) {
145 if (event.type == "click")
146 clickCounter += event.data;
147 else if (event.type == "mouseover")
148 mouseoverCounter += event.data;
152 $elem.trigger("click").trigger("mouseover");
155 var $elem = jQuery("#firstp")
163 click:handlerWithData,
164 mouseover:handlerWithData
169 equals( clickCounter, 3, "bind(Object)" );
170 equals( mouseoverCounter, 3, "bind(Object)" );
173 equals( clickCounter, 4, "bind(Object)" );
174 equals( mouseoverCounter, 4, "bind(Object)" );
176 jQuery("#firstp").unbind({
182 equals( clickCounter, 4, "bind(Object)" );
183 equals( mouseoverCounter, 4, "bind(Object)" );
186 test("bind(), iframes", function() {
187 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
188 var doc = jQuery("#loadediframe").contents();
190 jQuery("div", doc).bind("click", function() {
191 ok( true, "Binding to element inside iframe" );
192 }).click().unbind('click');
195 test("bind(), trigger change on select", function() {
198 function selectOnChange(event) {
199 equals( event.data, counter++, "Event.data is not a global event object" );
201 jQuery("#form select").each(function(i){
202 jQuery(this).bind('change', i, selectOnChange);
203 }).trigger('change');
206 test("bind(), namespaced events, cloned events", function() {
209 jQuery("#firstp").bind("custom.test",function(e){
210 ok(true, "Custom event triggered");
213 jQuery("#firstp").bind("click",function(e){
214 ok(true, "Normal click triggered");
217 jQuery("#firstp").bind("click.test",function(e){
218 ok(true, "Namespaced click triggered");
221 // Trigger both bound fn (2)
222 jQuery("#firstp").trigger("click");
224 // Trigger one bound fn (1)
225 jQuery("#firstp").trigger("click.test");
227 // Remove only the one fn
228 jQuery("#firstp").unbind("click.test");
230 // Trigger the remaining fn (1)
231 jQuery("#firstp").trigger("click");
233 // Remove the remaining fn
234 jQuery("#firstp").unbind(".test");
236 // Trigger the remaining fn (0)
237 jQuery("#firstp").trigger("custom");
239 // using contents will get comments regular, text, and comment nodes
240 jQuery("#nonnodes").contents().bind("tester", function () {
241 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
242 }).trigger("tester");
244 // Make sure events stick with appendTo'd elements (which are cloned) #2027
245 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
246 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
249 test("bind(), multi-namespaced events", function() {
261 function check(name, msg){
262 same(name, order.shift(), msg);
265 jQuery("#firstp").bind("custom.test",function(e){
266 check("custom.test", "Custom event triggered");
269 jQuery("#firstp").bind("custom.test2",function(e){
270 check("custom.test2", "Custom event triggered");
273 jQuery("#firstp").bind("click.test",function(e){
274 check("click.test", "Normal click triggered");
277 jQuery("#firstp").bind("click.test.abc",function(e){
278 check("click.test.abc", "Namespaced click triggered");
281 // Those would not trigger/unbind (#5303)
282 jQuery("#firstp").trigger("click.a.test");
283 jQuery("#firstp").unbind("click.a.test");
285 // Trigger both bound fn (1)
286 jQuery("#firstp").trigger("click.test.abc");
288 // Trigger one bound fn (1)
289 jQuery("#firstp").trigger("click.abc");
291 // Trigger two bound fn (2)
292 jQuery("#firstp").trigger("click.test");
294 // Remove only the one fn
295 jQuery("#firstp").unbind("click.abc");
297 // Trigger the remaining fn (1)
298 jQuery("#firstp").trigger("click");
300 // Remove the remaining fn
301 jQuery("#firstp").unbind(".test");
303 // Trigger the remaining fn (1)
304 jQuery("#firstp").trigger("custom");
307 test("bind(), with same function", function() {
310 var count = 0 , func = function(){
314 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
315 jQuery("#liveHandlerOrder").trigger("foo.bar");
317 equals(count, 1, "Verify binding function with multiple namespaces." );
319 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
320 jQuery("#liveHandlerOrder").trigger("foo.bar");
322 equals(count, 1, "Verify that removing events still work." );
325 test("bind(), make sure order is maintained", function() {
328 var elem = jQuery("#firstp"), log = [], check = [];
330 for ( var i = 0; i < 100; i++ ) (function(i){
331 elem.bind( "click", function(){
338 elem.trigger("click");
340 equals( log.join(","), check.join(","), "Make sure order was maintained." );
342 elem.unbind("click");
345 test("bind(), with different this object", function() {
347 var thisObject = { myThis: true },
348 data = { myData: true },
349 handler1 = function( event ) {
350 equals( this, thisObject, "bind() with different this object" );
352 handler2 = function( event ) {
353 equals( this, thisObject, "bind() with different this object and data" );
354 equals( event.data, data, "bind() with different this object and data" );
358 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
359 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
361 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
364 test("unbind(type)", function() {
367 var $elem = jQuery("#firstp"),
371 ok( false, message );
374 message = "unbind passing function";
375 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
377 message = "unbind all from event";
378 $elem.bind('error', error).unbind('error').triggerHandler('error');
380 message = "unbind all";
381 $elem.bind('error', error).unbind().triggerHandler('error');
383 message = "unbind many with function";
384 $elem.bind('error error2',error)
385 .unbind('error error2', error )
386 .trigger('error').triggerHandler('error2');
388 message = "unbind many"; // #3538
389 $elem.bind('error error2',error)
390 .unbind('error error2')
391 .trigger('error').triggerHandler('error2');
393 message = "unbind without a type or handler";
394 $elem.bind("error error2.test",error)
396 .trigger("error").triggerHandler("error2");
399 test("unbind(eventObject)", function() {
402 var $elem = jQuery("#firstp"),
405 function assert( expected ){
407 $elem.trigger('foo').triggerHandler('bar');
408 equals( num, expected, "Check the right handlers are triggered" );
412 // This handler shouldn't be unbound
413 .bind('foo', function(){
416 .bind('foo', function(e){
421 .bind('bar', function(){
435 test("hover()", function() {
437 handler1 = function( event ) { ++times; },
438 handler2 = function( event ) { ++times; };
441 .hover(handler1, handler2)
442 .mouseenter().mouseleave()
443 .unbind("mouseenter", handler1)
444 .unbind("mouseleave", handler2)
446 .mouseenter().mouseleave()
447 .unbind("mouseenter mouseleave", handler1)
448 .mouseenter().mouseleave();
450 equals( times, 4, "hover handlers fired" );
453 test("trigger() shortcuts", function() {
455 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
456 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
457 equals( close.length, 0, "Context element does not exist, length must be zero" );
458 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
462 jQuery("#check1").click(function() {
463 ok( true, "click event handler for checkbox gets fired twice, see #815" );
467 jQuery('#firstp')[0].onclick = function(event) {
470 jQuery('#firstp').click();
471 equals( counter, 1, "Check that click, triggers onclick event handler also" );
473 var clickCounter = 0;
474 jQuery('#simon1')[0].onclick = function(event) {
477 jQuery('#simon1').click();
478 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
480 jQuery('<img />').load(function(){
481 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
485 test("trigger() bubbling", function() {
488 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
490 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
491 jQuery("html").bind("click", function(e){ html++; });
492 jQuery("body").bind("click", function(e){ body++; });
493 jQuery("#main").bind("click", function(e){ main++; });
494 jQuery("#ap").bind("click", function(){ ap++; return false; });
496 jQuery("html").trigger("click");
497 equals( doc, 1, "HTML bubble" );
498 equals( html, 1, "HTML bubble" );
500 jQuery("body").trigger("click");
501 equals( doc, 2, "Body bubble" );
502 equals( html, 2, "Body bubble" );
503 equals( body, 1, "Body bubble" );
505 jQuery("#main").trigger("click");
506 equals( doc, 3, "Main bubble" );
507 equals( html, 3, "Main bubble" );
508 equals( body, 2, "Main bubble" );
509 equals( main, 1, "Main bubble" );
511 jQuery("#ap").trigger("click");
512 equals( doc, 3, "ap bubble" );
513 equals( html, 3, "ap bubble" );
514 equals( body, 2, "ap bubble" );
515 equals( main, 1, "ap bubble" );
516 equals( ap, 1, "ap bubble" );
519 test("trigger(type, [data], [fn])", function() {
522 var handler = function(event, a, b, c) {
523 equals( event.type, "click", "check passed data" );
524 equals( a, 1, "check passed data" );
525 equals( b, "2", "check passed data" );
526 equals( c, "abc", "check passed data" );
530 var $elem = jQuery("#firstp");
532 // Simulate a "native" click
533 $elem[0].click = function(){
534 ok( true, "Native call was triggered" );
537 // Triggers handlrs and native
539 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
541 // Simulate a "native" click
542 $elem[0].click = function(){
543 ok( false, "Native call was triggered" );
546 // Trigger only the handlers (no native)
548 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
552 jQuery('#form input:first').hide().trigger('focus');
556 ok( pass, "Trigger focus on hidden element" );
560 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
564 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
566 var form = jQuery("<form action=''></form>").appendTo("body");
568 // Make sure it can be prevented locally
569 form.submit(function(){
570 ok( true, "Local bind still works." );
575 form.trigger("submit");
577 form.unbind("submit");
579 jQuery(document).submit(function(){
580 ok( true, "Make sure bubble works up to document." );
585 form.trigger("submit");
587 jQuery(document).unbind("submit");
592 test("jQuery.Event.currentTarget", function(){
595 test("trigger(eventObject, [data], [fn])", function() {
598 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
599 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
601 var event = jQuery.Event("noNew");
602 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
603 equals( event.type, "noNew", "Verify its type" );
605 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
606 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
607 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
609 event.preventDefault();
610 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
611 event.stopPropagation();
612 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
614 event.isPropagationStopped = function(){ return false };
615 event.stopImmediatePropagation();
616 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
617 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
619 $parent.bind('foo',function(e){
621 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
622 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
623 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
624 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
627 // test with an event object
628 event = new jQuery.Event("foo");
629 event.secret = 'boo!';
630 $child.trigger(event);
632 // test with a literal object
633 $child.trigger({type:'foo', secret:'boo!'});
638 ok( false, "This assertion shouldn't be reached");
641 $parent.bind('foo', error );
643 $child.bind('foo',function(e, a, b, c ){
644 equals( arguments.length, 4, "Check arguments length");
645 equals( a, 1, "Check first custom argument");
646 equals( b, 2, "Check second custom argument");
647 equals( c, 3, "Check third custom argument");
649 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
650 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
651 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
654 e.stopImmediatePropagation();
659 // We should add this back in when we want to test the order
660 // in which event handlers are iterated.
661 //$child.bind('foo', error );
663 event = new jQuery.Event("foo");
664 $child.trigger( event, [1,2,3] ).unbind();
665 equals( event.result, "result", "Check event.result attribute");
667 // Will error if it bubbles
668 $child.triggerHandler('foo');
671 $parent.unbind().remove();
674 test("jQuery.Event.currentTarget", function(){
678 $elem = jQuery('<button>a</button>').click(function(e){
679 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
683 $elem.trigger('click');
689 test("toggle(Function, Function, ...)", function() {
693 fn1 = function(e) { count++; },
694 fn2 = function(e) { count--; },
695 preventDefault = function(e) { e.preventDefault() },
696 link = jQuery('#mark');
697 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
698 equals( count, 1, "Check for toggle(fn, fn)" );
700 jQuery("#firstp").toggle(function () {
701 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
702 }, function() {}).trigger("click", [ 1, 2, 3 ]);
705 jQuery("#simon1").one("click", function() {
706 ok( true, "Execute event only once" );
707 jQuery(this).toggle(function() {
708 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
710 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
713 }).click().click().click();
728 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
730 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
732 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
734 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
736 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
738 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
740 $div.unbind('click',fns[0]);
741 var data = jQuery.data( $div[0], 'events' );
742 ok( !data, "Unbinding one function from toggle unbinds them all");
744 // Test Multi-Toggles
746 $div = jQuery("<div/>");
747 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
749 same( a, [1], "Check that a click worked." );
751 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
753 same( a, [1,2], "Check that a click worked with a second toggle." );
754 same( b, [1], "Check that a click worked with a second toggle." );
757 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
758 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
761 test(".live()/.die()", function() {
764 var submit = 0, div = 0, livea = 0, liveb = 0;
766 jQuery("div").live("submit", function(){ submit++; return false; });
767 jQuery("div").live("click", function(){ div++; });
768 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
769 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
771 // Nothing should trigger on the body
772 jQuery("body").trigger("click");
773 equals( submit, 0, "Click on body" );
774 equals( div, 0, "Click on body" );
775 equals( livea, 0, "Click on body" );
776 equals( liveb, 0, "Click on body" );
778 // This should trigger two events
779 submit = 0, div = 0, livea = 0, liveb = 0;
780 jQuery("div#nothiddendiv").trigger("click");
781 equals( submit, 0, "Click on div" );
782 equals( div, 1, "Click on div" );
783 equals( livea, 1, "Click on div" );
784 equals( liveb, 0, "Click on div" );
786 // This should trigger three events (w/ bubbling)
787 submit = 0, div = 0, livea = 0, liveb = 0;
788 jQuery("div#nothiddendivchild").trigger("click");
789 equals( submit, 0, "Click on inner div" );
790 equals( div, 1, "Click on inner div" );
791 equals( livea, 1, "Click on inner div" );
792 equals( liveb, 1, "Click on inner div" );
794 // This should trigger one submit
795 submit = 0, div = 0, livea = 0, liveb = 0;
796 jQuery("div#nothiddendivchild").trigger("submit");
797 equals( submit, 1, "Submit on div" );
798 equals( div, 0, "Submit on div" );
799 equals( livea, 0, "Submit on div" );
800 equals( liveb, 0, "Submit on div" );
802 // Make sure no other events were removed in the process
803 submit = 0, div = 0, livea = 0, liveb = 0;
804 jQuery("div#nothiddendivchild").trigger("click");
805 equals( submit, 0, "die Click on inner div" );
806 equals( div, 1, "die Click on inner div" );
807 equals( livea, 1, "die Click on inner div" );
808 equals( liveb, 1, "die Click on inner div" );
810 // Now make sure that the removal works
811 submit = 0, div = 0, livea = 0, liveb = 0;
812 jQuery("div#nothiddendivchild").die("click");
813 jQuery("div#nothiddendivchild").trigger("click");
814 equals( submit, 0, "die Click on inner div" );
815 equals( div, 1, "die Click on inner div" );
816 equals( livea, 1, "die Click on inner div" );
817 equals( liveb, 0, "die Click on inner div" );
819 // Make sure that the click wasn't removed too early
820 submit = 0, div = 0, livea = 0, liveb = 0;
821 jQuery("div#nothiddendiv").trigger("click");
822 equals( submit, 0, "die Click on inner div" );
823 equals( div, 1, "die Click on inner div" );
824 equals( livea, 1, "die Click on inner div" );
825 equals( liveb, 0, "die Click on inner div" );
827 // Make sure that stopPropgation doesn't stop live events
828 submit = 0, div = 0, livea = 0, liveb = 0;
829 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
830 jQuery("div#nothiddendivchild").trigger("click");
831 equals( submit, 0, "stopPropagation Click on inner div" );
832 equals( div, 1, "stopPropagation Click on inner div" );
833 equals( livea, 1, "stopPropagation Click on inner div" );
834 equals( liveb, 1, "stopPropagation Click on inner div" );
836 // Make sure click events only fire with primary click
837 submit = 0, div = 0, livea = 0, liveb = 0;
838 var event = jQuery.Event("click");
840 jQuery("div#nothiddendiv").trigger(event);
842 equals( livea, 0, "live secondary click" );
844 jQuery("div#nothiddendivchild").die("click");
845 jQuery("div#nothiddendiv").die("click");
846 jQuery("div").die("click");
847 jQuery("div").die("submit");
849 // Test binding with a different context
850 var clicked = 0, container = jQuery('#main')[0];
851 jQuery("#foo", container).live("click", function(e){ clicked++; });
852 jQuery("div").trigger('click');
853 jQuery("#foo").trigger('click');
854 jQuery("#main").trigger('click');
855 jQuery("body").trigger('click');
856 equals( clicked, 2, "live with a context" );
858 // Make sure the event is actually stored on the context
859 ok( jQuery.data(container, "events").live, "live with a context" );
861 // Test unbinding with a different context
862 jQuery("#foo", container).die("click");
863 jQuery("#foo").trigger('click');
864 equals( clicked, 2, "die with a context");
866 // Test binding with event data
867 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
868 jQuery("#foo").trigger("click").die("click");
870 // Test binding with trigger data
871 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
872 jQuery("#foo").trigger("click", true).die("click");
874 // Test binding with different this object
875 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
876 jQuery("#foo").trigger("click").die("click");
878 // Test binding with different this object, event data, and trigger data
879 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
880 equals( e.data, true, "live with with different this object, event data, and trigger data" );
881 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
882 equals( data, true, "live with with different this object, event data, and trigger data")
884 jQuery("#foo").trigger("click", true).die("click");
886 // Verify that return false prevents default action
887 jQuery("#anchor2").live("click", function(){ return false; });
888 var hash = window.location.hash;
889 jQuery("#anchor2").trigger("click");
890 equals( window.location.hash, hash, "return false worked" );
891 jQuery("#anchor2").die("click");
893 // Verify that .preventDefault() prevents default action
894 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
895 var hash = window.location.hash;
896 jQuery("#anchor2").trigger("click");
897 equals( window.location.hash, hash, "e.preventDefault() worked" );
898 jQuery("#anchor2").die("click");
900 // Test binding the same handler to multiple points
902 function callback(){ called++; return false; }
904 jQuery("#nothiddendiv").live("click", callback);
905 jQuery("#anchor2").live("click", callback);
907 jQuery("#nothiddendiv").trigger("click");
908 equals( called, 1, "Verify that only one click occurred." );
910 jQuery("#anchor2").trigger("click");
911 equals( called, 2, "Verify that only one click occurred." );
913 // Make sure that only one callback is removed
914 jQuery("#anchor2").die("click", callback);
916 jQuery("#nothiddendiv").trigger("click");
917 equals( called, 3, "Verify that only one click occurred." );
919 jQuery("#anchor2").trigger("click");
920 equals( called, 3, "Verify that no click occurred." );
922 // Make sure that it still works if the selector is the same,
923 // but the event type is different
924 jQuery("#nothiddendiv").live("foo", callback);
927 jQuery("#nothiddendiv").die("click", callback);
929 jQuery("#nothiddendiv").trigger("click");
930 equals( called, 3, "Verify that no click occurred." );
932 jQuery("#nothiddendiv").trigger("foo");
933 equals( called, 4, "Verify that one foo occurred." );
936 jQuery("#nothiddendiv").die("foo", callback);
938 // Make sure we don't loose the target by DOM modifications
939 // after the bubble already reached the liveHandler
940 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
942 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
943 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
945 jQuery("#nothiddendiv span").click();
946 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
947 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
950 jQuery("#nothiddendivchild").die("click");
952 // Verify that .live() ocurs and cancel buble in the same order as
953 // we would expect .bind() and .click() without delegation
954 var lived = 0, livee = 0;
956 // bind one pair in one order
957 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
958 jQuery('span#liveSpan1').live('click', function(){ livee++; });
960 jQuery('span#liveSpan1 a').click();
961 equals( lived, 1, "Verify that only one first handler occurred." );
962 equals( livee, 0, "Verify that second handler doesn't." );
964 // and one pair in inverse
965 jQuery('span#liveSpan2').live('click', function(){ livee++; });
966 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
970 jQuery('span#liveSpan2 a').click();
971 equals( lived, 1, "Verify that only one first handler occurred." );
972 equals( livee, 0, "Verify that second handler doesn't." );
975 jQuery("span#liveSpan1 a").die("click")
976 jQuery("span#liveSpan1").die("click");
977 jQuery("span#liveSpan2 a").die("click");
978 jQuery("span#liveSpan2").die("click");
980 // Test this, target and currentTarget are correct
981 jQuery('span#liveSpan1').live('click', function(e){
982 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
983 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
984 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
987 jQuery('span#liveSpan1 a').click();
989 jQuery('span#liveSpan1').die('click');
991 // Work with deep selectors
994 function clickB(){ livee++; }
996 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
997 jQuery("#nothiddendiv div").live("click", clickB);
998 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1000 equals( livee, 0, "No clicks, deep selector." );
1003 jQuery("#nothiddendivchild").trigger("click");
1004 equals( livee, 2, "Click, deep selector." );
1007 jQuery("#nothiddendivchild").trigger("mouseover");
1008 equals( livee, 1, "Mouseover, deep selector." );
1010 jQuery("#nothiddendiv div").die("mouseover");
1013 jQuery("#nothiddendivchild").trigger("click");
1014 equals( livee, 2, "Click, deep selector." );
1017 jQuery("#nothiddendivchild").trigger("mouseover");
1018 equals( livee, 0, "Mouseover, deep selector." );
1020 jQuery("#nothiddendiv div").die("click", clickB);
1023 jQuery("#nothiddendivchild").trigger("click");
1024 equals( livee, 1, "Click, deep selector." );
1026 jQuery("#nothiddendiv div").die("click");
1029 test("die all bound events", function(){
1033 var div = jQuery("div#nothiddendivchild");
1035 div.live("click submit", function(){ count++; });
1038 div.trigger("click");
1039 div.trigger("submit");
1041 equals( count, 0, "Make sure no events were triggered." );
1044 test("live with multiple events", function(){
1048 var div = jQuery("div#nothiddendivchild");
1050 div.live("click submit", function(){ count++; });
1052 div.trigger("click");
1053 div.trigger("submit");
1055 equals( count, 2, "Make sure both the click and submit were triggered." );
1058 test("live with namespaces", function(){
1061 var count1 = 0, count2 = 0;
1063 jQuery("#liveSpan1").live("foo.bar", function(){
1067 jQuery("#liveSpan2").live("foo.zed", function(){
1071 jQuery("#liveSpan1").trigger("foo.bar");
1072 equals( count1, 1, "Got live foo.bar" );
1074 jQuery("#liveSpan2").trigger("foo.zed");
1075 equals( count2, 1, "Got live foo.zed" );
1078 jQuery("#liveSpan2").die("foo.zed");
1079 jQuery("#liveSpan1").trigger("foo.bar");
1081 equals( count1, 2, "Got live foo.bar after dieing foo.zed" );
1083 jQuery("#liveSpan2").trigger("foo.zed");
1084 equals( count2, 1, "Got live foo.zed" );
1087 jQuery("#liveSpan1").die("foo.bar");
1089 jQuery("#liveSpan1").trigger("foo.bar");
1090 equals( count1, 2, "Did not respond to foo.bar after dieing it" );
1092 jQuery("#liveSpan2").trigger("foo.zed");
1093 equals( count2, 1, "Did not trigger foo.zed again" );
1096 test("live with change", function(){
1097 var selectChange = 0, checkboxChange = 0;
1099 var select = jQuery("select[name='S1']")
1100 select.live("change", function() {
1104 var checkbox = jQuery("#check2"),
1105 checkboxFunction = function(){
1108 checkbox.live("change", checkboxFunction);
1110 // test click on select
1112 // second click that changed it
1114 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1115 select.trigger("change");
1116 equals( selectChange, 1, "Change on click." );
1118 // test keys on select
1120 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1121 select.trigger("change");
1122 equals( selectChange, 1, "Change on keyup." );
1124 // test click on checkbox
1125 checkbox.trigger("change");
1126 equals( checkboxChange, 1, "Change on checkbox." );
1128 // test before activate on radio
1130 // test blur/focus on textarea
1131 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1132 textarea.live("change", function() {
1136 textarea.val(oldVal + "foo");
1137 textarea.trigger("change");
1138 equals( textareaChange, 1, "Change on textarea." );
1140 textarea.val(oldVal);
1141 textarea.die("change");
1143 // test blur/focus on text
1144 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1145 text.live("change", function() {
1149 text.val(oldVal+"foo");
1150 text.trigger("change");
1151 equals( textChange, 1, "Change on text input." );
1153 text.val(oldTextVal);
1156 // test blur/focus on password
1157 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1158 password.live("change", function() {
1162 password.val(oldPasswordVal + "foo");
1163 password.trigger("change");
1164 equals( passwordChange, 1, "Change on password input." );
1166 password.val(oldPasswordVal);
1167 password.die("change");
1169 // make sure die works
1173 select.die("change");
1174 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1175 select.trigger("change");
1176 equals( selectChange, 0, "Die on click works." );
1179 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1180 select.trigger("change");
1181 equals( selectChange, 0, "Die on keyup works." );
1183 // die specific checkbox
1184 checkbox.die("change", checkboxFunction);
1185 checkbox.trigger("change");
1186 equals( checkboxChange, 1, "Die on checkbox." );
1189 test("live with submit", function() {
1190 var count1 = 0, count2 = 0;
1192 jQuery("#testForm").live("submit", function(ev) {
1194 ev.preventDefault();
1197 jQuery("body").live("submit", function(ev) {
1199 ev.preventDefault();
1202 if ( jQuery.support.submitBubbles ) {
1203 jQuery("#testForm input[name=sub1]")[0].click();
1207 jQuery("#testForm input[name=sub1]")[0].click();
1208 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1213 jQuery("#testForm").die("submit");
1214 jQuery("body").die("submit");
1217 test(".delegate()/.undelegate()", function() {
1220 var submit = 0, div = 0, livea = 0, liveb = 0;
1222 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1223 jQuery("#body").delegate("div", "click", function(){ div++; });
1224 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1225 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1227 // Nothing should trigger on the body
1228 jQuery("body").trigger("click");
1229 equals( submit, 0, "Click on body" );
1230 equals( div, 0, "Click on body" );
1231 equals( livea, 0, "Click on body" );
1232 equals( liveb, 0, "Click on body" );
1234 // This should trigger two events
1235 jQuery("div#nothiddendiv").trigger("click");
1236 equals( submit, 0, "Click on div" );
1237 equals( div, 1, "Click on div" );
1238 equals( livea, 1, "Click on div" );
1239 equals( liveb, 0, "Click on div" );
1241 // This should trigger three events (w/ bubbling)
1242 jQuery("div#nothiddendivchild").trigger("click");
1243 equals( submit, 0, "Click on inner div" );
1244 equals( div, 2, "Click on inner div" );
1245 equals( livea, 2, "Click on inner div" );
1246 equals( liveb, 1, "Click on inner div" );
1248 // This should trigger one submit
1249 jQuery("div#nothiddendivchild").trigger("submit");
1250 equals( submit, 1, "Submit on div" );
1251 equals( div, 2, "Submit on div" );
1252 equals( livea, 2, "Submit on div" );
1253 equals( liveb, 1, "Submit on div" );
1255 // Make sure no other events were removed in the process
1256 jQuery("div#nothiddendivchild").trigger("click");
1257 equals( submit, 1, "undelegate Click on inner div" );
1258 equals( div, 3, "undelegate Click on inner div" );
1259 equals( livea, 3, "undelegate Click on inner div" );
1260 equals( liveb, 2, "undelegate Click on inner div" );
1262 // Now make sure that the removal works
1263 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1264 jQuery("div#nothiddendivchild").trigger("click");
1265 equals( submit, 1, "undelegate Click on inner div" );
1266 equals( div, 4, "undelegate Click on inner div" );
1267 equals( livea, 4, "undelegate Click on inner div" );
1268 equals( liveb, 2, "undelegate Click on inner div" );
1270 // Make sure that the click wasn't removed too early
1271 jQuery("div#nothiddendiv").trigger("click");
1272 equals( submit, 1, "undelegate Click on inner div" );
1273 equals( div, 5, "undelegate Click on inner div" );
1274 equals( livea, 5, "undelegate Click on inner div" );
1275 equals( liveb, 2, "undelegate Click on inner div" );
1277 // Make sure that stopPropgation doesn't stop live events
1278 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1279 jQuery("div#nothiddendivchild").trigger("click");
1280 equals( submit, 1, "stopPropagation Click on inner div" );
1281 equals( div, 6, "stopPropagation Click on inner div" );
1282 equals( livea, 6, "stopPropagation Click on inner div" );
1283 equals( liveb, 3, "stopPropagation Click on inner div" );
1285 // Make sure click events only fire with primary click
1286 var event = jQuery.Event("click");
1288 jQuery("div#nothiddendiv").trigger(event);
1290 equals( livea, 6, "delegate secondary click" );
1292 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1293 jQuery("#body").undelegate("div#nothiddendiv", "click");
1294 jQuery("#body").undelegate("div", "click");
1295 jQuery("#body").undelegate("div", "submit");
1297 // Test binding with a different context
1298 var clicked = 0, container = jQuery('#main')[0];
1299 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1300 jQuery("div").trigger('click');
1301 jQuery("#foo").trigger('click');
1302 jQuery("#main").trigger('click');
1303 jQuery("body").trigger('click');
1304 equals( clicked, 2, "delegate with a context" );
1306 // Make sure the event is actually stored on the context
1307 ok( jQuery.data(container, "events").live, "delegate with a context" );
1309 // Test unbinding with a different context
1310 jQuery("#main").undelegate("#foo", "click");
1311 jQuery("#foo").trigger('click');
1312 equals( clicked, 2, "undelegate with a context");
1314 // Test binding with event data
1315 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1316 jQuery("#foo").trigger("click");
1317 jQuery("#body").undelegate("#foo", "click");
1319 // Test binding with trigger data
1320 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1321 jQuery("#foo").trigger("click", true);
1322 jQuery("#body").undelegate("#foo", "click");
1324 // Test binding with different this object
1325 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1326 jQuery("#foo").trigger("click");
1327 jQuery("#body").undelegate("#foo", "click");
1329 // Test binding with different this object, event data, and trigger data
1330 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1331 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1332 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1333 equals( data, true, "delegate with with different this object, event data, and trigger data")
1334 }, { foo: "bar" }));
1335 jQuery("#foo").trigger("click", true);
1336 jQuery("#body").undelegate("#foo", "click");
1338 // Verify that return false prevents default action
1339 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1340 var hash = window.location.hash;
1341 jQuery("#anchor2").trigger("click");
1342 equals( window.location.hash, hash, "return false worked" );
1343 jQuery("#body").undelegate("#anchor2", "click");
1345 // Verify that .preventDefault() prevents default action
1346 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1347 var hash = window.location.hash;
1348 jQuery("#anchor2").trigger("click");
1349 equals( window.location.hash, hash, "e.preventDefault() worked" );
1350 jQuery("#body").undelegate("#anchor2", "click");
1352 // Test binding the same handler to multiple points
1354 function callback(){ called++; return false; }
1356 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1357 jQuery("#body").delegate("#anchor2", "click", callback);
1359 jQuery("#nothiddendiv").trigger("click");
1360 equals( called, 1, "Verify that only one click occurred." );
1362 jQuery("#anchor2").trigger("click");
1363 equals( called, 2, "Verify that only one click occurred." );
1365 // Make sure that only one callback is removed
1366 jQuery("#body").undelegate("#anchor2", "click", callback);
1368 jQuery("#nothiddendiv").trigger("click");
1369 equals( called, 3, "Verify that only one click occurred." );
1371 jQuery("#anchor2").trigger("click");
1372 equals( called, 3, "Verify that no click occurred." );
1374 // Make sure that it still works if the selector is the same,
1375 // but the event type is different
1376 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1379 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1381 jQuery("#nothiddendiv").trigger("click");
1382 equals( called, 3, "Verify that no click occurred." );
1384 jQuery("#nothiddendiv").trigger("foo");
1385 equals( called, 4, "Verify that one foo occurred." );
1388 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1390 // Make sure we don't loose the target by DOM modifications
1391 // after the bubble already reached the liveHandler
1392 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1394 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1395 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1397 jQuery("#nothiddendiv span").click();
1398 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1399 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1402 jQuery("#body").undelegate("#nothiddendivchild", "click");
1404 // Verify that .live() ocurs and cancel buble in the same order as
1405 // we would expect .bind() and .click() without delegation
1406 var lived = 0, livee = 0;
1408 // bind one pair in one order
1409 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1410 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1412 jQuery('span#liveSpan1 a').click();
1413 equals( lived, 1, "Verify that only one first handler occurred." );
1414 equals( livee, 0, "Verify that second handler doesn't." );
1416 // and one pair in inverse
1417 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1418 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1422 jQuery('span#liveSpan2 a').click();
1423 equals( lived, 1, "Verify that only one first handler occurred." );
1424 equals( livee, 0, "Verify that second handler doesn't." );
1427 jQuery("#body").undelegate("click");
1429 // Test this, target and currentTarget are correct
1430 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1431 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1432 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1433 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1436 jQuery('span#liveSpan1 a').click();
1438 jQuery("#body").undelegate('span#liveSpan1', 'click');
1440 // Work with deep selectors
1443 function clickB(){ livee++; }
1445 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1446 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1447 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1449 equals( livee, 0, "No clicks, deep selector." );
1452 jQuery("#nothiddendivchild").trigger("click");
1453 equals( livee, 2, "Click, deep selector." );
1456 jQuery("#nothiddendivchild").trigger("mouseover");
1457 equals( livee, 1, "Mouseover, deep selector." );
1459 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1462 jQuery("#nothiddendivchild").trigger("click");
1463 equals( livee, 2, "Click, deep selector." );
1466 jQuery("#nothiddendivchild").trigger("mouseover");
1467 equals( livee, 0, "Mouseover, deep selector." );
1469 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1472 jQuery("#nothiddendivchild").trigger("click");
1473 equals( livee, 1, "Click, deep selector." );
1475 jQuery("#body").undelegate("#nothiddendiv div", "click");
1478 test("undelegate all bound events", function(){
1482 var div = jQuery("#body");
1484 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1487 jQuery("div#nothiddendivchild").trigger("click");
1488 jQuery("div#nothiddendivchild").trigger("submit");
1490 equals( count, 0, "Make sure no events were triggered." );
1493 test("delegate with multiple events", function(){
1497 var div = jQuery("#body");
1499 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1501 jQuery("div#nothiddendivchild").trigger("click");
1502 jQuery("div#nothiddendivchild").trigger("submit");
1504 equals( count, 2, "Make sure both the click and submit were triggered." );
1506 jQuery("#body").undelegate();
1509 test("delegate with change", function(){
1510 var selectChange = 0, checkboxChange = 0;
1512 var select = jQuery("select[name='S1']");
1513 jQuery("#body").delegate("select[name='S1']", "change", function() {
1517 var checkbox = jQuery("#check2"),
1518 checkboxFunction = function(){
1521 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1523 // test click on select
1525 // second click that changed it
1527 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1528 select.trigger("change");
1529 equals( selectChange, 1, "Change on click." );
1531 // test keys on select
1533 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1534 select.trigger("change");
1535 equals( selectChange, 1, "Change on keyup." );
1537 // test click on checkbox
1538 checkbox.trigger("change");
1539 equals( checkboxChange, 1, "Change on checkbox." );
1541 // test before activate on radio
1543 // test blur/focus on textarea
1544 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1545 jQuery("#body").delegate("#area1", "change", function() {
1549 textarea.val(oldVal + "foo");
1550 textarea.trigger("change");
1551 equals( textareaChange, 1, "Change on textarea." );
1553 textarea.val(oldVal);
1554 jQuery("#body").undelegate("#area1", "change");
1556 // test blur/focus on text
1557 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1558 jQuery("#body").delegate("#name", "change", function() {
1562 text.val(oldVal+"foo");
1563 text.trigger("change");
1564 equals( textChange, 1, "Change on text input." );
1566 text.val(oldTextVal);
1567 jQuery("#body").die("change");
1569 // test blur/focus on password
1570 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1571 jQuery("#body").delegate("#name", "change", function() {
1575 password.val(oldPasswordVal + "foo");
1576 password.trigger("change");
1577 equals( passwordChange, 1, "Change on password input." );
1579 password.val(oldPasswordVal);
1580 jQuery("#body").undelegate("#name", "change");
1582 // make sure die works
1586 jQuery("#body").undelegate("select[name='S1']", "change");
1587 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1588 select.trigger("change");
1589 equals( selectChange, 0, "Die on click works." );
1592 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1593 select.trigger("change");
1594 equals( selectChange, 0, "Die on keyup works." );
1596 // die specific checkbox
1597 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1598 checkbox.trigger("change");
1599 equals( checkboxChange, 1, "Die on checkbox." );
1602 test("delegate with submit", function() {
1603 var count1 = 0, count2 = 0;
1605 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1607 ev.preventDefault();
1610 jQuery(document).delegate("body", "submit", function(ev) {
1612 ev.preventDefault();
1615 if ( jQuery.support.submitBubbles ) {
1616 jQuery("#testForm input[name=sub1]")[0].click();
1620 jQuery("#testForm input[name=sub1]")[0].click();
1621 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1626 jQuery("#body").undelegate();
1627 jQuery(document).undelegate();
1630 test("Non DOM element events", function() {
1634 .bind('nonelementglobal', function(e) {
1635 ok( true, "Global event on non-DOM annonymos object triggered" );
1641 .bind('nonelementobj', function(e) {
1642 ok( true, "Event on non-DOM object triggered" );
1643 }).bind('nonelementglobal', function() {
1644 ok( true, "Global event on non-DOM object triggered" );
1647 jQuery(o).trigger('nonelementobj');
1648 jQuery.event.trigger('nonelementglobal');
1652 test("jQuery(function($) {})", function() {
1654 jQuery(function($) {
1655 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");
1660 test("event properties", function() {
1662 jQuery("#simon1").click(function(event) {
1663 ok( event.timeStamp, "assert event.timeStamp is present" );