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." );
90 teardown: function(){},
91 add: function( handleObj ) {
92 var handler = handleObj.handler;
93 handleObj.handler = function(e) {
95 handler.apply( this, arguments );
101 div.bind("test.a", {x: 1}, function(e) {
102 ok( !!e.xyz, "Make sure that the data is getting passed through." );
103 equals( e.data.x, 1, "Make sure data is attached properly." );
106 div.bind("test.b", {x: 2}, function(e) {
107 ok( !!e.xyz, "Make sure that the data is getting passed through." );
108 equals( e.data.x, 2, "Make sure data is attached properly." );
115 div.trigger("test.a");
118 div.trigger("test.b");
121 test("bind(), no data", function() {
123 var handler = function(event) {
124 ok ( !event.data, "Check that no data is added to the event object" );
126 jQuery("#firstp").bind("click", handler).trigger("click");
129 test("bind/one/unbind(Object)", function(){
132 var clickCounter = 0, mouseoverCounter = 0;
133 function handler(event) {
134 if (event.type == "click")
136 else if (event.type == "mouseover")
140 function handlerWithData(event) {
141 if (event.type == "click")
142 clickCounter += event.data;
143 else if (event.type == "mouseover")
144 mouseoverCounter += event.data;
148 $elem.trigger("click").trigger("mouseover");
151 var $elem = jQuery("#firstp")
159 click:handlerWithData,
160 mouseover:handlerWithData
165 equals( clickCounter, 3, "bind(Object)" );
166 equals( mouseoverCounter, 3, "bind(Object)" );
169 equals( clickCounter, 4, "bind(Object)" );
170 equals( mouseoverCounter, 4, "bind(Object)" );
172 jQuery("#firstp").unbind({
178 equals( clickCounter, 4, "bind(Object)" );
179 equals( mouseoverCounter, 4, "bind(Object)" );
182 test("bind(), iframes", function() {
183 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
184 var doc = jQuery("#loadediframe").contents();
186 jQuery("div", doc).bind("click", function() {
187 ok( true, "Binding to element inside iframe" );
188 }).click().unbind('click');
191 test("bind(), trigger change on select", function() {
194 function selectOnChange(event) {
195 equals( event.data, counter++, "Event.data is not a global event object" );
197 jQuery("#form select").each(function(i){
198 jQuery(this).bind('change', i, selectOnChange);
199 }).trigger('change');
202 test("bind(), namespaced events, cloned events", function() {
205 jQuery("#firstp").bind("custom.test",function(e){
206 ok(true, "Custom event triggered");
209 jQuery("#firstp").bind("click",function(e){
210 ok(true, "Normal click triggered");
213 jQuery("#firstp").bind("click.test",function(e){
214 ok(true, "Namespaced click triggered");
217 // Trigger both bound fn (2)
218 jQuery("#firstp").trigger("click");
220 // Trigger one bound fn (1)
221 jQuery("#firstp").trigger("click.test");
223 // Remove only the one fn
224 jQuery("#firstp").unbind("click.test");
226 // Trigger the remaining fn (1)
227 jQuery("#firstp").trigger("click");
229 // Remove the remaining fn
230 jQuery("#firstp").unbind(".test");
232 // Trigger the remaining fn (0)
233 jQuery("#firstp").trigger("custom");
235 // using contents will get comments regular, text, and comment nodes
236 jQuery("#nonnodes").contents().bind("tester", function () {
237 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
238 }).trigger("tester");
240 // Make sure events stick with appendTo'd elements (which are cloned) #2027
241 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
242 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
245 test("bind(), multi-namespaced events", function() {
257 function check(name, msg){
258 same(name, order.shift(), msg);
261 jQuery("#firstp").bind("custom.test",function(e){
262 check("custom.test", "Custom event triggered");
265 jQuery("#firstp").bind("custom.test2",function(e){
266 check("custom.test2", "Custom event triggered");
269 jQuery("#firstp").bind("click.test",function(e){
270 check("click.test", "Normal click triggered");
273 jQuery("#firstp").bind("click.test.abc",function(e){
274 check("click.test.abc", "Namespaced click triggered");
277 // Those would not trigger/unbind (#5303)
278 jQuery("#firstp").trigger("click.a.test");
279 jQuery("#firstp").unbind("click.a.test");
281 // Trigger both bound fn (1)
282 jQuery("#firstp").trigger("click.test.abc");
284 // Trigger one bound fn (1)
285 jQuery("#firstp").trigger("click.abc");
287 // Trigger two bound fn (2)
288 jQuery("#firstp").trigger("click.test");
290 // Remove only the one fn
291 jQuery("#firstp").unbind("click.abc");
293 // Trigger the remaining fn (1)
294 jQuery("#firstp").trigger("click");
296 // Remove the remaining fn
297 jQuery("#firstp").unbind(".test");
299 // Trigger the remaining fn (1)
300 jQuery("#firstp").trigger("custom");
303 test("bind(), with same function", function() {
306 var count = 0 , func = function(){
310 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
311 jQuery("#liveHandlerOrder").trigger("foo.bar");
313 equals(count, 1, "Verify binding function with multiple namespaces." );
315 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
316 jQuery("#liveHandlerOrder").trigger("foo.bar");
318 equals(count, 1, "Verify that removing events still work." );
321 test("bind(), make sure order is maintained", function() {
324 var elem = jQuery("#firstp"), log = [], check = [];
326 for ( var i = 0; i < 100; i++ ) (function(i){
327 elem.bind( "click", function(){
334 elem.trigger("click");
336 equals( log.join(","), check.join(","), "Make sure order was maintained." );
338 elem.unbind("click");
341 test("bind(), with different this object", function() {
343 var thisObject = { myThis: true },
344 data = { myData: true },
345 handler1 = function( event ) {
346 equals( this, thisObject, "bind() with different this object" );
348 handler2 = function( event ) {
349 equals( this, thisObject, "bind() with different this object and data" );
350 equals( event.data, data, "bind() with different this object and data" );
354 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
355 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
357 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
360 test("unbind(type)", function() {
363 var $elem = jQuery("#firstp"),
367 ok( false, message );
370 message = "unbind passing function";
371 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
373 message = "unbind all from event";
374 $elem.bind('error', error).unbind('error').triggerHandler('error');
376 message = "unbind all";
377 $elem.bind('error', error).unbind().triggerHandler('error');
379 message = "unbind many with function";
380 $elem.bind('error error2',error)
381 .unbind('error error2', error )
382 .trigger('error').triggerHandler('error2');
384 message = "unbind many"; // #3538
385 $elem.bind('error error2',error)
386 .unbind('error error2')
387 .trigger('error').triggerHandler('error2');
389 message = "unbind without a type or handler";
390 $elem.bind("error error2.test",error)
392 .trigger("error").triggerHandler("error2");
395 test("unbind(eventObject)", function() {
398 var $elem = jQuery("#firstp"),
401 function assert( expected ){
403 $elem.trigger('foo').triggerHandler('bar');
404 equals( num, expected, "Check the right handlers are triggered" );
408 // This handler shouldn't be unbound
409 .bind('foo', function(){
412 .bind('foo', function(e){
417 .bind('bar', function(){
431 test("hover()", function() {
433 handler1 = function( event ) { ++times; },
434 handler2 = function( event ) { ++times; };
437 .hover(handler1, handler2)
438 .mouseenter().mouseleave()
439 .unbind("mouseenter", handler1)
440 .unbind("mouseleave", handler2)
442 .mouseenter().mouseleave()
443 .unbind("mouseenter mouseleave", handler1)
444 .mouseenter().mouseleave();
446 equals( times, 4, "hover handlers fired" );
449 test("trigger() shortcuts", function() {
451 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
452 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
453 equals( close.length, 0, "Context element does not exist, length must be zero" );
454 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
458 jQuery("#check1").click(function() {
459 ok( true, "click event handler for checkbox gets fired twice, see #815" );
463 jQuery('#firstp')[0].onclick = function(event) {
466 jQuery('#firstp').click();
467 equals( counter, 1, "Check that click, triggers onclick event handler also" );
469 var clickCounter = 0;
470 jQuery('#simon1')[0].onclick = function(event) {
473 jQuery('#simon1').click();
474 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
476 jQuery('<img />').load(function(){
477 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
481 test("trigger() bubbling", function() {
484 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
486 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
487 jQuery("html").bind("click", function(e){ html++; });
488 jQuery("body").bind("click", function(e){ body++; });
489 jQuery("#main").bind("click", function(e){ main++; });
490 jQuery("#ap").bind("click", function(){ ap++; return false; });
492 jQuery("html").trigger("click");
493 equals( doc, 1, "HTML bubble" );
494 equals( html, 1, "HTML bubble" );
496 jQuery("body").trigger("click");
497 equals( doc, 2, "Body bubble" );
498 equals( html, 2, "Body bubble" );
499 equals( body, 1, "Body bubble" );
501 jQuery("#main").trigger("click");
502 equals( doc, 3, "Main bubble" );
503 equals( html, 3, "Main bubble" );
504 equals( body, 2, "Main bubble" );
505 equals( main, 1, "Main bubble" );
507 jQuery("#ap").trigger("click");
508 equals( doc, 3, "ap bubble" );
509 equals( html, 3, "ap bubble" );
510 equals( body, 2, "ap bubble" );
511 equals( main, 1, "ap bubble" );
512 equals( ap, 1, "ap bubble" );
515 test("trigger(type, [data], [fn])", function() {
518 var handler = function(event, a, b, c) {
519 equals( event.type, "click", "check passed data" );
520 equals( a, 1, "check passed data" );
521 equals( b, "2", "check passed data" );
522 equals( c, "abc", "check passed data" );
526 var $elem = jQuery("#firstp");
528 // Simulate a "native" click
529 $elem[0].click = function(){
530 ok( true, "Native call was triggered" );
533 // Triggers handlrs and native
535 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
537 // Simulate a "native" click
538 $elem[0].click = function(){
539 ok( false, "Native call was triggered" );
542 // Trigger only the handlers (no native)
544 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
548 jQuery('#form input:first').hide().trigger('focus');
552 ok( pass, "Trigger focus on hidden element" );
556 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
560 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
562 var form = jQuery("<form action=''></form>").appendTo("body");
564 // Make sure it can be prevented locally
565 form.submit(function(){
566 ok( true, "Local bind still works." );
571 form.trigger("submit");
573 form.unbind("submit");
575 jQuery(document).submit(function(){
576 ok( true, "Make sure bubble works up to document." );
581 form.trigger("submit");
583 jQuery(document).unbind("submit");
588 test("jQuery.Event.currentTarget", function(){
591 test("trigger(eventObject, [data], [fn])", function() {
594 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
595 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
597 var event = jQuery.Event("noNew");
598 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
599 equals( event.type, "noNew", "Verify its type" );
601 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
602 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
603 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
605 event.preventDefault();
606 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
607 event.stopPropagation();
608 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
610 event.isPropagationStopped = function(){ return false };
611 event.stopImmediatePropagation();
612 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
613 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
615 $parent.bind('foo',function(e){
617 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
618 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
619 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
620 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
623 // test with an event object
624 event = new jQuery.Event("foo");
625 event.secret = 'boo!';
626 $child.trigger(event);
628 // test with a literal object
629 $child.trigger({type:'foo', secret:'boo!'});
634 ok( false, "This assertion shouldn't be reached");
637 $parent.bind('foo', error );
639 $child.bind('foo',function(e, a, b, c ){
640 equals( arguments.length, 4, "Check arguments length");
641 equals( a, 1, "Check first custom argument");
642 equals( b, 2, "Check second custom argument");
643 equals( c, 3, "Check third custom argument");
645 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
646 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
647 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
650 e.stopImmediatePropagation();
655 // We should add this back in when we want to test the order
656 // in which event handlers are iterated.
657 //$child.bind('foo', error );
659 event = new jQuery.Event("foo");
660 $child.trigger( event, [1,2,3] ).unbind();
661 equals( event.result, "result", "Check event.result attribute");
663 // Will error if it bubbles
664 $child.triggerHandler('foo');
667 $parent.unbind().remove();
670 test("jQuery.Event.currentTarget", function(){
674 $elem = jQuery('<button>a</button>').click(function(e){
675 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
679 $elem.trigger('click');
685 test("toggle(Function, Function, ...)", function() {
689 fn1 = function(e) { count++; },
690 fn2 = function(e) { count--; },
691 preventDefault = function(e) { e.preventDefault() },
692 link = jQuery('#mark');
693 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
694 equals( count, 1, "Check for toggle(fn, fn)" );
696 jQuery("#firstp").toggle(function () {
697 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
698 }, function() {}).trigger("click", [ 1, 2, 3 ]);
701 jQuery("#simon1").one("click", function() {
702 ok( true, "Execute event only once" );
703 jQuery(this).toggle(function() {
704 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
706 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
709 }).click().click().click();
724 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
726 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
728 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
730 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
732 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
734 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
736 $div.unbind('click',fns[0]);
737 var data = jQuery.data( $div[0], 'events' );
738 ok( !data, "Unbinding one function from toggle unbinds them all");
740 // Test Multi-Toggles
742 $div = jQuery("<div/>");
743 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
745 same( a, [1], "Check that a click worked." );
747 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
749 same( a, [1,2], "Check that a click worked with a second toggle." );
750 same( b, [1], "Check that a click worked with a second toggle." );
753 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
754 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
757 test(".live()/.die()", function() {
760 var submit = 0, div = 0, livea = 0, liveb = 0;
762 jQuery("div").live("submit", function(){ submit++; return false; });
763 jQuery("div").live("click", function(){ div++; });
764 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
765 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
767 // Nothing should trigger on the body
768 jQuery("body").trigger("click");
769 equals( submit, 0, "Click on body" );
770 equals( div, 0, "Click on body" );
771 equals( livea, 0, "Click on body" );
772 equals( liveb, 0, "Click on body" );
774 // This should trigger two events
775 submit = 0, div = 0, livea = 0, liveb = 0;
776 jQuery("div#nothiddendiv").trigger("click");
777 equals( submit, 0, "Click on div" );
778 equals( div, 1, "Click on div" );
779 equals( livea, 1, "Click on div" );
780 equals( liveb, 0, "Click on div" );
782 // This should trigger three events (w/ bubbling)
783 submit = 0, div = 0, livea = 0, liveb = 0;
784 jQuery("div#nothiddendivchild").trigger("click");
785 equals( submit, 0, "Click on inner div" );
786 equals( div, 1, "Click on inner div" );
787 equals( livea, 1, "Click on inner div" );
788 equals( liveb, 1, "Click on inner div" );
790 // This should trigger one submit
791 submit = 0, div = 0, livea = 0, liveb = 0;
792 jQuery("div#nothiddendivchild").trigger("submit");
793 equals( submit, 1, "Submit on div" );
794 equals( div, 0, "Submit on div" );
795 equals( livea, 0, "Submit on div" );
796 equals( liveb, 0, "Submit on div" );
798 // Make sure no other events were removed in the process
799 submit = 0, div = 0, livea = 0, liveb = 0;
800 jQuery("div#nothiddendivchild").trigger("click");
801 equals( submit, 0, "die Click on inner div" );
802 equals( div, 1, "die Click on inner div" );
803 equals( livea, 1, "die Click on inner div" );
804 equals( liveb, 1, "die Click on inner div" );
806 // Now make sure that the removal works
807 submit = 0, div = 0, livea = 0, liveb = 0;
808 jQuery("div#nothiddendivchild").die("click");
809 jQuery("div#nothiddendivchild").trigger("click");
810 equals( submit, 0, "die Click on inner div" );
811 equals( div, 1, "die Click on inner div" );
812 equals( livea, 1, "die Click on inner div" );
813 equals( liveb, 0, "die Click on inner div" );
815 // Make sure that the click wasn't removed too early
816 submit = 0, div = 0, livea = 0, liveb = 0;
817 jQuery("div#nothiddendiv").trigger("click");
818 equals( submit, 0, "die Click on inner div" );
819 equals( div, 1, "die Click on inner div" );
820 equals( livea, 1, "die Click on inner div" );
821 equals( liveb, 0, "die Click on inner div" );
823 // Make sure that stopPropgation doesn't stop live events
824 submit = 0, div = 0, livea = 0, liveb = 0;
825 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
826 jQuery("div#nothiddendivchild").trigger("click");
827 equals( submit, 0, "stopPropagation Click on inner div" );
828 equals( div, 1, "stopPropagation Click on inner div" );
829 equals( livea, 1, "stopPropagation Click on inner div" );
830 equals( liveb, 1, "stopPropagation Click on inner div" );
832 // Make sure click events only fire with primary click
833 submit = 0, div = 0, livea = 0, liveb = 0;
834 var event = jQuery.Event("click");
836 jQuery("div#nothiddendiv").trigger(event);
838 equals( livea, 0, "live secondary click" );
840 jQuery("div#nothiddendivchild").die("click");
841 jQuery("div#nothiddendiv").die("click");
842 jQuery("div").die("click");
843 jQuery("div").die("submit");
845 // Test binding with a different context
846 var clicked = 0, container = jQuery('#main')[0];
847 jQuery("#foo", container).live("click", function(e){ clicked++; });
848 jQuery("div").trigger('click');
849 jQuery("#foo").trigger('click');
850 jQuery("#main").trigger('click');
851 jQuery("body").trigger('click');
852 equals( clicked, 2, "live with a context" );
854 // Make sure the event is actually stored on the context
855 ok( jQuery.data(container, "events").live, "live with a context" );
857 // Test unbinding with a different context
858 jQuery("#foo", container).die("click");
859 jQuery("#foo").trigger('click');
860 equals( clicked, 2, "die with a context");
862 // Test binding with event data
863 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
864 jQuery("#foo").trigger("click").die("click");
866 // Test binding with trigger data
867 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
868 jQuery("#foo").trigger("click", true).die("click");
870 // Test binding with different this object
871 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
872 jQuery("#foo").trigger("click").die("click");
874 // Test binding with different this object, event data, and trigger data
875 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
876 equals( e.data, true, "live with with different this object, event data, and trigger data" );
877 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
878 equals( data, true, "live with with different this object, event data, and trigger data")
880 jQuery("#foo").trigger("click", true).die("click");
882 // Verify that return false prevents default action
883 jQuery("#anchor2").live("click", function(){ return false; });
884 var hash = window.location.hash;
885 jQuery("#anchor2").trigger("click");
886 equals( window.location.hash, hash, "return false worked" );
887 jQuery("#anchor2").die("click");
889 // Verify that .preventDefault() prevents default action
890 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
891 var hash = window.location.hash;
892 jQuery("#anchor2").trigger("click");
893 equals( window.location.hash, hash, "e.preventDefault() worked" );
894 jQuery("#anchor2").die("click");
896 // Test binding the same handler to multiple points
898 function callback(){ called++; return false; }
900 jQuery("#nothiddendiv").live("click", callback);
901 jQuery("#anchor2").live("click", callback);
903 jQuery("#nothiddendiv").trigger("click");
904 equals( called, 1, "Verify that only one click occurred." );
906 jQuery("#anchor2").trigger("click");
907 equals( called, 2, "Verify that only one click occurred." );
909 // Make sure that only one callback is removed
910 jQuery("#anchor2").die("click", callback);
912 jQuery("#nothiddendiv").trigger("click");
913 equals( called, 3, "Verify that only one click occurred." );
915 jQuery("#anchor2").trigger("click");
916 equals( called, 3, "Verify that no click occurred." );
918 // Make sure that it still works if the selector is the same,
919 // but the event type is different
920 jQuery("#nothiddendiv").live("foo", callback);
923 jQuery("#nothiddendiv").die("click", callback);
925 jQuery("#nothiddendiv").trigger("click");
926 equals( called, 3, "Verify that no click occurred." );
928 jQuery("#nothiddendiv").trigger("foo");
929 equals( called, 4, "Verify that one foo occurred." );
932 jQuery("#nothiddendiv").die("foo", callback);
934 // Make sure we don't loose the target by DOM modifications
935 // after the bubble already reached the liveHandler
936 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
938 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
939 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
941 jQuery("#nothiddendiv span").click();
942 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
943 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
946 jQuery("#nothiddendivchild").die("click");
948 // Verify that .live() ocurs and cancel buble in the same order as
949 // we would expect .bind() and .click() without delegation
950 var lived = 0, livee = 0;
952 // bind one pair in one order
953 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
954 jQuery('span#liveSpan1').live('click', function(){ livee++; });
956 jQuery('span#liveSpan1 a').click();
957 equals( lived, 1, "Verify that only one first handler occurred." );
958 equals( livee, 0, "Verify that second handler doesn't." );
960 // and one pair in inverse
961 jQuery('span#liveSpan2').live('click', function(){ livee++; });
962 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
966 jQuery('span#liveSpan2 a').click();
967 equals( lived, 1, "Verify that only one first handler occurred." );
968 equals( livee, 0, "Verify that second handler doesn't." );
971 jQuery("span#liveSpan1 a").die("click")
972 jQuery("span#liveSpan1").die("click");
973 jQuery("span#liveSpan2 a").die("click");
974 jQuery("span#liveSpan2").die("click");
976 // Test this, target and currentTarget are correct
977 jQuery('span#liveSpan1').live('click', function(e){
978 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
979 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
980 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
983 jQuery('span#liveSpan1 a').click();
985 jQuery('span#liveSpan1').die('click');
987 // Work with deep selectors
990 function clickB(){ livee++; }
992 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
993 jQuery("#nothiddendiv div").live("click", clickB);
994 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
996 equals( livee, 0, "No clicks, deep selector." );
999 jQuery("#nothiddendivchild").trigger("click");
1000 equals( livee, 2, "Click, deep selector." );
1003 jQuery("#nothiddendivchild").trigger("mouseover");
1004 equals( livee, 1, "Mouseover, deep selector." );
1006 jQuery("#nothiddendiv div").die("mouseover");
1009 jQuery("#nothiddendivchild").trigger("click");
1010 equals( livee, 2, "Click, deep selector." );
1013 jQuery("#nothiddendivchild").trigger("mouseover");
1014 equals( livee, 0, "Mouseover, deep selector." );
1016 jQuery("#nothiddendiv div").die("click", clickB);
1019 jQuery("#nothiddendivchild").trigger("click");
1020 equals( livee, 1, "Click, deep selector." );
1022 jQuery("#nothiddendiv div").die("click");
1025 test("die all bound events", function(){
1029 var div = jQuery("div#nothiddendivchild");
1031 div.live("click submit", function(){ count++; });
1034 div.trigger("click");
1035 div.trigger("submit");
1037 equals( count, 0, "Make sure no events were triggered." );
1040 test("live with multiple events", function(){
1044 var div = jQuery("div#nothiddendivchild");
1046 div.live("click submit", function(){ count++; });
1048 div.trigger("click");
1049 div.trigger("submit");
1051 equals( count, 2, "Make sure both the click and submit were triggered." );
1054 test("live with namespaces", function(){
1057 var count1 = 0, count2 = 0;
1059 jQuery("#liveSpan1").live("foo.bar", function(){
1063 jQuery("#liveSpan2").live("foo.zed", function(){
1067 jQuery("#liveSpan1").trigger("foo.bar");
1068 equals( count1, 1, "Got live foo.bar" );
1070 jQuery("#liveSpan2").trigger("foo.zed");
1071 equals( count2, 1, "Got live foo.zed" );
1074 jQuery("#liveSpan2").die("foo.zed");
1075 jQuery("#liveSpan1").trigger("foo.bar");
1077 equals( count1, 2, "Got live foo.bar after dieing foo.zed" );
1079 jQuery("#liveSpan2").trigger("foo.zed");
1080 equals( count2, 1, "Got live foo.zed" );
1083 jQuery("#liveSpan1").die("foo.bar");
1085 jQuery("#liveSpan1").trigger("foo.bar");
1086 equals( count1, 2, "Did not respond to foo.bar after dieing it" );
1088 jQuery("#liveSpan2").trigger("foo.zed");
1089 equals( count2, 1, "Did not trigger foo.zed again" );
1092 test("live with change", function(){
1093 var selectChange = 0, checkboxChange = 0;
1095 var select = jQuery("select[name='S1']")
1096 select.live("change", function() {
1100 var checkbox = jQuery("#check2"),
1101 checkboxFunction = function(){
1104 checkbox.live("change", checkboxFunction);
1106 // test click on select
1108 // second click that changed it
1110 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1111 select.trigger("change");
1112 equals( selectChange, 1, "Change on click." );
1114 // test keys on select
1116 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1117 select.trigger("change");
1118 equals( selectChange, 1, "Change on keyup." );
1120 // test click on checkbox
1121 checkbox.trigger("change");
1122 equals( checkboxChange, 1, "Change on checkbox." );
1124 // test before activate on radio
1126 // test blur/focus on textarea
1127 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1128 textarea.live("change", function() {
1132 textarea.val(oldVal + "foo");
1133 textarea.trigger("change");
1134 equals( textareaChange, 1, "Change on textarea." );
1136 textarea.val(oldVal);
1137 textarea.die("change");
1139 // test blur/focus on text
1140 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1141 text.live("change", function() {
1145 text.val(oldVal+"foo");
1146 text.trigger("change");
1147 equals( textChange, 1, "Change on text input." );
1149 text.val(oldTextVal);
1152 // test blur/focus on password
1153 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1154 password.live("change", function() {
1158 password.val(oldPasswordVal + "foo");
1159 password.trigger("change");
1160 equals( passwordChange, 1, "Change on password input." );
1162 password.val(oldPasswordVal);
1163 password.die("change");
1165 // make sure die works
1169 select.die("change");
1170 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1171 select.trigger("change");
1172 equals( selectChange, 0, "Die on click works." );
1175 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1176 select.trigger("change");
1177 equals( selectChange, 0, "Die on keyup works." );
1179 // die specific checkbox
1180 checkbox.die("change", checkboxFunction);
1181 checkbox.trigger("change");
1182 equals( checkboxChange, 1, "Die on checkbox." );
1185 test("live with submit", function() {
1186 var count1 = 0, count2 = 0;
1188 jQuery("#testForm").live("submit", function(ev) {
1190 ev.preventDefault();
1193 jQuery("body").live("submit", function(ev) {
1195 ev.preventDefault();
1198 if ( jQuery.support.submitBubbles ) {
1199 jQuery("#testForm input[name=sub1]")[0].click();
1203 jQuery("#testForm input[name=sub1]")[0].click();
1204 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1209 jQuery("#testForm").die("submit");
1210 jQuery("body").die("submit");
1213 test(".delegate()/.undelegate()", function() {
1216 var submit = 0, div = 0, livea = 0, liveb = 0;
1218 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1219 jQuery("#body").delegate("div", "click", function(){ div++; });
1220 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1221 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1223 // Nothing should trigger on the body
1224 jQuery("body").trigger("click");
1225 equals( submit, 0, "Click on body" );
1226 equals( div, 0, "Click on body" );
1227 equals( livea, 0, "Click on body" );
1228 equals( liveb, 0, "Click on body" );
1230 // This should trigger two events
1231 jQuery("div#nothiddendiv").trigger("click");
1232 equals( submit, 0, "Click on div" );
1233 equals( div, 1, "Click on div" );
1234 equals( livea, 1, "Click on div" );
1235 equals( liveb, 0, "Click on div" );
1237 // This should trigger three events (w/ bubbling)
1238 jQuery("div#nothiddendivchild").trigger("click");
1239 equals( submit, 0, "Click on inner div" );
1240 equals( div, 2, "Click on inner div" );
1241 equals( livea, 2, "Click on inner div" );
1242 equals( liveb, 1, "Click on inner div" );
1244 // This should trigger one submit
1245 jQuery("div#nothiddendivchild").trigger("submit");
1246 equals( submit, 1, "Submit on div" );
1247 equals( div, 2, "Submit on div" );
1248 equals( livea, 2, "Submit on div" );
1249 equals( liveb, 1, "Submit on div" );
1251 // Make sure no other events were removed in the process
1252 jQuery("div#nothiddendivchild").trigger("click");
1253 equals( submit, 1, "undelegate Click on inner div" );
1254 equals( div, 3, "undelegate Click on inner div" );
1255 equals( livea, 3, "undelegate Click on inner div" );
1256 equals( liveb, 2, "undelegate Click on inner div" );
1258 // Now make sure that the removal works
1259 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1260 jQuery("div#nothiddendivchild").trigger("click");
1261 equals( submit, 1, "undelegate Click on inner div" );
1262 equals( div, 4, "undelegate Click on inner div" );
1263 equals( livea, 4, "undelegate Click on inner div" );
1264 equals( liveb, 2, "undelegate Click on inner div" );
1266 // Make sure that the click wasn't removed too early
1267 jQuery("div#nothiddendiv").trigger("click");
1268 equals( submit, 1, "undelegate Click on inner div" );
1269 equals( div, 5, "undelegate Click on inner div" );
1270 equals( livea, 5, "undelegate Click on inner div" );
1271 equals( liveb, 2, "undelegate Click on inner div" );
1273 // Make sure that stopPropgation doesn't stop live events
1274 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1275 jQuery("div#nothiddendivchild").trigger("click");
1276 equals( submit, 1, "stopPropagation Click on inner div" );
1277 equals( div, 6, "stopPropagation Click on inner div" );
1278 equals( livea, 6, "stopPropagation Click on inner div" );
1279 equals( liveb, 3, "stopPropagation Click on inner div" );
1281 // Make sure click events only fire with primary click
1282 var event = jQuery.Event("click");
1284 jQuery("div#nothiddendiv").trigger(event);
1286 equals( livea, 6, "delegate secondary click" );
1288 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1289 jQuery("#body").undelegate("div#nothiddendiv", "click");
1290 jQuery("#body").undelegate("div", "click");
1291 jQuery("#body").undelegate("div", "submit");
1293 // Test binding with a different context
1294 var clicked = 0, container = jQuery('#main')[0];
1295 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1296 jQuery("div").trigger('click');
1297 jQuery("#foo").trigger('click');
1298 jQuery("#main").trigger('click');
1299 jQuery("body").trigger('click');
1300 equals( clicked, 2, "delegate with a context" );
1302 // Make sure the event is actually stored on the context
1303 ok( jQuery.data(container, "events").live, "delegate with a context" );
1305 // Test unbinding with a different context
1306 jQuery("#main").undelegate("#foo", "click");
1307 jQuery("#foo").trigger('click');
1308 equals( clicked, 2, "undelegate with a context");
1310 // Test binding with event data
1311 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1312 jQuery("#foo").trigger("click");
1313 jQuery("#body").undelegate("#foo", "click");
1315 // Test binding with trigger data
1316 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1317 jQuery("#foo").trigger("click", true);
1318 jQuery("#body").undelegate("#foo", "click");
1320 // Test binding with different this object
1321 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1322 jQuery("#foo").trigger("click");
1323 jQuery("#body").undelegate("#foo", "click");
1325 // Test binding with different this object, event data, and trigger data
1326 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1327 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1328 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1329 equals( data, true, "delegate with with different this object, event data, and trigger data")
1330 }, { foo: "bar" }));
1331 jQuery("#foo").trigger("click", true);
1332 jQuery("#body").undelegate("#foo", "click");
1334 // Verify that return false prevents default action
1335 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1336 var hash = window.location.hash;
1337 jQuery("#anchor2").trigger("click");
1338 equals( window.location.hash, hash, "return false worked" );
1339 jQuery("#body").undelegate("#anchor2", "click");
1341 // Verify that .preventDefault() prevents default action
1342 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1343 var hash = window.location.hash;
1344 jQuery("#anchor2").trigger("click");
1345 equals( window.location.hash, hash, "e.preventDefault() worked" );
1346 jQuery("#body").undelegate("#anchor2", "click");
1348 // Test binding the same handler to multiple points
1350 function callback(){ called++; return false; }
1352 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1353 jQuery("#body").delegate("#anchor2", "click", callback);
1355 jQuery("#nothiddendiv").trigger("click");
1356 equals( called, 1, "Verify that only one click occurred." );
1358 jQuery("#anchor2").trigger("click");
1359 equals( called, 2, "Verify that only one click occurred." );
1361 // Make sure that only one callback is removed
1362 jQuery("#body").undelegate("#anchor2", "click", callback);
1364 jQuery("#nothiddendiv").trigger("click");
1365 equals( called, 3, "Verify that only one click occurred." );
1367 jQuery("#anchor2").trigger("click");
1368 equals( called, 3, "Verify that no click occurred." );
1370 // Make sure that it still works if the selector is the same,
1371 // but the event type is different
1372 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1375 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1377 jQuery("#nothiddendiv").trigger("click");
1378 equals( called, 3, "Verify that no click occurred." );
1380 jQuery("#nothiddendiv").trigger("foo");
1381 equals( called, 4, "Verify that one foo occurred." );
1384 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1386 // Make sure we don't loose the target by DOM modifications
1387 // after the bubble already reached the liveHandler
1388 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1390 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1391 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1393 jQuery("#nothiddendiv span").click();
1394 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1395 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1398 jQuery("#body").undelegate("#nothiddendivchild", "click");
1400 // Verify that .live() ocurs and cancel buble in the same order as
1401 // we would expect .bind() and .click() without delegation
1402 var lived = 0, livee = 0;
1404 // bind one pair in one order
1405 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1406 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1408 jQuery('span#liveSpan1 a').click();
1409 equals( lived, 1, "Verify that only one first handler occurred." );
1410 equals( livee, 0, "Verify that second handler doesn't." );
1412 // and one pair in inverse
1413 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1414 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1418 jQuery('span#liveSpan2 a').click();
1419 equals( lived, 1, "Verify that only one first handler occurred." );
1420 equals( livee, 0, "Verify that second handler doesn't." );
1423 jQuery("#body").undelegate("click");
1425 // Test this, target and currentTarget are correct
1426 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1427 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1428 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1429 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1432 jQuery('span#liveSpan1 a').click();
1434 jQuery("#body").undelegate('span#liveSpan1', 'click');
1436 // Work with deep selectors
1439 function clickB(){ livee++; }
1441 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1442 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1443 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1445 equals( livee, 0, "No clicks, deep selector." );
1448 jQuery("#nothiddendivchild").trigger("click");
1449 equals( livee, 2, "Click, deep selector." );
1452 jQuery("#nothiddendivchild").trigger("mouseover");
1453 equals( livee, 1, "Mouseover, deep selector." );
1455 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1458 jQuery("#nothiddendivchild").trigger("click");
1459 equals( livee, 2, "Click, deep selector." );
1462 jQuery("#nothiddendivchild").trigger("mouseover");
1463 equals( livee, 0, "Mouseover, deep selector." );
1465 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1468 jQuery("#nothiddendivchild").trigger("click");
1469 equals( livee, 1, "Click, deep selector." );
1471 jQuery("#body").undelegate("#nothiddendiv div", "click");
1474 test("undelegate all bound events", function(){
1478 var div = jQuery("#body");
1480 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1483 jQuery("div#nothiddendivchild").trigger("click");
1484 jQuery("div#nothiddendivchild").trigger("submit");
1486 equals( count, 0, "Make sure no events were triggered." );
1489 test("delegate with multiple events", function(){
1493 var div = jQuery("#body");
1495 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1497 jQuery("div#nothiddendivchild").trigger("click");
1498 jQuery("div#nothiddendivchild").trigger("submit");
1500 equals( count, 2, "Make sure both the click and submit were triggered." );
1502 jQuery("#body").undelegate();
1505 test("delegate with change", function(){
1506 var selectChange = 0, checkboxChange = 0;
1508 var select = jQuery("select[name='S1']");
1509 jQuery("#body").delegate("select[name='S1']", "change", function() {
1513 var checkbox = jQuery("#check2"),
1514 checkboxFunction = function(){
1517 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1519 // test click on select
1521 // second click that changed it
1523 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1524 select.trigger("change");
1525 equals( selectChange, 1, "Change on click." );
1527 // test keys on select
1529 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1530 select.trigger("change");
1531 equals( selectChange, 1, "Change on keyup." );
1533 // test click on checkbox
1534 checkbox.trigger("change");
1535 equals( checkboxChange, 1, "Change on checkbox." );
1537 // test before activate on radio
1539 // test blur/focus on textarea
1540 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1541 jQuery("#body").delegate("#area1", "change", function() {
1545 textarea.val(oldVal + "foo");
1546 textarea.trigger("change");
1547 equals( textareaChange, 1, "Change on textarea." );
1549 textarea.val(oldVal);
1550 jQuery("#body").undelegate("#area1", "change");
1552 // test blur/focus on text
1553 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1554 jQuery("#body").delegate("#name", "change", function() {
1558 text.val(oldVal+"foo");
1559 text.trigger("change");
1560 equals( textChange, 1, "Change on text input." );
1562 text.val(oldTextVal);
1563 jQuery("#body").die("change");
1565 // test blur/focus on password
1566 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1567 jQuery("#body").delegate("#name", "change", function() {
1571 password.val(oldPasswordVal + "foo");
1572 password.trigger("change");
1573 equals( passwordChange, 1, "Change on password input." );
1575 password.val(oldPasswordVal);
1576 jQuery("#body").undelegate("#name", "change");
1578 // make sure die works
1582 jQuery("#body").undelegate("select[name='S1']", "change");
1583 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1584 select.trigger("change");
1585 equals( selectChange, 0, "Die on click works." );
1588 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1589 select.trigger("change");
1590 equals( selectChange, 0, "Die on keyup works." );
1592 // die specific checkbox
1593 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1594 checkbox.trigger("change");
1595 equals( checkboxChange, 1, "Die on checkbox." );
1598 test("delegate with submit", function() {
1599 var count1 = 0, count2 = 0;
1601 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1603 ev.preventDefault();
1606 jQuery(document).delegate("body", "submit", function(ev) {
1608 ev.preventDefault();
1611 if ( jQuery.support.submitBubbles ) {
1612 jQuery("#testForm input[name=sub1]")[0].click();
1616 jQuery("#testForm input[name=sub1]")[0].click();
1617 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1622 jQuery("#body").undelegate();
1623 jQuery(document).undelegate();
1626 test("Non DOM element events", function() {
1630 .bind('nonelementglobal', function(e) {
1631 ok( true, "Global event on non-DOM annonymos object triggered" );
1637 .bind('nonelementobj', function(e) {
1638 ok( true, "Event on non-DOM object triggered" );
1639 }).bind('nonelementglobal', function() {
1640 ok( true, "Global event on non-DOM object triggered" );
1643 jQuery(o).trigger('nonelementobj');
1644 jQuery.event.trigger('nonelementglobal');
1648 test("jQuery(function($) {})", function() {
1650 jQuery(function($) {
1651 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");
1656 test("event properties", function() {
1658 jQuery("#simon1").click(function(event) {
1659 ok( event.timeStamp, "assert event.timeStamp is present" );