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(name, false), unbind(name, false)", function() {
391 jQuery("#main").bind("click", function(e){ main++; });
392 jQuery("#ap").trigger("click");
393 equals( main, 1, "Verify that the trigger happened correctly." );
396 jQuery("#ap").bind("click", false);
397 jQuery("#ap").trigger("click");
398 equals( main, 0, "Verify that no bubble happened." );
401 jQuery("#ap").unbind("click", false);
402 jQuery("#ap").trigger("click");
403 equals( main, 1, "Verify that the trigger happened correctly." );
406 test("bind()/trigger()/unbind() on plain object", function() {
411 // Make sure it doesn't complain when no events are found
412 jQuery(obj).trigger("test");
414 // Make sure it doesn't complain when no events are found
415 jQuery(obj).unbind("test");
417 jQuery(obj).bind("test", function(){
418 ok( true, "Custom event run." );
421 ok( jQuery(obj).data("events"), "Object has events bound." );
424 jQuery(obj).trigger("test");
426 jQuery(obj).unbind("test");
429 jQuery(obj).trigger("test");
431 // Make sure it doesn't complain when no events are found
432 jQuery(obj).unbind("test");
435 test("unbind(type)", function() {
438 var $elem = jQuery("#firstp"),
442 ok( false, message );
445 message = "unbind passing function";
446 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
448 message = "unbind all from event";
449 $elem.bind('error', error).unbind('error').triggerHandler('error');
451 message = "unbind all";
452 $elem.bind('error', error).unbind().triggerHandler('error');
454 message = "unbind many with function";
455 $elem.bind('error error2',error)
456 .unbind('error error2', error )
457 .trigger('error').triggerHandler('error2');
459 message = "unbind many"; // #3538
460 $elem.bind('error error2',error)
461 .unbind('error error2')
462 .trigger('error').triggerHandler('error2');
464 message = "unbind without a type or handler";
465 $elem.bind("error error2.test",error)
467 .trigger("error").triggerHandler("error2");
470 test("unbind(eventObject)", function() {
473 var $elem = jQuery("#firstp"),
476 function assert( expected ){
478 $elem.trigger('foo').triggerHandler('bar');
479 equals( num, expected, "Check the right handlers are triggered" );
483 // This handler shouldn't be unbound
484 .bind('foo', function(){
487 .bind('foo', function(e){
492 .bind('bar', function(){
506 test("hover()", function() {
508 handler1 = function( event ) { ++times; },
509 handler2 = function( event ) { ++times; };
512 .hover(handler1, handler2)
513 .mouseenter().mouseleave()
514 .unbind("mouseenter", handler1)
515 .unbind("mouseleave", handler2)
517 .mouseenter().mouseleave()
518 .unbind("mouseenter mouseleave", handler1)
519 .mouseenter().mouseleave();
521 equals( times, 4, "hover handlers fired" );
524 test("trigger() shortcuts", function() {
526 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
527 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
528 equals( close.length, 0, "Context element does not exist, length must be zero" );
529 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
533 jQuery("#check1").click(function() {
534 ok( true, "click event handler for checkbox gets fired twice, see #815" );
538 jQuery('#firstp')[0].onclick = function(event) {
541 jQuery('#firstp').click();
542 equals( counter, 1, "Check that click, triggers onclick event handler also" );
544 var clickCounter = 0;
545 jQuery('#simon1')[0].onclick = function(event) {
548 jQuery('#simon1').click();
549 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
551 jQuery('<img />').load(function(){
552 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
556 test("trigger() bubbling", function() {
559 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
561 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
562 jQuery("html").bind("click", function(e){ html++; });
563 jQuery("body").bind("click", function(e){ body++; });
564 jQuery("#main").bind("click", function(e){ main++; });
565 jQuery("#ap").bind("click", function(){ ap++; return false; });
567 jQuery("html").trigger("click");
568 equals( doc, 1, "HTML bubble" );
569 equals( html, 1, "HTML bubble" );
571 jQuery("body").trigger("click");
572 equals( doc, 2, "Body bubble" );
573 equals( html, 2, "Body bubble" );
574 equals( body, 1, "Body bubble" );
576 jQuery("#main").trigger("click");
577 equals( doc, 3, "Main bubble" );
578 equals( html, 3, "Main bubble" );
579 equals( body, 2, "Main bubble" );
580 equals( main, 1, "Main bubble" );
582 jQuery("#ap").trigger("click");
583 equals( doc, 3, "ap bubble" );
584 equals( html, 3, "ap bubble" );
585 equals( body, 2, "ap bubble" );
586 equals( main, 1, "ap bubble" );
587 equals( ap, 1, "ap bubble" );
590 test("trigger(type, [data], [fn])", function() {
593 var handler = function(event, a, b, c) {
594 equals( event.type, "click", "check passed data" );
595 equals( a, 1, "check passed data" );
596 equals( b, "2", "check passed data" );
597 equals( c, "abc", "check passed data" );
601 var $elem = jQuery("#firstp");
603 // Simulate a "native" click
604 $elem[0].click = function(){
605 ok( true, "Native call was triggered" );
608 // Triggers handlrs and native
610 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
612 // Simulate a "native" click
613 $elem[0].click = function(){
614 ok( false, "Native call was triggered" );
617 // Trigger only the handlers (no native)
619 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
623 jQuery('#form input:first').hide().trigger('focus');
627 ok( pass, "Trigger focus on hidden element" );
631 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
635 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
637 var form = jQuery("<form action=''></form>").appendTo("body");
639 // Make sure it can be prevented locally
640 form.submit(function(){
641 ok( true, "Local bind still works." );
646 form.trigger("submit");
648 form.unbind("submit");
650 jQuery(document).submit(function(){
651 ok( true, "Make sure bubble works up to document." );
656 form.trigger("submit");
658 jQuery(document).unbind("submit");
663 test("jQuery.Event.currentTarget", function(){
666 test("trigger(eventObject, [data], [fn])", function() {
669 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
670 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
672 var event = jQuery.Event("noNew");
673 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
674 equals( event.type, "noNew", "Verify its type" );
676 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
677 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
678 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
680 event.preventDefault();
681 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
682 event.stopPropagation();
683 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
685 event.isPropagationStopped = function(){ return false };
686 event.stopImmediatePropagation();
687 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
688 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
690 $parent.bind('foo',function(e){
692 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
693 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
694 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
695 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
698 // test with an event object
699 event = new jQuery.Event("foo");
700 event.secret = 'boo!';
701 $child.trigger(event);
703 // test with a literal object
704 $child.trigger({type:'foo', secret:'boo!'});
709 ok( false, "This assertion shouldn't be reached");
712 $parent.bind('foo', error );
714 $child.bind('foo',function(e, a, b, c ){
715 equals( arguments.length, 4, "Check arguments length");
716 equals( a, 1, "Check first custom argument");
717 equals( b, 2, "Check second custom argument");
718 equals( c, 3, "Check third custom argument");
720 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
721 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
722 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
725 e.stopImmediatePropagation();
730 // We should add this back in when we want to test the order
731 // in which event handlers are iterated.
732 //$child.bind('foo', error );
734 event = new jQuery.Event("foo");
735 $child.trigger( event, [1,2,3] ).unbind();
736 equals( event.result, "result", "Check event.result attribute");
738 // Will error if it bubbles
739 $child.triggerHandler('foo');
742 $parent.unbind().remove();
745 test("jQuery.Event.currentTarget", function(){
749 $elem = jQuery('<button>a</button>').click(function(e){
750 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
754 $elem.trigger('click');
760 test("toggle(Function, Function, ...)", function() {
764 fn1 = function(e) { count++; },
765 fn2 = function(e) { count--; },
766 preventDefault = function(e) { e.preventDefault() },
767 link = jQuery('#mark');
768 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
769 equals( count, 1, "Check for toggle(fn, fn)" );
771 jQuery("#firstp").toggle(function () {
772 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
773 }, function() {}).trigger("click", [ 1, 2, 3 ]);
776 jQuery("#simon1").one("click", function() {
777 ok( true, "Execute event only once" );
778 jQuery(this).toggle(function() {
779 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
781 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
784 }).click().click().click();
799 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
801 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
803 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
805 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
807 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
809 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
811 $div.unbind('click',fns[0]);
812 var data = jQuery.data( $div[0], 'events' );
813 ok( !data, "Unbinding one function from toggle unbinds them all");
815 // Test Multi-Toggles
817 $div = jQuery("<div/>");
818 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
820 same( a, [1], "Check that a click worked." );
822 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
824 same( a, [1,2], "Check that a click worked with a second toggle." );
825 same( b, [1], "Check that a click worked with a second toggle." );
828 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
829 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
832 test(".live()/.die()", function() {
835 var submit = 0, div = 0, livea = 0, liveb = 0;
837 jQuery("div").live("submit", function(){ submit++; return false; });
838 jQuery("div").live("click", function(){ div++; });
839 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
840 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
842 // Nothing should trigger on the body
843 jQuery("body").trigger("click");
844 equals( submit, 0, "Click on body" );
845 equals( div, 0, "Click on body" );
846 equals( livea, 0, "Click on body" );
847 equals( liveb, 0, "Click on body" );
849 // This should trigger two events
850 submit = 0, div = 0, livea = 0, liveb = 0;
851 jQuery("div#nothiddendiv").trigger("click");
852 equals( submit, 0, "Click on div" );
853 equals( div, 1, "Click on div" );
854 equals( livea, 1, "Click on div" );
855 equals( liveb, 0, "Click on div" );
857 // This should trigger three events (w/ bubbling)
858 submit = 0, div = 0, livea = 0, liveb = 0;
859 jQuery("div#nothiddendivchild").trigger("click");
860 equals( submit, 0, "Click on inner div" );
861 equals( div, 2, "Click on inner div" );
862 equals( livea, 1, "Click on inner div" );
863 equals( liveb, 1, "Click on inner div" );
865 // This should trigger one submit
866 submit = 0, div = 0, livea = 0, liveb = 0;
867 jQuery("div#nothiddendivchild").trigger("submit");
868 equals( submit, 1, "Submit on div" );
869 equals( div, 0, "Submit on div" );
870 equals( livea, 0, "Submit on div" );
871 equals( liveb, 0, "Submit on div" );
873 // Make sure no other events were removed in the process
874 submit = 0, div = 0, livea = 0, liveb = 0;
875 jQuery("div#nothiddendivchild").trigger("click");
876 equals( submit, 0, "die Click on inner div" );
877 equals( div, 2, "die Click on inner div" );
878 equals( livea, 1, "die Click on inner div" );
879 equals( liveb, 1, "die Click on inner div" );
881 // Now make sure that the removal works
882 submit = 0, div = 0, livea = 0, liveb = 0;
883 jQuery("div#nothiddendivchild").die("click");
884 jQuery("div#nothiddendivchild").trigger("click");
885 equals( submit, 0, "die Click on inner div" );
886 equals( div, 2, "die Click on inner div" );
887 equals( livea, 1, "die Click on inner div" );
888 equals( liveb, 0, "die Click on inner div" );
890 // Make sure that the click wasn't removed too early
891 submit = 0, div = 0, livea = 0, liveb = 0;
892 jQuery("div#nothiddendiv").trigger("click");
893 equals( submit, 0, "die Click on inner div" );
894 equals( div, 1, "die Click on inner div" );
895 equals( livea, 1, "die Click on inner div" );
896 equals( liveb, 0, "die Click on inner div" );
898 // Make sure that stopPropgation doesn't stop live events
899 submit = 0, div = 0, livea = 0, liveb = 0;
900 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
901 jQuery("div#nothiddendivchild").trigger("click");
902 equals( submit, 0, "stopPropagation Click on inner div" );
903 equals( div, 1, "stopPropagation Click on inner div" );
904 equals( livea, 0, "stopPropagation Click on inner div" );
905 equals( liveb, 1, "stopPropagation Click on inner div" );
907 // Make sure click events only fire with primary click
908 submit = 0, div = 0, livea = 0, liveb = 0;
909 var event = jQuery.Event("click");
911 jQuery("div#nothiddendiv").trigger(event);
913 equals( livea, 0, "live secondary click" );
915 jQuery("div#nothiddendivchild").die("click");
916 jQuery("div#nothiddendiv").die("click");
917 jQuery("div").die("click");
918 jQuery("div").die("submit");
920 // Test binding with a different context
921 var clicked = 0, container = jQuery('#main')[0];
922 jQuery("#foo", container).live("click", function(e){ clicked++; });
923 jQuery("div").trigger('click');
924 jQuery("#foo").trigger('click');
925 jQuery("#main").trigger('click');
926 jQuery("body").trigger('click');
927 equals( clicked, 2, "live with a context" );
929 // Make sure the event is actually stored on the context
930 ok( jQuery.data(container, "events").live, "live with a context" );
932 // Test unbinding with a different context
933 jQuery("#foo", container).die("click");
934 jQuery("#foo").trigger('click');
935 equals( clicked, 2, "die with a context");
937 // Test binding with event data
938 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
939 jQuery("#foo").trigger("click").die("click");
941 // Test binding with trigger data
942 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
943 jQuery("#foo").trigger("click", true).die("click");
945 // Test binding with different this object
946 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
947 jQuery("#foo").trigger("click").die("click");
949 // Test binding with different this object, event data, and trigger data
950 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
951 equals( e.data, true, "live with with different this object, event data, and trigger data" );
952 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
953 equals( data, true, "live with with different this object, event data, and trigger data")
955 jQuery("#foo").trigger("click", true).die("click");
957 // Verify that return false prevents default action
958 jQuery("#anchor2").live("click", function(){ return false; });
959 var hash = window.location.hash;
960 jQuery("#anchor2").trigger("click");
961 equals( window.location.hash, hash, "return false worked" );
962 jQuery("#anchor2").die("click");
964 // Verify that .preventDefault() prevents default action
965 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
966 var hash = window.location.hash;
967 jQuery("#anchor2").trigger("click");
968 equals( window.location.hash, hash, "e.preventDefault() worked" );
969 jQuery("#anchor2").die("click");
971 // Test binding the same handler to multiple points
973 function callback(){ called++; return false; }
975 jQuery("#nothiddendiv").live("click", callback);
976 jQuery("#anchor2").live("click", callback);
978 jQuery("#nothiddendiv").trigger("click");
979 equals( called, 1, "Verify that only one click occurred." );
981 jQuery("#anchor2").trigger("click");
982 equals( called, 2, "Verify that only one click occurred." );
984 // Make sure that only one callback is removed
985 jQuery("#anchor2").die("click", callback);
987 jQuery("#nothiddendiv").trigger("click");
988 equals( called, 3, "Verify that only one click occurred." );
990 jQuery("#anchor2").trigger("click");
991 equals( called, 3, "Verify that no click occurred." );
993 // Make sure that it still works if the selector is the same,
994 // but the event type is different
995 jQuery("#nothiddendiv").live("foo", callback);
998 jQuery("#nothiddendiv").die("click", callback);
1000 jQuery("#nothiddendiv").trigger("click");
1001 equals( called, 3, "Verify that no click occurred." );
1003 jQuery("#nothiddendiv").trigger("foo");
1004 equals( called, 4, "Verify that one foo occurred." );
1007 jQuery("#nothiddendiv").die("foo", callback);
1009 // Make sure we don't loose the target by DOM modifications
1010 // after the bubble already reached the liveHandler
1011 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1013 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1014 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1016 jQuery("#nothiddendiv span").click();
1017 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1018 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1021 jQuery("#nothiddendivchild").die("click");
1023 // Verify that .live() ocurs and cancel buble in the same order as
1024 // we would expect .bind() and .click() without delegation
1025 var lived = 0, livee = 0;
1027 // bind one pair in one order
1028 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1029 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1031 jQuery('span#liveSpan1 a').click();
1032 equals( lived, 1, "Verify that only one first handler occurred." );
1033 equals( livee, 0, "Verify that second handler doesn't." );
1035 // and one pair in inverse
1036 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1037 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1041 jQuery('span#liveSpan2 a').click();
1042 equals( lived, 1, "Verify that only one first handler occurred." );
1043 equals( livee, 0, "Verify that second handler doesn't." );
1046 jQuery("span#liveSpan1 a").die("click")
1047 jQuery("span#liveSpan1").die("click");
1048 jQuery("span#liveSpan2 a").die("click");
1049 jQuery("span#liveSpan2").die("click");
1051 // Test this, target and currentTarget are correct
1052 jQuery('span#liveSpan1').live('click', function(e){
1053 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1054 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1055 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1058 jQuery('span#liveSpan1 a').click();
1060 jQuery('span#liveSpan1').die('click');
1062 // Work with deep selectors
1065 function clickB(){ livee++; }
1067 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1068 jQuery("#nothiddendiv div").live("click", clickB);
1069 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1071 equals( livee, 0, "No clicks, deep selector." );
1074 jQuery("#nothiddendivchild").trigger("click");
1075 equals( livee, 2, "Click, deep selector." );
1078 jQuery("#nothiddendivchild").trigger("mouseover");
1079 equals( livee, 1, "Mouseover, deep selector." );
1081 jQuery("#nothiddendiv div").die("mouseover");
1084 jQuery("#nothiddendivchild").trigger("click");
1085 equals( livee, 2, "Click, deep selector." );
1088 jQuery("#nothiddendivchild").trigger("mouseover");
1089 equals( livee, 0, "Mouseover, deep selector." );
1091 jQuery("#nothiddendiv div").die("click", clickB);
1094 jQuery("#nothiddendivchild").trigger("click");
1095 equals( livee, 1, "Click, deep selector." );
1097 jQuery("#nothiddendiv div").die("click");
1099 jQuery("#nothiddendiv div").live("blur", function(){
1100 ok( true, "Live div trigger blur." );
1103 jQuery("#nothiddendiv div").trigger("blur");
1105 jQuery("#nothiddendiv div").die("blur");
1108 test("die all bound events", function(){
1112 var div = jQuery("div#nothiddendivchild");
1114 div.live("click submit", function(){ count++; });
1117 div.trigger("click");
1118 div.trigger("submit");
1120 equals( count, 0, "Make sure no events were triggered." );
1123 test("live with multiple events", function(){
1127 var div = jQuery("div#nothiddendivchild");
1129 div.live("click submit", function(){ count++; });
1131 div.trigger("click");
1132 div.trigger("submit");
1134 equals( count, 2, "Make sure both the click and submit were triggered." );
1137 test("live with namespaces", function(){
1140 var count1 = 0, count2 = 0;
1142 jQuery("#liveSpan1").live("foo.bar", function(){
1146 jQuery("#liveSpan2").live("foo.zed", function(){
1150 jQuery("#liveSpan1").trigger("foo.bar");
1151 equals( count1, 1, "Got live foo.bar" );
1153 jQuery("#liveSpan2").trigger("foo.zed");
1154 equals( count2, 1, "Got live foo.zed" );
1157 jQuery("#liveSpan2").die("foo.zed");
1158 jQuery("#liveSpan1").trigger("foo.bar");
1160 equals( count1, 2, "Got live foo.bar after dieing foo.zed" );
1162 jQuery("#liveSpan2").trigger("foo.zed");
1163 equals( count2, 1, "Got live foo.zed" );
1166 jQuery("#liveSpan1").die("foo.bar");
1168 jQuery("#liveSpan1").trigger("foo.bar");
1169 equals( count1, 2, "Did not respond to foo.bar after dieing it" );
1171 jQuery("#liveSpan2").trigger("foo.zed");
1172 equals( count2, 1, "Did not trigger foo.zed again" );
1175 test("live with change", function(){
1176 var selectChange = 0, checkboxChange = 0;
1178 var select = jQuery("select[name='S1']")
1179 select.live("change", function() {
1183 var checkbox = jQuery("#check2"),
1184 checkboxFunction = function(){
1187 checkbox.live("change", checkboxFunction);
1189 // test click on select
1191 // second click that changed it
1193 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1194 select.trigger("change");
1195 equals( selectChange, 1, "Change on click." );
1197 // test keys on select
1199 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1200 select.trigger("change");
1201 equals( selectChange, 1, "Change on keyup." );
1203 // test click on checkbox
1204 checkbox.trigger("change");
1205 equals( checkboxChange, 1, "Change on checkbox." );
1207 // test before activate on radio
1209 // test blur/focus on textarea
1210 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1211 textarea.live("change", function() {
1215 textarea.val(oldVal + "foo");
1216 textarea.trigger("change");
1217 equals( textareaChange, 1, "Change on textarea." );
1219 textarea.val(oldVal);
1220 textarea.die("change");
1222 // test blur/focus on text
1223 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1224 text.live("change", function() {
1228 text.val(oldVal+"foo");
1229 text.trigger("change");
1230 equals( textChange, 1, "Change on text input." );
1232 text.val(oldTextVal);
1235 // test blur/focus on password
1236 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1237 password.live("change", function() {
1241 password.val(oldPasswordVal + "foo");
1242 password.trigger("change");
1243 equals( passwordChange, 1, "Change on password input." );
1245 password.val(oldPasswordVal);
1246 password.die("change");
1248 // make sure die works
1252 select.die("change");
1253 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1254 select.trigger("change");
1255 equals( selectChange, 0, "Die on click works." );
1258 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1259 select.trigger("change");
1260 equals( selectChange, 0, "Die on keyup works." );
1262 // die specific checkbox
1263 checkbox.die("change", checkboxFunction);
1264 checkbox.trigger("change");
1265 equals( checkboxChange, 1, "Die on checkbox." );
1268 test("live with submit", function() {
1269 var count1 = 0, count2 = 0;
1271 jQuery("#testForm").live("submit", function(ev) {
1273 ev.preventDefault();
1276 jQuery("body").live("submit", function(ev) {
1278 ev.preventDefault();
1281 if ( jQuery.support.submitBubbles ) {
1282 jQuery("#testForm input[name=sub1]")[0].click();
1286 jQuery("#testForm input[name=sub1]")[0].click();
1287 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1292 jQuery("#testForm").die("submit");
1293 jQuery("body").die("submit");
1296 test(".delegate()/.undelegate()", function() {
1299 var submit = 0, div = 0, livea = 0, liveb = 0;
1301 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1302 jQuery("#body").delegate("div", "click", function(){ div++; });
1303 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1304 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1306 // Nothing should trigger on the body
1307 jQuery("body").trigger("click");
1308 equals( submit, 0, "Click on body" );
1309 equals( div, 0, "Click on body" );
1310 equals( livea, 0, "Click on body" );
1311 equals( liveb, 0, "Click on body" );
1313 // This should trigger two events
1314 submit = 0, div = 0, livea = 0, liveb = 0;
1315 jQuery("div#nothiddendiv").trigger("click");
1316 equals( submit, 0, "Click on div" );
1317 equals( div, 1, "Click on div" );
1318 equals( livea, 1, "Click on div" );
1319 equals( liveb, 0, "Click on div" );
1321 // This should trigger three events (w/ bubbling)
1322 submit = 0, div = 0, livea = 0, liveb = 0;
1323 jQuery("div#nothiddendivchild").trigger("click");
1324 equals( submit, 0, "Click on inner div" );
1325 equals( div, 2, "Click on inner div" );
1326 equals( livea, 1, "Click on inner div" );
1327 equals( liveb, 1, "Click on inner div" );
1329 // This should trigger one submit
1330 submit = 0, div = 0, livea = 0, liveb = 0;
1331 jQuery("div#nothiddendivchild").trigger("submit");
1332 equals( submit, 1, "Submit on div" );
1333 equals( div, 0, "Submit on div" );
1334 equals( livea, 0, "Submit on div" );
1335 equals( liveb, 0, "Submit on div" );
1337 // Make sure no other events were removed in the process
1338 submit = 0, div = 0, livea = 0, liveb = 0;
1339 jQuery("div#nothiddendivchild").trigger("click");
1340 equals( submit, 0, "undelegate Click on inner div" );
1341 equals( div, 2, "undelegate Click on inner div" );
1342 equals( livea, 1, "undelegate Click on inner div" );
1343 equals( liveb, 1, "undelegate Click on inner div" );
1345 // Now make sure that the removal works
1346 submit = 0, div = 0, livea = 0, liveb = 0;
1347 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1348 jQuery("div#nothiddendivchild").trigger("click");
1349 equals( submit, 0, "undelegate Click on inner div" );
1350 equals( div, 2, "undelegate Click on inner div" );
1351 equals( livea, 1, "undelegate Click on inner div" );
1352 equals( liveb, 0, "undelegate Click on inner div" );
1354 // Make sure that the click wasn't removed too early
1355 submit = 0, div = 0, livea = 0, liveb = 0;
1356 jQuery("div#nothiddendiv").trigger("click");
1357 equals( submit, 0, "undelegate Click on inner div" );
1358 equals( div, 1, "undelegate Click on inner div" );
1359 equals( livea, 1, "undelegate Click on inner div" );
1360 equals( liveb, 0, "undelegate Click on inner div" );
1362 // Make sure that stopPropgation doesn't stop live events
1363 submit = 0, div = 0, livea = 0, liveb = 0;
1364 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1365 jQuery("div#nothiddendivchild").trigger("click");
1366 equals( submit, 0, "stopPropagation Click on inner div" );
1367 equals( div, 1, "stopPropagation Click on inner div" );
1368 equals( livea, 0, "stopPropagation Click on inner div" );
1369 equals( liveb, 1, "stopPropagation Click on inner div" );
1371 // Make sure click events only fire with primary click
1372 submit = 0, div = 0, livea = 0, liveb = 0;
1373 var event = jQuery.Event("click");
1375 jQuery("div#nothiddendiv").trigger(event);
1377 equals( livea, 0, "delegate secondary click" );
1379 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1380 jQuery("#body").undelegate("div#nothiddendiv", "click");
1381 jQuery("#body").undelegate("div", "click");
1382 jQuery("#body").undelegate("div", "submit");
1384 // Test binding with a different context
1385 var clicked = 0, container = jQuery('#main')[0];
1386 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1387 jQuery("div").trigger('click');
1388 jQuery("#foo").trigger('click');
1389 jQuery("#main").trigger('click');
1390 jQuery("body").trigger('click');
1391 equals( clicked, 2, "delegate with a context" );
1393 // Make sure the event is actually stored on the context
1394 ok( jQuery.data(container, "events").live, "delegate with a context" );
1396 // Test unbinding with a different context
1397 jQuery("#main").undelegate("#foo", "click");
1398 jQuery("#foo").trigger('click');
1399 equals( clicked, 2, "undelegate with a context");
1401 // Test binding with event data
1402 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1403 jQuery("#foo").trigger("click");
1404 jQuery("#body").undelegate("#foo", "click");
1406 // Test binding with trigger data
1407 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1408 jQuery("#foo").trigger("click", true);
1409 jQuery("#body").undelegate("#foo", "click");
1411 // Test binding with different this object
1412 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1413 jQuery("#foo").trigger("click");
1414 jQuery("#body").undelegate("#foo", "click");
1416 // Test binding with different this object, event data, and trigger data
1417 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1418 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1419 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1420 equals( data, true, "delegate with with different this object, event data, and trigger data")
1421 }, { foo: "bar" }));
1422 jQuery("#foo").trigger("click", true);
1423 jQuery("#body").undelegate("#foo", "click");
1425 // Verify that return false prevents default action
1426 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1427 var hash = window.location.hash;
1428 jQuery("#anchor2").trigger("click");
1429 equals( window.location.hash, hash, "return false worked" );
1430 jQuery("#body").undelegate("#anchor2", "click");
1432 // Verify that .preventDefault() prevents default action
1433 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1434 var hash = window.location.hash;
1435 jQuery("#anchor2").trigger("click");
1436 equals( window.location.hash, hash, "e.preventDefault() worked" );
1437 jQuery("#body").undelegate("#anchor2", "click");
1439 // Test binding the same handler to multiple points
1441 function callback(){ called++; return false; }
1443 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1444 jQuery("#body").delegate("#anchor2", "click", callback);
1446 jQuery("#nothiddendiv").trigger("click");
1447 equals( called, 1, "Verify that only one click occurred." );
1449 jQuery("#anchor2").trigger("click");
1450 equals( called, 2, "Verify that only one click occurred." );
1452 // Make sure that only one callback is removed
1453 jQuery("#body").undelegate("#anchor2", "click", callback);
1455 jQuery("#nothiddendiv").trigger("click");
1456 equals( called, 3, "Verify that only one click occurred." );
1458 jQuery("#anchor2").trigger("click");
1459 equals( called, 3, "Verify that no click occurred." );
1461 // Make sure that it still works if the selector is the same,
1462 // but the event type is different
1463 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1466 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1468 jQuery("#nothiddendiv").trigger("click");
1469 equals( called, 3, "Verify that no click occurred." );
1471 jQuery("#nothiddendiv").trigger("foo");
1472 equals( called, 4, "Verify that one foo occurred." );
1475 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1477 // Make sure we don't loose the target by DOM modifications
1478 // after the bubble already reached the liveHandler
1479 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1481 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1482 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1484 jQuery("#nothiddendiv span").click();
1485 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1486 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1489 jQuery("#body").undelegate("#nothiddendivchild", "click");
1491 // Verify that .live() ocurs and cancel buble in the same order as
1492 // we would expect .bind() and .click() without delegation
1493 var lived = 0, livee = 0;
1495 // bind one pair in one order
1496 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1497 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1499 jQuery('span#liveSpan1 a').click();
1500 equals( lived, 1, "Verify that only one first handler occurred." );
1501 equals( livee, 0, "Verify that second handler doesn't." );
1503 // and one pair in inverse
1504 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1505 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1509 jQuery('span#liveSpan2 a').click();
1510 equals( lived, 1, "Verify that only one first handler occurred." );
1511 equals( livee, 0, "Verify that second handler doesn't." );
1514 jQuery("#body").undelegate("click");
1516 // Test this, target and currentTarget are correct
1517 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1518 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1519 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1520 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1523 jQuery('span#liveSpan1 a').click();
1525 jQuery("#body").undelegate('span#liveSpan1', 'click');
1527 // Work with deep selectors
1530 function clickB(){ livee++; }
1532 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1533 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1534 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1536 equals( livee, 0, "No clicks, deep selector." );
1539 jQuery("#nothiddendivchild").trigger("click");
1540 equals( livee, 2, "Click, deep selector." );
1543 jQuery("#nothiddendivchild").trigger("mouseover");
1544 equals( livee, 1, "Mouseover, deep selector." );
1546 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1549 jQuery("#nothiddendivchild").trigger("click");
1550 equals( livee, 2, "Click, deep selector." );
1553 jQuery("#nothiddendivchild").trigger("mouseover");
1554 equals( livee, 0, "Mouseover, deep selector." );
1556 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1559 jQuery("#nothiddendivchild").trigger("click");
1560 equals( livee, 1, "Click, deep selector." );
1562 jQuery("#body").undelegate("#nothiddendiv div", "click");
1565 test("undelegate all bound events", function(){
1569 var div = jQuery("#body");
1571 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1574 jQuery("div#nothiddendivchild").trigger("click");
1575 jQuery("div#nothiddendivchild").trigger("submit");
1577 equals( count, 0, "Make sure no events were triggered." );
1580 test("delegate with multiple events", function(){
1584 var div = jQuery("#body");
1586 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1588 jQuery("div#nothiddendivchild").trigger("click");
1589 jQuery("div#nothiddendivchild").trigger("submit");
1591 equals( count, 2, "Make sure both the click and submit were triggered." );
1593 jQuery("#body").undelegate();
1596 test("delegate with change", function(){
1597 var selectChange = 0, checkboxChange = 0;
1599 var select = jQuery("select[name='S1']");
1600 jQuery("#body").delegate("select[name='S1']", "change", function() {
1604 var checkbox = jQuery("#check2"),
1605 checkboxFunction = function(){
1608 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1610 // test click on select
1612 // second click that changed it
1614 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1615 select.trigger("change");
1616 equals( selectChange, 1, "Change on click." );
1618 // test keys on select
1620 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1621 select.trigger("change");
1622 equals( selectChange, 1, "Change on keyup." );
1624 // test click on checkbox
1625 checkbox.trigger("change");
1626 equals( checkboxChange, 1, "Change on checkbox." );
1628 // test before activate on radio
1630 // test blur/focus on textarea
1631 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1632 jQuery("#body").delegate("#area1", "change", function() {
1636 textarea.val(oldVal + "foo");
1637 textarea.trigger("change");
1638 equals( textareaChange, 1, "Change on textarea." );
1640 textarea.val(oldVal);
1641 jQuery("#body").undelegate("#area1", "change");
1643 // test blur/focus on text
1644 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1645 jQuery("#body").delegate("#name", "change", function() {
1649 text.val(oldVal+"foo");
1650 text.trigger("change");
1651 equals( textChange, 1, "Change on text input." );
1653 text.val(oldTextVal);
1654 jQuery("#body").die("change");
1656 // test blur/focus on password
1657 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1658 jQuery("#body").delegate("#name", "change", function() {
1662 password.val(oldPasswordVal + "foo");
1663 password.trigger("change");
1664 equals( passwordChange, 1, "Change on password input." );
1666 password.val(oldPasswordVal);
1667 jQuery("#body").undelegate("#name", "change");
1669 // make sure die works
1673 jQuery("#body").undelegate("select[name='S1']", "change");
1674 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1675 select.trigger("change");
1676 equals( selectChange, 0, "Die on click works." );
1679 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1680 select.trigger("change");
1681 equals( selectChange, 0, "Die on keyup works." );
1683 // die specific checkbox
1684 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1685 checkbox.trigger("change");
1686 equals( checkboxChange, 1, "Die on checkbox." );
1689 test("delegate with submit", function() {
1690 var count1 = 0, count2 = 0;
1692 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1694 ev.preventDefault();
1697 jQuery(document).delegate("body", "submit", function(ev) {
1699 ev.preventDefault();
1702 if ( jQuery.support.submitBubbles ) {
1703 jQuery("#testForm input[name=sub1]")[0].click();
1707 jQuery("#testForm input[name=sub1]")[0].click();
1708 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1713 jQuery("#body").undelegate();
1714 jQuery(document).undelegate();
1717 test("Non DOM element events", function() {
1721 .bind('nonelementglobal', function(e) {
1722 ok( true, "Global event on non-DOM annonymos object triggered" );
1728 .bind('nonelementobj', function(e) {
1729 ok( true, "Event on non-DOM object triggered" );
1730 }).bind('nonelementglobal', function() {
1731 ok( true, "Global event on non-DOM object triggered" );
1734 jQuery(o).trigger('nonelementobj');
1735 jQuery.event.trigger('nonelementglobal');
1739 test("jQuery(function($) {})", function() {
1741 jQuery(function($) {
1742 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");
1747 test("event properties", function() {
1749 jQuery("#simon1").click(function(event) {
1750 ok( event.timeStamp, "assert event.timeStamp is present" );