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("click(), with data", function() {
16 var handler = function(event) {
17 ok( event.data, "bind() with data, check passed data exists" );
18 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
20 jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
22 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
25 test("bind(), with data, trigger with data", function() {
27 var handler = function(event, data) {
28 ok( event.data, "check passed data exists" );
29 equals( event.data.foo, "bar", "Check value of passed data" );
30 ok( data, "Check trigger data" );
31 equals( data.bar, "foo", "Check value of trigger data" );
33 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
36 test("bind(), multiple events at once", function() {
40 var handler = function(event) {
41 if (event.type == "click")
43 else if (event.type == "mouseover")
44 mouseoverCounter += 1;
46 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
47 equals( clickCounter, 1, "bind() with multiple events at once" );
48 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
51 test("bind(), multiple events at once and namespaces", function() {
56 var div = jQuery("<div/>").bind("focusin.a", function(e) {
57 equals( e.type, cur, "Verify right single event was fired." );
61 div.trigger("focusin.a");
63 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
64 equals( e.type, cur, "Verify right multi event was fired." );
65 equals( e.data, obj, "Make sure the data came in correctly." );
72 div.trigger("mouseover");
74 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
75 equals( e.type, cur, "Verify right multi event was fired." );
79 div.trigger("focusin.a");
82 div.trigger("focusout.b");
85 test("bind(), namespace with special add", function() {
88 var div = jQuery("<div/>").bind("test", function(e) {
89 ok( true, "Test event fired." );
94 jQuery.event.special.test = {
95 _default: function(e) {
96 equals( this, document, "Make sure we're at the top of the chain." );
97 equals( e.type, "test", "And that we're still dealing with a test event." );
98 equals( e.target, div[0], "And that the target is correct." );
101 teardown: function(){
102 ok(true, "Teardown called.");
104 add: function( handleObj ) {
105 var handler = handleObj.handler;
106 handleObj.handler = function(e) {
108 handler.apply( this, arguments );
112 ok(true, "Remove called.");
116 div.bind("test.a", {x: 1}, function(e) {
117 ok( !!e.xyz, "Make sure that the data is getting passed through." );
118 equals( e.data.x, 1, "Make sure data is attached properly." );
121 div.bind("test.b", {x: 2}, function(e) {
122 ok( !!e.xyz, "Make sure that the data is getting passed through." );
123 equals( e.data.x, 2, "Make sure data is attached properly." );
130 div.trigger("test.a");
133 div.trigger("test.b");
138 div = jQuery("<div/>").bind("test", function(e) {
139 ok( true, "Test event fired." );
143 div.appendTo("#main").remove();
145 delete jQuery.event.special.test;
148 test("bind(), no data", function() {
150 var handler = function(event) {
151 ok ( !event.data, "Check that no data is added to the event object" );
153 jQuery("#firstp").bind("click", handler).trigger("click");
156 test("bind/one/unbind(Object)", function(){
159 var clickCounter = 0, mouseoverCounter = 0;
160 function handler(event) {
161 if (event.type == "click")
163 else if (event.type == "mouseover")
167 function handlerWithData(event) {
168 if (event.type == "click")
169 clickCounter += event.data;
170 else if (event.type == "mouseover")
171 mouseoverCounter += event.data;
175 $elem.trigger("click").trigger("mouseover");
178 var $elem = jQuery("#firstp")
186 click:handlerWithData,
187 mouseover:handlerWithData
192 equals( clickCounter, 3, "bind(Object)" );
193 equals( mouseoverCounter, 3, "bind(Object)" );
196 equals( clickCounter, 4, "bind(Object)" );
197 equals( mouseoverCounter, 4, "bind(Object)" );
199 jQuery("#firstp").unbind({
205 equals( clickCounter, 4, "bind(Object)" );
206 equals( mouseoverCounter, 4, "bind(Object)" );
209 test("bind(), iframes", function() {
210 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
211 var doc = jQuery("#loadediframe").contents();
213 jQuery("div", doc).bind("click", function() {
214 ok( true, "Binding to element inside iframe" );
215 }).click().unbind('click');
218 test("bind(), trigger change on select", function() {
221 function selectOnChange(event) {
222 equals( event.data, counter++, "Event.data is not a global event object" );
224 jQuery("#form select").each(function(i){
225 jQuery(this).bind('change', i, selectOnChange);
226 }).trigger('change');
229 test("bind(), namespaced events, cloned events", function() {
232 jQuery("#firstp").bind("custom.test",function(e){
233 ok(true, "Custom event triggered");
236 jQuery("#firstp").bind("click",function(e){
237 ok(true, "Normal click triggered");
240 jQuery("#firstp").bind("click.test",function(e){
241 ok(true, "Namespaced click triggered");
244 // Trigger both bound fn (2)
245 jQuery("#firstp").trigger("click");
247 // Trigger one bound fn (1)
248 jQuery("#firstp").trigger("click.test");
250 // Remove only the one fn
251 jQuery("#firstp").unbind("click.test");
253 // Trigger the remaining fn (1)
254 jQuery("#firstp").trigger("click");
256 // Remove the remaining fn
257 jQuery("#firstp").unbind(".test");
259 // Trigger the remaining fn (0)
260 jQuery("#firstp").trigger("custom");
262 // using contents will get comments regular, text, and comment nodes
263 jQuery("#nonnodes").contents().bind("tester", function () {
264 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
265 }).trigger("tester");
267 // Make sure events stick with appendTo'd elements (which are cloned) #2027
268 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
269 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
272 test("bind(), multi-namespaced events", function() {
284 function check(name, msg){
285 same(name, order.shift(), msg);
288 jQuery("#firstp").bind("custom.test",function(e){
289 check("custom.test", "Custom event triggered");
292 jQuery("#firstp").bind("custom.test2",function(e){
293 check("custom.test2", "Custom event triggered");
296 jQuery("#firstp").bind("click.test",function(e){
297 check("click.test", "Normal click triggered");
300 jQuery("#firstp").bind("click.test.abc",function(e){
301 check("click.test.abc", "Namespaced click triggered");
304 // Those would not trigger/unbind (#5303)
305 jQuery("#firstp").trigger("click.a.test");
306 jQuery("#firstp").unbind("click.a.test");
308 // Trigger both bound fn (1)
309 jQuery("#firstp").trigger("click.test.abc");
311 // Trigger one bound fn (1)
312 jQuery("#firstp").trigger("click.abc");
314 // Trigger two bound fn (2)
315 jQuery("#firstp").trigger("click.test");
317 // Remove only the one fn
318 jQuery("#firstp").unbind("click.abc");
320 // Trigger the remaining fn (1)
321 jQuery("#firstp").trigger("click");
323 // Remove the remaining fn
324 jQuery("#firstp").unbind(".test");
326 // Trigger the remaining fn (1)
327 jQuery("#firstp").trigger("custom");
330 test("bind(), with same function", function() {
333 var count = 0 , func = function(){
337 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
338 jQuery("#liveHandlerOrder").trigger("foo.bar");
340 equals(count, 1, "Verify binding function with multiple namespaces." );
342 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
343 jQuery("#liveHandlerOrder").trigger("foo.bar");
345 equals(count, 1, "Verify that removing events still work." );
348 test("bind(), make sure order is maintained", function() {
351 var elem = jQuery("#firstp"), log = [], check = [];
353 for ( var i = 0; i < 100; i++ ) (function(i){
354 elem.bind( "click", function(){
361 elem.trigger("click");
363 equals( log.join(","), check.join(","), "Make sure order was maintained." );
365 elem.unbind("click");
368 test("bind(), with different this object", function() {
370 var thisObject = { myThis: true },
371 data = { myData: true },
372 handler1 = function( event ) {
373 equals( this, thisObject, "bind() with different this object" );
375 handler2 = function( event ) {
376 equals( this, thisObject, "bind() with different this object and data" );
377 equals( event.data, data, "bind() with different this object and data" );
381 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
382 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
384 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
387 test("bind()/trigger()/unbind() on plain object", function() {
392 // Make sure it doesn't complain when no events are found
393 jQuery(obj).trigger("test");
395 // Make sure it doesn't complain when no events are found
396 jQuery(obj).unbind("test");
398 jQuery(obj).bind("test", function(){
399 ok( true, "Custom event run." );
402 ok( jQuery(obj).data("events"), "Object has events bound." );
405 jQuery(obj).trigger("test");
407 jQuery(obj).unbind("test");
410 jQuery(obj).trigger("test");
412 // Make sure it doesn't complain when no events are found
413 jQuery(obj).unbind("test");
416 test("unbind(type)", function() {
419 var $elem = jQuery("#firstp"),
423 ok( false, message );
426 message = "unbind passing function";
427 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
429 message = "unbind all from event";
430 $elem.bind('error', error).unbind('error').triggerHandler('error');
432 message = "unbind all";
433 $elem.bind('error', error).unbind().triggerHandler('error');
435 message = "unbind many with function";
436 $elem.bind('error error2',error)
437 .unbind('error error2', error )
438 .trigger('error').triggerHandler('error2');
440 message = "unbind many"; // #3538
441 $elem.bind('error error2',error)
442 .unbind('error error2')
443 .trigger('error').triggerHandler('error2');
445 message = "unbind without a type or handler";
446 $elem.bind("error error2.test",error)
448 .trigger("error").triggerHandler("error2");
451 test("unbind(eventObject)", function() {
454 var $elem = jQuery("#firstp"),
457 function assert( expected ){
459 $elem.trigger('foo').triggerHandler('bar');
460 equals( num, expected, "Check the right handlers are triggered" );
464 // This handler shouldn't be unbound
465 .bind('foo', function(){
468 .bind('foo', function(e){
473 .bind('bar', function(){
487 test("hover()", function() {
489 handler1 = function( event ) { ++times; },
490 handler2 = function( event ) { ++times; };
493 .hover(handler1, handler2)
494 .mouseenter().mouseleave()
495 .unbind("mouseenter", handler1)
496 .unbind("mouseleave", handler2)
498 .mouseenter().mouseleave()
499 .unbind("mouseenter mouseleave", handler1)
500 .mouseenter().mouseleave();
502 equals( times, 4, "hover handlers fired" );
505 test("trigger() shortcuts", function() {
507 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
508 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
509 equals( close.length, 0, "Context element does not exist, length must be zero" );
510 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
514 jQuery("#check1").click(function() {
515 ok( true, "click event handler for checkbox gets fired twice, see #815" );
519 jQuery('#firstp')[0].onclick = function(event) {
522 jQuery('#firstp').click();
523 equals( counter, 1, "Check that click, triggers onclick event handler also" );
525 var clickCounter = 0;
526 jQuery('#simon1')[0].onclick = function(event) {
529 jQuery('#simon1').click();
530 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
532 jQuery('<img />').load(function(){
533 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
537 test("trigger() bubbling", function() {
540 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
542 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
543 jQuery("html").bind("click", function(e){ html++; });
544 jQuery("body").bind("click", function(e){ body++; });
545 jQuery("#main").bind("click", function(e){ main++; });
546 jQuery("#ap").bind("click", function(){ ap++; return false; });
548 jQuery("html").trigger("click");
549 equals( doc, 1, "HTML bubble" );
550 equals( html, 1, "HTML bubble" );
552 jQuery("body").trigger("click");
553 equals( doc, 2, "Body bubble" );
554 equals( html, 2, "Body bubble" );
555 equals( body, 1, "Body bubble" );
557 jQuery("#main").trigger("click");
558 equals( doc, 3, "Main bubble" );
559 equals( html, 3, "Main bubble" );
560 equals( body, 2, "Main bubble" );
561 equals( main, 1, "Main bubble" );
563 jQuery("#ap").trigger("click");
564 equals( doc, 3, "ap bubble" );
565 equals( html, 3, "ap bubble" );
566 equals( body, 2, "ap bubble" );
567 equals( main, 1, "ap bubble" );
568 equals( ap, 1, "ap bubble" );
571 test("trigger(type, [data], [fn])", function() {
574 var handler = function(event, a, b, c) {
575 equals( event.type, "click", "check passed data" );
576 equals( a, 1, "check passed data" );
577 equals( b, "2", "check passed data" );
578 equals( c, "abc", "check passed data" );
582 var $elem = jQuery("#firstp");
584 // Simulate a "native" click
585 $elem[0].click = function(){
586 ok( true, "Native call was triggered" );
589 // Triggers handlrs and native
591 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
593 // Simulate a "native" click
594 $elem[0].click = function(){
595 ok( false, "Native call was triggered" );
598 // Trigger only the handlers (no native)
600 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
604 jQuery('#form input:first').hide().trigger('focus');
608 ok( pass, "Trigger focus on hidden element" );
612 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
616 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
618 var form = jQuery("<form action=''></form>").appendTo("body");
620 // Make sure it can be prevented locally
621 form.submit(function(){
622 ok( true, "Local bind still works." );
627 form.trigger("submit");
629 form.unbind("submit");
631 jQuery(document).submit(function(){
632 ok( true, "Make sure bubble works up to document." );
637 form.trigger("submit");
639 jQuery(document).unbind("submit");
644 test("jQuery.Event.currentTarget", function(){
647 test("trigger(eventObject, [data], [fn])", function() {
650 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
651 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
653 var event = jQuery.Event("noNew");
654 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
655 equals( event.type, "noNew", "Verify its type" );
657 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
658 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
659 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
661 event.preventDefault();
662 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
663 event.stopPropagation();
664 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
666 event.isPropagationStopped = function(){ return false };
667 event.stopImmediatePropagation();
668 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
669 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
671 $parent.bind('foo',function(e){
673 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
674 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
675 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
676 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
679 // test with an event object
680 event = new jQuery.Event("foo");
681 event.secret = 'boo!';
682 $child.trigger(event);
684 // test with a literal object
685 $child.trigger({type:'foo', secret:'boo!'});
690 ok( false, "This assertion shouldn't be reached");
693 $parent.bind('foo', error );
695 $child.bind('foo',function(e, a, b, c ){
696 equals( arguments.length, 4, "Check arguments length");
697 equals( a, 1, "Check first custom argument");
698 equals( b, 2, "Check second custom argument");
699 equals( c, 3, "Check third custom argument");
701 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
702 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
703 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
706 e.stopImmediatePropagation();
711 // We should add this back in when we want to test the order
712 // in which event handlers are iterated.
713 //$child.bind('foo', error );
715 event = new jQuery.Event("foo");
716 $child.trigger( event, [1,2,3] ).unbind();
717 equals( event.result, "result", "Check event.result attribute");
719 // Will error if it bubbles
720 $child.triggerHandler('foo');
723 $parent.unbind().remove();
726 test("jQuery.Event.currentTarget", function(){
730 $elem = jQuery('<button>a</button>').click(function(e){
731 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
735 $elem.trigger('click');
741 test("toggle(Function, Function, ...)", function() {
745 fn1 = function(e) { count++; },
746 fn2 = function(e) { count--; },
747 preventDefault = function(e) { e.preventDefault() },
748 link = jQuery('#mark');
749 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
750 equals( count, 1, "Check for toggle(fn, fn)" );
752 jQuery("#firstp").toggle(function () {
753 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
754 }, function() {}).trigger("click", [ 1, 2, 3 ]);
757 jQuery("#simon1").one("click", function() {
758 ok( true, "Execute event only once" );
759 jQuery(this).toggle(function() {
760 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
762 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
765 }).click().click().click();
780 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
782 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
784 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
786 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
788 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
790 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
792 $div.unbind('click',fns[0]);
793 var data = jQuery.data( $div[0], 'events' );
794 ok( !data, "Unbinding one function from toggle unbinds them all");
796 // Test Multi-Toggles
798 $div = jQuery("<div/>");
799 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
801 same( a, [1], "Check that a click worked." );
803 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
805 same( a, [1,2], "Check that a click worked with a second toggle." );
806 same( b, [1], "Check that a click worked with a second toggle." );
809 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
810 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
813 test(".live()/.die()", function() {
816 var submit = 0, div = 0, livea = 0, liveb = 0;
818 jQuery("div").live("submit", function(){ submit++; return false; });
819 jQuery("div").live("click", function(){ div++; });
820 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
821 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
823 // Nothing should trigger on the body
824 jQuery("body").trigger("click");
825 equals( submit, 0, "Click on body" );
826 equals( div, 0, "Click on body" );
827 equals( livea, 0, "Click on body" );
828 equals( liveb, 0, "Click on body" );
830 // This should trigger two events
831 submit = 0, div = 0, livea = 0, liveb = 0;
832 jQuery("div#nothiddendiv").trigger("click");
833 equals( submit, 0, "Click on div" );
834 equals( div, 1, "Click on div" );
835 equals( livea, 1, "Click on div" );
836 equals( liveb, 0, "Click on div" );
838 // This should trigger three events (w/ bubbling)
839 submit = 0, div = 0, livea = 0, liveb = 0;
840 jQuery("div#nothiddendivchild").trigger("click");
841 equals( submit, 0, "Click on inner div" );
842 equals( div, 2, "Click on inner div" );
843 equals( livea, 1, "Click on inner div" );
844 equals( liveb, 1, "Click on inner div" );
846 // This should trigger one submit
847 submit = 0, div = 0, livea = 0, liveb = 0;
848 jQuery("div#nothiddendivchild").trigger("submit");
849 equals( submit, 1, "Submit on div" );
850 equals( div, 0, "Submit on div" );
851 equals( livea, 0, "Submit on div" );
852 equals( liveb, 0, "Submit on div" );
854 // Make sure no other events were removed in the process
855 submit = 0, div = 0, livea = 0, liveb = 0;
856 jQuery("div#nothiddendivchild").trigger("click");
857 equals( submit, 0, "die Click on inner div" );
858 equals( div, 2, "die Click on inner div" );
859 equals( livea, 1, "die Click on inner div" );
860 equals( liveb, 1, "die Click on inner div" );
862 // Now make sure that the removal works
863 submit = 0, div = 0, livea = 0, liveb = 0;
864 jQuery("div#nothiddendivchild").die("click");
865 jQuery("div#nothiddendivchild").trigger("click");
866 equals( submit, 0, "die Click on inner div" );
867 equals( div, 2, "die Click on inner div" );
868 equals( livea, 1, "die Click on inner div" );
869 equals( liveb, 0, "die Click on inner div" );
871 // Make sure that the click wasn't removed too early
872 submit = 0, div = 0, livea = 0, liveb = 0;
873 jQuery("div#nothiddendiv").trigger("click");
874 equals( submit, 0, "die Click on inner div" );
875 equals( div, 1, "die Click on inner div" );
876 equals( livea, 1, "die Click on inner div" );
877 equals( liveb, 0, "die Click on inner div" );
879 // Make sure that stopPropgation doesn't stop live events
880 submit = 0, div = 0, livea = 0, liveb = 0;
881 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
882 jQuery("div#nothiddendivchild").trigger("click");
883 equals( submit, 0, "stopPropagation Click on inner div" );
884 equals( div, 1, "stopPropagation Click on inner div" );
885 equals( livea, 0, "stopPropagation Click on inner div" );
886 equals( liveb, 1, "stopPropagation Click on inner div" );
888 // Make sure click events only fire with primary click
889 submit = 0, div = 0, livea = 0, liveb = 0;
890 var event = jQuery.Event("click");
892 jQuery("div#nothiddendiv").trigger(event);
894 equals( livea, 0, "live secondary click" );
896 jQuery("div#nothiddendivchild").die("click");
897 jQuery("div#nothiddendiv").die("click");
898 jQuery("div").die("click");
899 jQuery("div").die("submit");
901 // Test binding with a different context
902 var clicked = 0, container = jQuery('#main')[0];
903 jQuery("#foo", container).live("click", function(e){ clicked++; });
904 jQuery("div").trigger('click');
905 jQuery("#foo").trigger('click');
906 jQuery("#main").trigger('click');
907 jQuery("body").trigger('click');
908 equals( clicked, 2, "live with a context" );
910 // Make sure the event is actually stored on the context
911 ok( jQuery.data(container, "events").live, "live with a context" );
913 // Test unbinding with a different context
914 jQuery("#foo", container).die("click");
915 jQuery("#foo").trigger('click');
916 equals( clicked, 2, "die with a context");
918 // Test binding with event data
919 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
920 jQuery("#foo").trigger("click").die("click");
922 // Test binding with trigger data
923 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
924 jQuery("#foo").trigger("click", true).die("click");
926 // Test binding with different this object
927 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
928 jQuery("#foo").trigger("click").die("click");
930 // Test binding with different this object, event data, and trigger data
931 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
932 equals( e.data, true, "live with with different this object, event data, and trigger data" );
933 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
934 equals( data, true, "live with with different this object, event data, and trigger data")
936 jQuery("#foo").trigger("click", true).die("click");
938 // Verify that return false prevents default action
939 jQuery("#anchor2").live("click", function(){ return false; });
940 var hash = window.location.hash;
941 jQuery("#anchor2").trigger("click");
942 equals( window.location.hash, hash, "return false worked" );
943 jQuery("#anchor2").die("click");
945 // Verify that .preventDefault() prevents default action
946 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
947 var hash = window.location.hash;
948 jQuery("#anchor2").trigger("click");
949 equals( window.location.hash, hash, "e.preventDefault() worked" );
950 jQuery("#anchor2").die("click");
952 // Test binding the same handler to multiple points
954 function callback(){ called++; return false; }
956 jQuery("#nothiddendiv").live("click", callback);
957 jQuery("#anchor2").live("click", callback);
959 jQuery("#nothiddendiv").trigger("click");
960 equals( called, 1, "Verify that only one click occurred." );
962 jQuery("#anchor2").trigger("click");
963 equals( called, 2, "Verify that only one click occurred." );
965 // Make sure that only one callback is removed
966 jQuery("#anchor2").die("click", callback);
968 jQuery("#nothiddendiv").trigger("click");
969 equals( called, 3, "Verify that only one click occurred." );
971 jQuery("#anchor2").trigger("click");
972 equals( called, 3, "Verify that no click occurred." );
974 // Make sure that it still works if the selector is the same,
975 // but the event type is different
976 jQuery("#nothiddendiv").live("foo", callback);
979 jQuery("#nothiddendiv").die("click", callback);
981 jQuery("#nothiddendiv").trigger("click");
982 equals( called, 3, "Verify that no click occurred." );
984 jQuery("#nothiddendiv").trigger("foo");
985 equals( called, 4, "Verify that one foo occurred." );
988 jQuery("#nothiddendiv").die("foo", callback);
990 // Make sure we don't loose the target by DOM modifications
991 // after the bubble already reached the liveHandler
992 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
994 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
995 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
997 jQuery("#nothiddendiv span").click();
998 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
999 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1002 jQuery("#nothiddendivchild").die("click");
1004 // Verify that .live() ocurs and cancel buble in the same order as
1005 // we would expect .bind() and .click() without delegation
1006 var lived = 0, livee = 0;
1008 // bind one pair in one order
1009 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1010 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1012 jQuery('span#liveSpan1 a').click();
1013 equals( lived, 1, "Verify that only one first handler occurred." );
1014 equals( livee, 0, "Verify that second handler doesn't." );
1016 // and one pair in inverse
1017 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1018 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1022 jQuery('span#liveSpan2 a').click();
1023 equals( lived, 1, "Verify that only one first handler occurred." );
1024 equals( livee, 0, "Verify that second handler doesn't." );
1027 jQuery("span#liveSpan1 a").die("click")
1028 jQuery("span#liveSpan1").die("click");
1029 jQuery("span#liveSpan2 a").die("click");
1030 jQuery("span#liveSpan2").die("click");
1032 // Test this, target and currentTarget are correct
1033 jQuery('span#liveSpan1').live('click', function(e){
1034 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1035 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1036 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1039 jQuery('span#liveSpan1 a').click();
1041 jQuery('span#liveSpan1').die('click');
1043 // Work with deep selectors
1046 function clickB(){ livee++; }
1048 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1049 jQuery("#nothiddendiv div").live("click", clickB);
1050 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1052 equals( livee, 0, "No clicks, deep selector." );
1055 jQuery("#nothiddendivchild").trigger("click");
1056 equals( livee, 2, "Click, deep selector." );
1059 jQuery("#nothiddendivchild").trigger("mouseover");
1060 equals( livee, 1, "Mouseover, deep selector." );
1062 jQuery("#nothiddendiv div").die("mouseover");
1065 jQuery("#nothiddendivchild").trigger("click");
1066 equals( livee, 2, "Click, deep selector." );
1069 jQuery("#nothiddendivchild").trigger("mouseover");
1070 equals( livee, 0, "Mouseover, deep selector." );
1072 jQuery("#nothiddendiv div").die("click", clickB);
1075 jQuery("#nothiddendivchild").trigger("click");
1076 equals( livee, 1, "Click, deep selector." );
1078 jQuery("#nothiddendiv div").die("click");
1080 jQuery("#nothiddendiv div").live("blur", function(){
1081 ok( true, "Live div trigger blur." );
1084 jQuery("#nothiddendiv div").trigger("blur");
1086 jQuery("#nothiddendiv div").die("blur");
1089 test("die all bound events", function(){
1093 var div = jQuery("div#nothiddendivchild");
1095 div.live("click submit", function(){ count++; });
1098 div.trigger("click");
1099 div.trigger("submit");
1101 equals( count, 0, "Make sure no events were triggered." );
1104 test("live with multiple events", function(){
1108 var div = jQuery("div#nothiddendivchild");
1110 div.live("click submit", function(){ count++; });
1112 div.trigger("click");
1113 div.trigger("submit");
1115 equals( count, 2, "Make sure both the click and submit were triggered." );
1118 test("live with namespaces", function(){
1121 var count1 = 0, count2 = 0;
1123 jQuery("#liveSpan1").live("foo.bar", function(){
1127 jQuery("#liveSpan2").live("foo.zed", function(){
1131 jQuery("#liveSpan1").trigger("foo.bar");
1132 equals( count1, 1, "Got live foo.bar" );
1134 jQuery("#liveSpan2").trigger("foo.zed");
1135 equals( count2, 1, "Got live foo.zed" );
1138 jQuery("#liveSpan2").die("foo.zed");
1139 jQuery("#liveSpan1").trigger("foo.bar");
1141 equals( count1, 2, "Got live foo.bar after dieing foo.zed" );
1143 jQuery("#liveSpan2").trigger("foo.zed");
1144 equals( count2, 1, "Got live foo.zed" );
1147 jQuery("#liveSpan1").die("foo.bar");
1149 jQuery("#liveSpan1").trigger("foo.bar");
1150 equals( count1, 2, "Did not respond to foo.bar after dieing it" );
1152 jQuery("#liveSpan2").trigger("foo.zed");
1153 equals( count2, 1, "Did not trigger foo.zed again" );
1156 test("live with change", function(){
1157 var selectChange = 0, checkboxChange = 0;
1159 var select = jQuery("select[name='S1']")
1160 select.live("change", function() {
1164 var checkbox = jQuery("#check2"),
1165 checkboxFunction = function(){
1168 checkbox.live("change", checkboxFunction);
1170 // test click on select
1172 // second click that changed it
1174 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1175 select.trigger("change");
1176 equals( selectChange, 1, "Change on click." );
1178 // test keys on select
1180 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1181 select.trigger("change");
1182 equals( selectChange, 1, "Change on keyup." );
1184 // test click on checkbox
1185 checkbox.trigger("change");
1186 equals( checkboxChange, 1, "Change on checkbox." );
1188 // test before activate on radio
1190 // test blur/focus on textarea
1191 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1192 textarea.live("change", function() {
1196 textarea.val(oldVal + "foo");
1197 textarea.trigger("change");
1198 equals( textareaChange, 1, "Change on textarea." );
1200 textarea.val(oldVal);
1201 textarea.die("change");
1203 // test blur/focus on text
1204 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1205 text.live("change", function() {
1209 text.val(oldVal+"foo");
1210 text.trigger("change");
1211 equals( textChange, 1, "Change on text input." );
1213 text.val(oldTextVal);
1216 // test blur/focus on password
1217 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1218 password.live("change", function() {
1222 password.val(oldPasswordVal + "foo");
1223 password.trigger("change");
1224 equals( passwordChange, 1, "Change on password input." );
1226 password.val(oldPasswordVal);
1227 password.die("change");
1229 // make sure die works
1233 select.die("change");
1234 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1235 select.trigger("change");
1236 equals( selectChange, 0, "Die on click works." );
1239 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1240 select.trigger("change");
1241 equals( selectChange, 0, "Die on keyup works." );
1243 // die specific checkbox
1244 checkbox.die("change", checkboxFunction);
1245 checkbox.trigger("change");
1246 equals( checkboxChange, 1, "Die on checkbox." );
1249 test("live with submit", function() {
1250 var count1 = 0, count2 = 0;
1252 jQuery("#testForm").live("submit", function(ev) {
1254 ev.preventDefault();
1257 jQuery("body").live("submit", function(ev) {
1259 ev.preventDefault();
1262 if ( jQuery.support.submitBubbles ) {
1263 jQuery("#testForm input[name=sub1]")[0].click();
1267 jQuery("#testForm input[name=sub1]")[0].click();
1268 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1273 jQuery("#testForm").die("submit");
1274 jQuery("body").die("submit");
1277 test(".delegate()/.undelegate()", function() {
1280 var submit = 0, div = 0, livea = 0, liveb = 0;
1282 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1283 jQuery("#body").delegate("div", "click", function(){ div++; });
1284 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1285 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1287 // Nothing should trigger on the body
1288 jQuery("body").trigger("click");
1289 equals( submit, 0, "Click on body" );
1290 equals( div, 0, "Click on body" );
1291 equals( livea, 0, "Click on body" );
1292 equals( liveb, 0, "Click on body" );
1294 // This should trigger two events
1295 submit = 0, div = 0, livea = 0, liveb = 0;
1296 jQuery("div#nothiddendiv").trigger("click");
1297 equals( submit, 0, "Click on div" );
1298 equals( div, 1, "Click on div" );
1299 equals( livea, 1, "Click on div" );
1300 equals( liveb, 0, "Click on div" );
1302 // This should trigger three events (w/ bubbling)
1303 submit = 0, div = 0, livea = 0, liveb = 0;
1304 jQuery("div#nothiddendivchild").trigger("click");
1305 equals( submit, 0, "Click on inner div" );
1306 equals( div, 2, "Click on inner div" );
1307 equals( livea, 1, "Click on inner div" );
1308 equals( liveb, 1, "Click on inner div" );
1310 // This should trigger one submit
1311 submit = 0, div = 0, livea = 0, liveb = 0;
1312 jQuery("div#nothiddendivchild").trigger("submit");
1313 equals( submit, 1, "Submit on div" );
1314 equals( div, 0, "Submit on div" );
1315 equals( livea, 0, "Submit on div" );
1316 equals( liveb, 0, "Submit on div" );
1318 // Make sure no other events were removed in the process
1319 submit = 0, div = 0, livea = 0, liveb = 0;
1320 jQuery("div#nothiddendivchild").trigger("click");
1321 equals( submit, 0, "undelegate Click on inner div" );
1322 equals( div, 2, "undelegate Click on inner div" );
1323 equals( livea, 1, "undelegate Click on inner div" );
1324 equals( liveb, 1, "undelegate Click on inner div" );
1326 // Now make sure that the removal works
1327 submit = 0, div = 0, livea = 0, liveb = 0;
1328 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1329 jQuery("div#nothiddendivchild").trigger("click");
1330 equals( submit, 0, "undelegate Click on inner div" );
1331 equals( div, 2, "undelegate Click on inner div" );
1332 equals( livea, 1, "undelegate Click on inner div" );
1333 equals( liveb, 0, "undelegate Click on inner div" );
1335 // Make sure that the click wasn't removed too early
1336 submit = 0, div = 0, livea = 0, liveb = 0;
1337 jQuery("div#nothiddendiv").trigger("click");
1338 equals( submit, 0, "undelegate Click on inner div" );
1339 equals( div, 1, "undelegate Click on inner div" );
1340 equals( livea, 1, "undelegate Click on inner div" );
1341 equals( liveb, 0, "undelegate Click on inner div" );
1343 // Make sure that stopPropgation doesn't stop live events
1344 submit = 0, div = 0, livea = 0, liveb = 0;
1345 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1346 jQuery("div#nothiddendivchild").trigger("click");
1347 equals( submit, 0, "stopPropagation Click on inner div" );
1348 equals( div, 1, "stopPropagation Click on inner div" );
1349 equals( livea, 0, "stopPropagation Click on inner div" );
1350 equals( liveb, 1, "stopPropagation Click on inner div" );
1352 // Make sure click events only fire with primary click
1353 submit = 0, div = 0, livea = 0, liveb = 0;
1354 var event = jQuery.Event("click");
1356 jQuery("div#nothiddendiv").trigger(event);
1358 equals( livea, 0, "delegate secondary click" );
1360 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1361 jQuery("#body").undelegate("div#nothiddendiv", "click");
1362 jQuery("#body").undelegate("div", "click");
1363 jQuery("#body").undelegate("div", "submit");
1365 // Test binding with a different context
1366 var clicked = 0, container = jQuery('#main')[0];
1367 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1368 jQuery("div").trigger('click');
1369 jQuery("#foo").trigger('click');
1370 jQuery("#main").trigger('click');
1371 jQuery("body").trigger('click');
1372 equals( clicked, 2, "delegate with a context" );
1374 // Make sure the event is actually stored on the context
1375 ok( jQuery.data(container, "events").live, "delegate with a context" );
1377 // Test unbinding with a different context
1378 jQuery("#main").undelegate("#foo", "click");
1379 jQuery("#foo").trigger('click');
1380 equals( clicked, 2, "undelegate with a context");
1382 // Test binding with event data
1383 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1384 jQuery("#foo").trigger("click");
1385 jQuery("#body").undelegate("#foo", "click");
1387 // Test binding with trigger data
1388 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1389 jQuery("#foo").trigger("click", true);
1390 jQuery("#body").undelegate("#foo", "click");
1392 // Test binding with different this object
1393 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1394 jQuery("#foo").trigger("click");
1395 jQuery("#body").undelegate("#foo", "click");
1397 // Test binding with different this object, event data, and trigger data
1398 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1399 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1400 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1401 equals( data, true, "delegate with with different this object, event data, and trigger data")
1402 }, { foo: "bar" }));
1403 jQuery("#foo").trigger("click", true);
1404 jQuery("#body").undelegate("#foo", "click");
1406 // Verify that return false prevents default action
1407 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1408 var hash = window.location.hash;
1409 jQuery("#anchor2").trigger("click");
1410 equals( window.location.hash, hash, "return false worked" );
1411 jQuery("#body").undelegate("#anchor2", "click");
1413 // Verify that .preventDefault() prevents default action
1414 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1415 var hash = window.location.hash;
1416 jQuery("#anchor2").trigger("click");
1417 equals( window.location.hash, hash, "e.preventDefault() worked" );
1418 jQuery("#body").undelegate("#anchor2", "click");
1420 // Test binding the same handler to multiple points
1422 function callback(){ called++; return false; }
1424 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1425 jQuery("#body").delegate("#anchor2", "click", callback);
1427 jQuery("#nothiddendiv").trigger("click");
1428 equals( called, 1, "Verify that only one click occurred." );
1430 jQuery("#anchor2").trigger("click");
1431 equals( called, 2, "Verify that only one click occurred." );
1433 // Make sure that only one callback is removed
1434 jQuery("#body").undelegate("#anchor2", "click", callback);
1436 jQuery("#nothiddendiv").trigger("click");
1437 equals( called, 3, "Verify that only one click occurred." );
1439 jQuery("#anchor2").trigger("click");
1440 equals( called, 3, "Verify that no click occurred." );
1442 // Make sure that it still works if the selector is the same,
1443 // but the event type is different
1444 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1447 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1449 jQuery("#nothiddendiv").trigger("click");
1450 equals( called, 3, "Verify that no click occurred." );
1452 jQuery("#nothiddendiv").trigger("foo");
1453 equals( called, 4, "Verify that one foo occurred." );
1456 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1458 // Make sure we don't loose the target by DOM modifications
1459 // after the bubble already reached the liveHandler
1460 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1462 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1463 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1465 jQuery("#nothiddendiv span").click();
1466 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1467 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1470 jQuery("#body").undelegate("#nothiddendivchild", "click");
1472 // Verify that .live() ocurs and cancel buble in the same order as
1473 // we would expect .bind() and .click() without delegation
1474 var lived = 0, livee = 0;
1476 // bind one pair in one order
1477 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1478 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1480 jQuery('span#liveSpan1 a').click();
1481 equals( lived, 1, "Verify that only one first handler occurred." );
1482 equals( livee, 0, "Verify that second handler doesn't." );
1484 // and one pair in inverse
1485 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1486 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1490 jQuery('span#liveSpan2 a').click();
1491 equals( lived, 1, "Verify that only one first handler occurred." );
1492 equals( livee, 0, "Verify that second handler doesn't." );
1495 jQuery("#body").undelegate("click");
1497 // Test this, target and currentTarget are correct
1498 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1499 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1500 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1501 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1504 jQuery('span#liveSpan1 a').click();
1506 jQuery("#body").undelegate('span#liveSpan1', 'click');
1508 // Work with deep selectors
1511 function clickB(){ livee++; }
1513 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1514 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1515 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1517 equals( livee, 0, "No clicks, deep selector." );
1520 jQuery("#nothiddendivchild").trigger("click");
1521 equals( livee, 2, "Click, deep selector." );
1524 jQuery("#nothiddendivchild").trigger("mouseover");
1525 equals( livee, 1, "Mouseover, deep selector." );
1527 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1530 jQuery("#nothiddendivchild").trigger("click");
1531 equals( livee, 2, "Click, deep selector." );
1534 jQuery("#nothiddendivchild").trigger("mouseover");
1535 equals( livee, 0, "Mouseover, deep selector." );
1537 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1540 jQuery("#nothiddendivchild").trigger("click");
1541 equals( livee, 1, "Click, deep selector." );
1543 jQuery("#body").undelegate("#nothiddendiv div", "click");
1546 test("undelegate all bound events", function(){
1550 var div = jQuery("#body");
1552 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1555 jQuery("div#nothiddendivchild").trigger("click");
1556 jQuery("div#nothiddendivchild").trigger("submit");
1558 equals( count, 0, "Make sure no events were triggered." );
1561 test("delegate with multiple events", function(){
1565 var div = jQuery("#body");
1567 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1569 jQuery("div#nothiddendivchild").trigger("click");
1570 jQuery("div#nothiddendivchild").trigger("submit");
1572 equals( count, 2, "Make sure both the click and submit were triggered." );
1574 jQuery("#body").undelegate();
1577 test("delegate with change", function(){
1578 var selectChange = 0, checkboxChange = 0;
1580 var select = jQuery("select[name='S1']");
1581 jQuery("#body").delegate("select[name='S1']", "change", function() {
1585 var checkbox = jQuery("#check2"),
1586 checkboxFunction = function(){
1589 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1591 // test click on select
1593 // second click that changed it
1595 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1596 select.trigger("change");
1597 equals( selectChange, 1, "Change on click." );
1599 // test keys on select
1601 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1602 select.trigger("change");
1603 equals( selectChange, 1, "Change on keyup." );
1605 // test click on checkbox
1606 checkbox.trigger("change");
1607 equals( checkboxChange, 1, "Change on checkbox." );
1609 // test before activate on radio
1611 // test blur/focus on textarea
1612 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1613 jQuery("#body").delegate("#area1", "change", function() {
1617 textarea.val(oldVal + "foo");
1618 textarea.trigger("change");
1619 equals( textareaChange, 1, "Change on textarea." );
1621 textarea.val(oldVal);
1622 jQuery("#body").undelegate("#area1", "change");
1624 // test blur/focus on text
1625 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1626 jQuery("#body").delegate("#name", "change", function() {
1630 text.val(oldVal+"foo");
1631 text.trigger("change");
1632 equals( textChange, 1, "Change on text input." );
1634 text.val(oldTextVal);
1635 jQuery("#body").die("change");
1637 // test blur/focus on password
1638 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1639 jQuery("#body").delegate("#name", "change", function() {
1643 password.val(oldPasswordVal + "foo");
1644 password.trigger("change");
1645 equals( passwordChange, 1, "Change on password input." );
1647 password.val(oldPasswordVal);
1648 jQuery("#body").undelegate("#name", "change");
1650 // make sure die works
1654 jQuery("#body").undelegate("select[name='S1']", "change");
1655 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1656 select.trigger("change");
1657 equals( selectChange, 0, "Die on click works." );
1660 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1661 select.trigger("change");
1662 equals( selectChange, 0, "Die on keyup works." );
1664 // die specific checkbox
1665 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1666 checkbox.trigger("change");
1667 equals( checkboxChange, 1, "Die on checkbox." );
1670 test("delegate with submit", function() {
1671 var count1 = 0, count2 = 0;
1673 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1675 ev.preventDefault();
1678 jQuery(document).delegate("body", "submit", function(ev) {
1680 ev.preventDefault();
1683 if ( jQuery.support.submitBubbles ) {
1684 jQuery("#testForm input[name=sub1]")[0].click();
1688 jQuery("#testForm input[name=sub1]")[0].click();
1689 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1694 jQuery("#body").undelegate();
1695 jQuery(document).undelegate();
1698 test("Non DOM element events", function() {
1702 .bind('nonelementglobal', function(e) {
1703 ok( true, "Global event on non-DOM annonymos object triggered" );
1709 .bind('nonelementobj', function(e) {
1710 ok( true, "Event on non-DOM object triggered" );
1711 }).bind('nonelementglobal', function() {
1712 ok( true, "Global event on non-DOM object triggered" );
1715 jQuery(o).trigger('nonelementobj');
1716 jQuery.event.trigger('nonelementglobal');
1720 test("jQuery(function($) {})", function() {
1722 jQuery(function($) {
1723 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");
1728 test("event properties", function() {
1730 jQuery("#simon1").click(function(event) {
1731 ok( event.timeStamp, "assert event.timeStamp is present" );