3 test("bind(), with data", function() {
5 var handler = function(event) {
6 ok( event.data, "bind() with data, check passed data exists" );
7 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
9 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
11 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
14 test("bind(), with data, trigger with data", function() {
16 var handler = function(event, data) {
17 ok( event.data, "check passed data exists" );
18 equals( event.data.foo, "bar", "Check value of passed data" );
19 ok( data, "Check trigger data" );
20 equals( data.bar, "foo", "Check value of trigger data" );
22 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
25 test("bind(), multiple events at once", function() {
29 var handler = function(event) {
30 if (event.type == "click")
32 else if (event.type == "mouseover")
33 mouseoverCounter += 1;
35 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
36 equals( clickCounter, 1, "bind() with multiple events at once" );
37 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
40 test("bind(), multiple events at once and namespaces", function() {
45 var div = jQuery("<div/>").bind("focusin.a", function(e) {
46 equals( e.type, cur, "Verify right single event was fired." );
50 div.trigger("focusin.a");
52 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
53 equals( e.type, cur, "Verify right multi event was fired." );
54 equals( e.data, obj, "Make sure the data came in correctly." );
61 div.trigger("mouseover");
63 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
64 equals( e.type, cur, "Verify right multi event was fired." );
68 div.trigger("focusin.a");
71 div.trigger("focusout.b");
74 test("bind(), namespace with special add", function() {
77 var div = jQuery("<div/>").bind("test", function(e) {
78 ok( true, "Test event fired." );
83 jQuery.event.special.test = {
84 _default: function(e) {
85 equals( this, document, "Make sure we're at the top of the chain." );
86 equals( e.type, "test", "And that we're still dealing with a test event." );
87 equals( e.target, div[0], "And that the target is correct." );
90 teardown: function(){},
91 add: function( handler, data, namespaces ) {
94 handler.apply( this, arguments );
100 div.bind("test.a", {x: 1}, function(e) {
101 ok( !!e.xyz, "Make sure that the data is getting passed through." );
102 equals( e.data.x, 1, "Make sure data is attached properly." );
105 div.bind("test.b", {x: 2}, function(e) {
106 ok( !!e.xyz, "Make sure that the data is getting passed through." );
107 equals( e.data.x, 2, "Make sure data is attached properly." );
114 div.trigger("test.a");
117 div.trigger("test.b");
120 test("bind(), no data", function() {
122 var handler = function(event) {
123 ok ( !event.data, "Check that no data is added to the event object" );
125 jQuery("#firstp").bind("click", handler).trigger("click");
128 test("bind/one/unbind(Object)", function(){
131 var clickCounter = 0, mouseoverCounter = 0;
132 function handler(event) {
133 if (event.type == "click")
135 else if (event.type == "mouseover")
139 function handlerWithData(event) {
140 if (event.type == "click")
141 clickCounter += event.data;
142 else if (event.type == "mouseover")
143 mouseoverCounter += event.data;
147 $elem.trigger("click").trigger("mouseover");
150 var $elem = jQuery("#firstp")
158 click:handlerWithData,
159 mouseover:handlerWithData
164 equals( clickCounter, 3, "bind(Object)" );
165 equals( mouseoverCounter, 3, "bind(Object)" );
168 equals( clickCounter, 4, "bind(Object)" );
169 equals( mouseoverCounter, 4, "bind(Object)" );
171 jQuery("#firstp").unbind({
177 equals( clickCounter, 4, "bind(Object)" );
178 equals( mouseoverCounter, 4, "bind(Object)" );
181 test("bind(), iframes", function() {
182 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
183 var doc = jQuery("#loadediframe").contents();
185 jQuery("div", doc).bind("click", function() {
186 ok( true, "Binding to element inside iframe" );
187 }).click().unbind('click');
190 test("bind(), trigger change on select", function() {
193 function selectOnChange(event) {
194 equals( event.data, counter++, "Event.data is not a global event object" );
196 jQuery("#form select").each(function(i){
197 jQuery(this).bind('change', i, selectOnChange);
198 }).trigger('change');
201 test("bind(), namespaced events, cloned events", function() {
204 jQuery("#firstp").bind("custom.test",function(e){
205 ok(true, "Custom event triggered");
208 jQuery("#firstp").bind("click",function(e){
209 ok(true, "Normal click triggered");
212 jQuery("#firstp").bind("click.test",function(e){
213 ok(true, "Namespaced click triggered");
216 // Trigger both bound fn (2)
217 jQuery("#firstp").trigger("click");
219 // Trigger one bound fn (1)
220 jQuery("#firstp").trigger("click.test");
222 // Remove only the one fn
223 jQuery("#firstp").unbind("click.test");
225 // Trigger the remaining fn (1)
226 jQuery("#firstp").trigger("click");
228 // Remove the remaining fn
229 jQuery("#firstp").unbind(".test");
231 // Trigger the remaining fn (0)
232 jQuery("#firstp").trigger("custom");
234 // using contents will get comments regular, text, and comment nodes
235 jQuery("#nonnodes").contents().bind("tester", function () {
236 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
237 }).trigger("tester");
239 // Make sure events stick with appendTo'd elements (which are cloned) #2027
240 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
241 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
244 test("bind(), multi-namespaced events", function() {
256 function check(name, msg){
257 same(name, order.shift(), msg);
260 jQuery("#firstp").bind("custom.test",function(e){
261 check("custom.test", "Custom event triggered");
264 jQuery("#firstp").bind("custom.test2",function(e){
265 check("custom.test2", "Custom event triggered");
268 jQuery("#firstp").bind("click.test",function(e){
269 check("click.test", "Normal click triggered");
272 jQuery("#firstp").bind("click.test.abc",function(e){
273 check("click.test.abc", "Namespaced click triggered");
276 // Those would not trigger/unbind (#5303)
277 jQuery("#firstp").trigger("click.a.test");
278 jQuery("#firstp").unbind("click.a.test");
280 // Trigger both bound fn (1)
281 jQuery("#firstp").trigger("click.test.abc");
283 // Trigger one bound fn (1)
284 jQuery("#firstp").trigger("click.abc");
286 // Trigger two bound fn (2)
287 jQuery("#firstp").trigger("click.test");
289 // Remove only the one fn
290 jQuery("#firstp").unbind("click.abc");
292 // Trigger the remaining fn (1)
293 jQuery("#firstp").trigger("click");
295 // Remove the remaining fn
296 jQuery("#firstp").unbind(".test");
298 // Trigger the remaining fn (1)
299 jQuery("#firstp").trigger("custom");
302 test("bind(), with different this object", function() {
304 var thisObject = { myThis: true },
305 data = { myData: true },
306 handler1 = function( event ) {
307 equals( this, thisObject, "bind() with different this object" );
309 handler2 = function( event ) {
310 equals( this, thisObject, "bind() with different this object and data" );
311 equals( event.data, data, "bind() with different this object and data" );
315 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
316 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
318 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
321 test("unbind(type)", function() {
324 var $elem = jQuery("#firstp"),
328 ok( false, message );
331 message = "unbind passing function";
332 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
334 message = "unbind all from event";
335 $elem.bind('error', error).unbind('error').triggerHandler('error');
337 message = "unbind all";
338 $elem.bind('error', error).unbind().triggerHandler('error');
340 message = "unbind many with function";
341 $elem.bind('error error2',error)
342 .unbind('error error2', error )
343 .trigger('error').triggerHandler('error2');
345 message = "unbind many"; // #3538
346 $elem.bind('error error2',error)
347 .unbind('error error2')
348 .trigger('error').triggerHandler('error2');
350 message = "unbind without a type or handler";
351 $elem.bind("error error2.test",error)
353 .trigger("error").triggerHandler("error2");
356 test("unbind(eventObject)", function() {
359 var $elem = jQuery("#firstp"),
362 function assert( expected ){
364 $elem.trigger('foo').triggerHandler('bar');
365 equals( num, expected, "Check the right handlers are triggered" );
369 // This handler shouldn't be unbound
370 .bind('foo', function(){
373 .bind('foo', function(e){
378 .bind('bar', function(){
392 test("hover()", function() {
394 handler1 = function( event ) { ++times; },
395 handler2 = function( event ) { ++times; };
398 .hover(handler1, handler2)
399 .mouseenter().mouseleave()
400 .unbind("mouseenter", handler1)
401 .unbind("mouseleave", handler2)
403 .mouseenter().mouseleave()
404 .unbind("mouseenter mouseleave", handler1)
405 .mouseenter().mouseleave();
407 equals( times, 4, "hover handlers fired" );
410 test("trigger() shortcuts", function() {
412 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
413 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
414 equals( close.length, 0, "Context element does not exist, length must be zero" );
415 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
419 jQuery("#check1").click(function() {
420 ok( true, "click event handler for checkbox gets fired twice, see #815" );
424 jQuery('#firstp')[0].onclick = function(event) {
427 jQuery('#firstp').click();
428 equals( counter, 1, "Check that click, triggers onclick event handler also" );
430 var clickCounter = 0;
431 jQuery('#simon1')[0].onclick = function(event) {
434 jQuery('#simon1').click();
435 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
437 jQuery('<img />').load(function(){
438 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
442 test("trigger() bubbling", function() {
445 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
447 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
448 jQuery("html").bind("click", function(e){ html++; });
449 jQuery("body").bind("click", function(e){ body++; });
450 jQuery("#main").bind("click", function(e){ main++; });
451 jQuery("#ap").bind("click", function(){ ap++; return false; });
453 jQuery("html").trigger("click");
454 equals( doc, 1, "HTML bubble" );
455 equals( html, 1, "HTML bubble" );
457 jQuery("body").trigger("click");
458 equals( doc, 2, "Body bubble" );
459 equals( html, 2, "Body bubble" );
460 equals( body, 1, "Body bubble" );
462 jQuery("#main").trigger("click");
463 equals( doc, 3, "Main bubble" );
464 equals( html, 3, "Main bubble" );
465 equals( body, 2, "Main bubble" );
466 equals( main, 1, "Main bubble" );
468 jQuery("#ap").trigger("click");
469 equals( doc, 3, "ap bubble" );
470 equals( html, 3, "ap bubble" );
471 equals( body, 2, "ap bubble" );
472 equals( main, 1, "ap bubble" );
473 equals( ap, 1, "ap bubble" );
476 test("trigger(type, [data], [fn])", function() {
479 var handler = function(event, a, b, c) {
480 equals( event.type, "click", "check passed data" );
481 equals( a, 1, "check passed data" );
482 equals( b, "2", "check passed data" );
483 equals( c, "abc", "check passed data" );
487 var $elem = jQuery("#firstp");
489 // Simulate a "native" click
490 $elem[0].click = function(){
491 ok( true, "Native call was triggered" );
494 // Triggers handlrs and native
496 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
498 // Simulate a "native" click
499 $elem[0].click = function(){
500 ok( false, "Native call was triggered" );
503 // Trigger only the handlers (no native)
505 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
509 jQuery('#form input:first').hide().trigger('focus');
513 ok( pass, "Trigger focus on hidden element" );
517 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
521 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
523 var form = jQuery("<form action=''></form>").appendTo("body");
525 // Make sure it can be prevented locally
526 form.submit(function(){
527 ok( true, "Local bind still works." );
532 form.trigger("submit");
534 form.unbind("submit");
536 jQuery(document).submit(function(){
537 ok( true, "Make sure bubble works up to document." );
542 form.trigger("submit");
544 jQuery(document).unbind("submit");
549 test("jQuery.Event.currentTarget", function(){
552 test("trigger(eventObject, [data], [fn])", function() {
555 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
556 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
558 var event = jQuery.Event("noNew");
559 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
560 equals( event.type, "noNew", "Verify its type" );
562 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
563 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
564 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
566 event.preventDefault();
567 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
568 event.stopPropagation();
569 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
571 event.isPropagationStopped = function(){ return false };
572 event.stopImmediatePropagation();
573 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
574 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
576 $parent.bind('foo',function(e){
578 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
579 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
580 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
581 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
584 // test with an event object
585 event = new jQuery.Event("foo");
586 event.secret = 'boo!';
587 $child.trigger(event);
589 // test with a literal object
590 $child.trigger({type:'foo', secret:'boo!'});
595 ok( false, "This assertion shouldn't be reached");
598 $parent.bind('foo', error );
600 $child.bind('foo',function(e, a, b, c ){
601 equals( arguments.length, 4, "Check arguments length");
602 equals( a, 1, "Check first custom argument");
603 equals( b, 2, "Check second custom argument");
604 equals( c, 3, "Check third custom argument");
606 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
607 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
608 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
611 e.stopImmediatePropagation();
616 // We should add this back in when we want to test the order
617 // in which event handlers are iterated.
618 //$child.bind('foo', error );
620 event = new jQuery.Event("foo");
621 $child.trigger( event, [1,2,3] ).unbind();
622 equals( event.result, "result", "Check event.result attribute");
624 // Will error if it bubbles
625 $child.triggerHandler('foo');
628 $parent.unbind().remove();
631 test("jQuery.Event.currentTarget", function(){
635 $elem = jQuery('<button>a</button>').click(function(e){
636 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
640 $elem.trigger('click');
646 test("toggle(Function, Function, ...)", function() {
650 fn1 = function(e) { count++; },
651 fn2 = function(e) { count--; },
652 preventDefault = function(e) { e.preventDefault() },
653 link = jQuery('#mark');
654 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
655 equals( count, 1, "Check for toggle(fn, fn)" );
657 jQuery("#firstp").toggle(function () {
658 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
659 }, function() {}).trigger("click", [ 1, 2, 3 ]);
662 jQuery("#simon1").one("click", function() {
663 ok( true, "Execute event only once" );
664 jQuery(this).toggle(function() {
665 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
667 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
670 }).click().click().click();
685 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
687 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
689 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
691 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
693 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
695 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
697 $div.unbind('click',fns[0]);
698 var data = jQuery.data( $div[0], 'events' );
699 ok( !data, "Unbinding one function from toggle unbinds them all");
701 // Test Multi-Toggles
703 $div = jQuery("<div/>");
704 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
706 same( a, [1], "Check that a click worked." );
708 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
710 same( a, [1,2], "Check that a click worked with a second toggle." );
711 same( b, [1], "Check that a click worked with a second toggle." );
714 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
715 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
718 test(".live()/.die()", function() {
721 var submit = 0, div = 0, livea = 0, liveb = 0;
723 jQuery("div").live("submit", function(){ submit++; return false; });
724 jQuery("div").live("click", function(){ div++; });
725 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
726 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
728 // Nothing should trigger on the body
729 jQuery("body").trigger("click");
730 equals( submit, 0, "Click on body" );
731 equals( div, 0, "Click on body" );
732 equals( livea, 0, "Click on body" );
733 equals( liveb, 0, "Click on body" );
735 // This should trigger two events
736 jQuery("div#nothiddendiv").trigger("click");
737 equals( submit, 0, "Click on div" );
738 equals( div, 1, "Click on div" );
739 equals( livea, 1, "Click on div" );
740 equals( liveb, 0, "Click on div" );
742 // This should trigger three events (w/ bubbling)
743 jQuery("div#nothiddendivchild").trigger("click");
744 equals( submit, 0, "Click on inner div" );
745 equals( div, 2, "Click on inner div" );
746 equals( livea, 2, "Click on inner div" );
747 equals( liveb, 1, "Click on inner div" );
749 // This should trigger one submit
750 jQuery("div#nothiddendivchild").trigger("submit");
751 equals( submit, 1, "Submit on div" );
752 equals( div, 2, "Submit on div" );
753 equals( livea, 2, "Submit on div" );
754 equals( liveb, 1, "Submit on div" );
756 // Make sure no other events were removed in the process
757 jQuery("div#nothiddendivchild").trigger("click");
758 equals( submit, 1, "die Click on inner div" );
759 equals( div, 3, "die Click on inner div" );
760 equals( livea, 3, "die Click on inner div" );
761 equals( liveb, 2, "die Click on inner div" );
763 // Now make sure that the removal works
764 jQuery("div#nothiddendivchild").die("click");
765 jQuery("div#nothiddendivchild").trigger("click");
766 equals( submit, 1, "die Click on inner div" );
767 equals( div, 4, "die Click on inner div" );
768 equals( livea, 4, "die Click on inner div" );
769 equals( liveb, 2, "die Click on inner div" );
771 // Make sure that the click wasn't removed too early
772 jQuery("div#nothiddendiv").trigger("click");
773 equals( submit, 1, "die Click on inner div" );
774 equals( div, 5, "die Click on inner div" );
775 equals( livea, 5, "die Click on inner div" );
776 equals( liveb, 2, "die Click on inner div" );
778 // Make sure that stopPropgation doesn't stop live events
779 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
780 jQuery("div#nothiddendivchild").trigger("click");
781 equals( submit, 1, "stopPropagation Click on inner div" );
782 equals( div, 6, "stopPropagation Click on inner div" );
783 equals( livea, 6, "stopPropagation Click on inner div" );
784 equals( liveb, 3, "stopPropagation Click on inner div" );
786 // Make sure click events only fire with primary click
787 var event = jQuery.Event("click");
789 jQuery("div#nothiddendiv").trigger(event);
791 equals( livea, 6, "live secondary click" );
793 jQuery("div#nothiddendivchild").die("click");
794 jQuery("div#nothiddendiv").die("click");
795 jQuery("div").die("click");
796 jQuery("div").die("submit");
798 // Test binding with a different context
799 var clicked = 0, container = jQuery('#main')[0];
800 jQuery("#foo", container).live("click", function(e){ clicked++; });
801 jQuery("div").trigger('click');
802 jQuery("#foo").trigger('click');
803 jQuery("#main").trigger('click');
804 jQuery("body").trigger('click');
805 equals( clicked, 2, "live with a context" );
807 // Make sure the event is actually stored on the context
808 ok( jQuery.data(container, "events").live, "live with a context" );
810 // Test unbinding with a different context
811 jQuery("#foo", container).die("click");
812 jQuery("#foo").trigger('click');
813 equals( clicked, 2, "die with a context");
815 // Test binding with event data
816 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
817 jQuery("#foo").trigger("click").die("click");
819 // Test binding with trigger data
820 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
821 jQuery("#foo").trigger("click", true).die("click");
823 // Test binding with different this object
824 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
825 jQuery("#foo").trigger("click").die("click");
827 // Test binding with different this object, event data, and trigger data
828 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
829 equals( e.data, true, "live with with different this object, event data, and trigger data" );
830 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
831 equals( data, true, "live with with different this object, event data, and trigger data")
833 jQuery("#foo").trigger("click", true).die("click");
835 // Verify that return false prevents default action
836 jQuery("#anchor2").live("click", function(){ return false; });
837 var hash = window.location.hash;
838 jQuery("#anchor2").trigger("click");
839 equals( window.location.hash, hash, "return false worked" );
840 jQuery("#anchor2").die("click");
842 // Verify that .preventDefault() prevents default action
843 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
844 var hash = window.location.hash;
845 jQuery("#anchor2").trigger("click");
846 equals( window.location.hash, hash, "e.preventDefault() worked" );
847 jQuery("#anchor2").die("click");
849 // Test binding the same handler to multiple points
851 function callback(){ called++; return false; }
853 jQuery("#nothiddendiv").live("click", callback);
854 jQuery("#anchor2").live("click", callback);
856 jQuery("#nothiddendiv").trigger("click");
857 equals( called, 1, "Verify that only one click occurred." );
859 jQuery("#anchor2").trigger("click");
860 equals( called, 2, "Verify that only one click occurred." );
862 // Make sure that only one callback is removed
863 jQuery("#anchor2").die("click", callback);
865 jQuery("#nothiddendiv").trigger("click");
866 equals( called, 3, "Verify that only one click occurred." );
868 jQuery("#anchor2").trigger("click");
869 equals( called, 3, "Verify that no click occurred." );
871 // Make sure that it still works if the selector is the same,
872 // but the event type is different
873 jQuery("#nothiddendiv").live("foo", callback);
876 jQuery("#nothiddendiv").die("click", callback);
878 jQuery("#nothiddendiv").trigger("click");
879 equals( called, 3, "Verify that no click occurred." );
881 jQuery("#nothiddendiv").trigger("foo");
882 equals( called, 4, "Verify that one foo occurred." );
885 jQuery("#nothiddendiv").die("foo", callback);
887 // Make sure we don't loose the target by DOM modifications
888 // after the bubble already reached the liveHandler
889 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
891 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
892 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
894 jQuery("#nothiddendiv span").click();
895 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
896 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
899 jQuery("#nothiddendivchild").die("click");
901 // Verify that .live() ocurs and cancel buble in the same order as
902 // we would expect .bind() and .click() without delegation
903 var lived = 0, livee = 0;
905 // bind one pair in one order
906 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
907 jQuery('span#liveSpan1').live('click', function(){ livee++; });
909 jQuery('span#liveSpan1 a').click();
910 equals( lived, 1, "Verify that only one first handler occurred." );
911 equals( livee, 0, "Verify that second handler doesn't." );
913 // and one pair in inverse
914 jQuery('span#liveSpan2').live('click', function(){ livee++; });
915 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
919 jQuery('span#liveSpan2 a').click();
920 equals( lived, 1, "Verify that only one first handler occurred." );
921 equals( livee, 0, "Verify that second handler doesn't." );
924 jQuery("span#liveSpan1 a").die("click")
925 jQuery("span#liveSpan1").die("click");
926 jQuery("span#liveSpan2 a").die("click");
927 jQuery("span#liveSpan2").die("click");
929 // Test this, target and currentTarget are correct
930 jQuery('span#liveSpan1').live('click', function(e){
931 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
932 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
933 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
936 jQuery('span#liveSpan1 a').click();
938 jQuery('span#liveSpan1').die('click');
940 // Work with deep selectors
943 function clickB(){ livee++; }
945 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
946 jQuery("#nothiddendiv div").live("click", clickB);
947 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
949 equals( livee, 0, "No clicks, deep selector." );
952 jQuery("#nothiddendivchild").trigger("click");
953 equals( livee, 2, "Click, deep selector." );
956 jQuery("#nothiddendivchild").trigger("mouseover");
957 equals( livee, 1, "Mouseover, deep selector." );
959 jQuery("#nothiddendiv div").die("mouseover");
962 jQuery("#nothiddendivchild").trigger("click");
963 equals( livee, 2, "Click, deep selector." );
966 jQuery("#nothiddendivchild").trigger("mouseover");
967 equals( livee, 0, "Mouseover, deep selector." );
969 jQuery("#nothiddendiv div").die("click", clickB);
972 jQuery("#nothiddendivchild").trigger("click");
973 equals( livee, 1, "Click, deep selector." );
975 jQuery("#nothiddendiv div").die("click");
978 test("die all bound events", function(){
982 var div = jQuery("div#nothiddendivchild");
984 div.live("click submit", function(){ count++; });
987 div.trigger("click");
988 div.trigger("submit");
990 equals( count, 0, "Make sure no events were triggered." );
993 test("live with multiple events", function(){
997 var div = jQuery("div#nothiddendivchild");
999 div.live("click submit", function(){ count++; });
1001 div.trigger("click");
1002 div.trigger("submit");
1004 equals( count, 2, "Make sure both the click and submit were triggered." );
1007 test("live with change", function(){
1008 var selectChange = 0, checkboxChange = 0;
1010 var select = jQuery("select[name='S1']")
1011 select.live("change", function() {
1015 var checkbox = jQuery("#check2"),
1016 checkboxFunction = function(){
1019 checkbox.live("change", checkboxFunction);
1021 // test click on select
1023 // second click that changed it
1025 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1026 select.trigger("change");
1027 equals( selectChange, 1, "Change on click." );
1029 // test keys on select
1031 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1032 select.trigger("change");
1033 equals( selectChange, 1, "Change on keyup." );
1035 // test click on checkbox
1036 checkbox.trigger("change");
1037 equals( checkboxChange, 1, "Change on checkbox." );
1039 // test before activate on radio
1041 // test blur/focus on textarea
1042 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1043 textarea.live("change", function() {
1047 textarea.val(oldVal + "foo");
1048 textarea.trigger("change");
1049 equals( textareaChange, 1, "Change on textarea." );
1051 textarea.val(oldVal);
1052 textarea.die("change");
1054 // test blur/focus on text
1055 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1056 text.live("change", function() {
1060 text.val(oldVal+"foo");
1061 text.trigger("change");
1062 equals( textChange, 1, "Change on text input." );
1064 text.val(oldTextVal);
1067 // test blur/focus on password
1068 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1069 password.live("change", function() {
1073 password.val(oldPasswordVal + "foo");
1074 password.trigger("change");
1075 equals( passwordChange, 1, "Change on password input." );
1077 password.val(oldPasswordVal);
1078 password.die("change");
1080 // make sure die works
1084 select.die("change");
1085 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1086 select.trigger("change");
1087 equals( selectChange, 0, "Die on click works." );
1090 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1091 select.trigger("change");
1092 equals( selectChange, 0, "Die on keyup works." );
1094 // die specific checkbox
1095 checkbox.die("change", checkboxFunction);
1096 checkbox.trigger("change");
1097 equals( checkboxChange, 1, "Die on checkbox." );
1100 test("live with submit", function() {
1101 var count1 = 0, count2 = 0;
1103 jQuery("#testForm").live("submit", function(ev) {
1105 ev.preventDefault();
1108 jQuery("body").live("submit", function(ev) {
1110 ev.preventDefault();
1113 if ( jQuery.support.submitBubbles ) {
1114 jQuery("#testForm input[name=sub1]")[0].click();
1118 jQuery("#testForm input[name=sub1]")[0].click();
1119 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1124 jQuery("#testForm").die("submit");
1125 jQuery("body").die("submit");
1128 test(".delegate()/.undelegate()", function() {
1131 var submit = 0, div = 0, livea = 0, liveb = 0;
1133 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1134 jQuery("#body").delegate("div", "click", function(){ div++; });
1135 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1136 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1138 // Nothing should trigger on the body
1139 jQuery("body").trigger("click");
1140 equals( submit, 0, "Click on body" );
1141 equals( div, 0, "Click on body" );
1142 equals( livea, 0, "Click on body" );
1143 equals( liveb, 0, "Click on body" );
1145 // This should trigger two events
1146 jQuery("div#nothiddendiv").trigger("click");
1147 equals( submit, 0, "Click on div" );
1148 equals( div, 1, "Click on div" );
1149 equals( livea, 1, "Click on div" );
1150 equals( liveb, 0, "Click on div" );
1152 // This should trigger three events (w/ bubbling)
1153 jQuery("div#nothiddendivchild").trigger("click");
1154 equals( submit, 0, "Click on inner div" );
1155 equals( div, 2, "Click on inner div" );
1156 equals( livea, 2, "Click on inner div" );
1157 equals( liveb, 1, "Click on inner div" );
1159 // This should trigger one submit
1160 jQuery("div#nothiddendivchild").trigger("submit");
1161 equals( submit, 1, "Submit on div" );
1162 equals( div, 2, "Submit on div" );
1163 equals( livea, 2, "Submit on div" );
1164 equals( liveb, 1, "Submit on div" );
1166 // Make sure no other events were removed in the process
1167 jQuery("div#nothiddendivchild").trigger("click");
1168 equals( submit, 1, "undelegate Click on inner div" );
1169 equals( div, 3, "undelegate Click on inner div" );
1170 equals( livea, 3, "undelegate Click on inner div" );
1171 equals( liveb, 2, "undelegate Click on inner div" );
1173 // Now make sure that the removal works
1174 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1175 jQuery("div#nothiddendivchild").trigger("click");
1176 equals( submit, 1, "undelegate Click on inner div" );
1177 equals( div, 4, "undelegate Click on inner div" );
1178 equals( livea, 4, "undelegate Click on inner div" );
1179 equals( liveb, 2, "undelegate Click on inner div" );
1181 // Make sure that the click wasn't removed too early
1182 jQuery("div#nothiddendiv").trigger("click");
1183 equals( submit, 1, "undelegate Click on inner div" );
1184 equals( div, 5, "undelegate Click on inner div" );
1185 equals( livea, 5, "undelegate Click on inner div" );
1186 equals( liveb, 2, "undelegate Click on inner div" );
1188 // Make sure that stopPropgation doesn't stop live events
1189 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1190 jQuery("div#nothiddendivchild").trigger("click");
1191 equals( submit, 1, "stopPropagation Click on inner div" );
1192 equals( div, 6, "stopPropagation Click on inner div" );
1193 equals( livea, 6, "stopPropagation Click on inner div" );
1194 equals( liveb, 3, "stopPropagation Click on inner div" );
1196 // Make sure click events only fire with primary click
1197 var event = jQuery.Event("click");
1199 jQuery("div#nothiddendiv").trigger(event);
1201 equals( livea, 6, "delegate secondary click" );
1203 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1204 jQuery("#body").undelegate("div#nothiddendiv", "click");
1205 jQuery("#body").undelegate("div", "click");
1206 jQuery("#body").undelegate("div", "submit");
1208 // Test binding with a different context
1209 var clicked = 0, container = jQuery('#main')[0];
1210 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1211 jQuery("div").trigger('click');
1212 jQuery("#foo").trigger('click');
1213 jQuery("#main").trigger('click');
1214 jQuery("body").trigger('click');
1215 equals( clicked, 2, "delegate with a context" );
1217 // Make sure the event is actually stored on the context
1218 ok( jQuery.data(container, "events").live, "delegate with a context" );
1220 // Test unbinding with a different context
1221 jQuery("#main").undelegate("#foo", "click");
1222 jQuery("#foo").trigger('click');
1223 equals( clicked, 2, "undelegate with a context");
1225 // Test binding with event data
1226 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1227 jQuery("#foo").trigger("click");
1228 jQuery("#body").undelegate("#foo", "click");
1230 // Test binding with trigger data
1231 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1232 jQuery("#foo").trigger("click", true);
1233 jQuery("#body").undelegate("#foo", "click");
1235 // Test binding with different this object
1236 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1237 jQuery("#foo").trigger("click");
1238 jQuery("#body").undelegate("#foo", "click");
1240 // Test binding with different this object, event data, and trigger data
1241 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1242 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1243 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1244 equals( data, true, "delegate with with different this object, event data, and trigger data")
1245 }, { foo: "bar" }));
1246 jQuery("#foo").trigger("click", true);
1247 jQuery("#body").undelegate("#foo", "click");
1249 // Verify that return false prevents default action
1250 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1251 var hash = window.location.hash;
1252 jQuery("#anchor2").trigger("click");
1253 equals( window.location.hash, hash, "return false worked" );
1254 jQuery("#body").undelegate("#anchor2", "click");
1256 // Verify that .preventDefault() prevents default action
1257 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1258 var hash = window.location.hash;
1259 jQuery("#anchor2").trigger("click");
1260 equals( window.location.hash, hash, "e.preventDefault() worked" );
1261 jQuery("#body").undelegate("#anchor2", "click");
1263 // Test binding the same handler to multiple points
1265 function callback(){ called++; return false; }
1267 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1268 jQuery("#body").delegate("#anchor2", "click", callback);
1270 jQuery("#nothiddendiv").trigger("click");
1271 equals( called, 1, "Verify that only one click occurred." );
1273 jQuery("#anchor2").trigger("click");
1274 equals( called, 2, "Verify that only one click occurred." );
1276 // Make sure that only one callback is removed
1277 jQuery("#body").undelegate("#anchor2", "click", callback);
1279 jQuery("#nothiddendiv").trigger("click");
1280 equals( called, 3, "Verify that only one click occurred." );
1282 jQuery("#anchor2").trigger("click");
1283 equals( called, 3, "Verify that no click occurred." );
1285 // Make sure that it still works if the selector is the same,
1286 // but the event type is different
1287 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1290 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1292 jQuery("#nothiddendiv").trigger("click");
1293 equals( called, 3, "Verify that no click occurred." );
1295 jQuery("#nothiddendiv").trigger("foo");
1296 equals( called, 4, "Verify that one foo occurred." );
1299 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1301 // Make sure we don't loose the target by DOM modifications
1302 // after the bubble already reached the liveHandler
1303 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1305 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1306 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1308 jQuery("#nothiddendiv span").click();
1309 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1310 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1313 jQuery("#body").undelegate("#nothiddendivchild", "click");
1315 // Verify that .live() ocurs and cancel buble in the same order as
1316 // we would expect .bind() and .click() without delegation
1317 var lived = 0, livee = 0;
1319 // bind one pair in one order
1320 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1321 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1323 jQuery('span#liveSpan1 a').click();
1324 equals( lived, 1, "Verify that only one first handler occurred." );
1325 equals( livee, 0, "Verify that second handler doesn't." );
1327 // and one pair in inverse
1328 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1329 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1333 jQuery('span#liveSpan2 a').click();
1334 equals( lived, 1, "Verify that only one first handler occurred." );
1335 equals( livee, 0, "Verify that second handler doesn't." );
1338 jQuery("#body").undelegate("click");
1340 // Test this, target and currentTarget are correct
1341 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1342 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1343 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1344 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1347 jQuery('span#liveSpan1 a').click();
1349 jQuery("#body").undelegate('span#liveSpan1', 'click');
1351 // Work with deep selectors
1354 function clickB(){ livee++; }
1356 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1357 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1358 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1360 equals( livee, 0, "No clicks, deep selector." );
1363 jQuery("#nothiddendivchild").trigger("click");
1364 equals( livee, 2, "Click, deep selector." );
1367 jQuery("#nothiddendivchild").trigger("mouseover");
1368 equals( livee, 1, "Mouseover, deep selector." );
1370 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1373 jQuery("#nothiddendivchild").trigger("click");
1374 equals( livee, 2, "Click, deep selector." );
1377 jQuery("#nothiddendivchild").trigger("mouseover");
1378 equals( livee, 0, "Mouseover, deep selector." );
1380 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1383 jQuery("#nothiddendivchild").trigger("click");
1384 equals( livee, 1, "Click, deep selector." );
1386 jQuery("#body").undelegate("#nothiddendiv div", "click");
1389 test("undelegate all bound events", function(){
1393 var div = jQuery("#body");
1395 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1398 jQuery("div#nothiddendivchild").trigger("click");
1399 jQuery("div#nothiddendivchild").trigger("submit");
1401 equals( count, 0, "Make sure no events were triggered." );
1404 test("delegate with multiple events", function(){
1408 var div = jQuery("#body");
1410 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1412 jQuery("div#nothiddendivchild").trigger("click");
1413 jQuery("div#nothiddendivchild").trigger("submit");
1415 equals( count, 2, "Make sure both the click and submit were triggered." );
1417 jQuery("#body").undelegate();
1420 test("delegate with change", function(){
1421 var selectChange = 0, checkboxChange = 0;
1423 var select = jQuery("select[name='S1']");
1424 jQuery("#body").delegate("select[name='S1']", "change", function() {
1428 var checkbox = jQuery("#check2"),
1429 checkboxFunction = function(){
1432 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1434 // test click on select
1436 // second click that changed it
1438 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1439 select.trigger("change");
1440 equals( selectChange, 1, "Change on click." );
1442 // test keys on select
1444 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1445 select.trigger("change");
1446 equals( selectChange, 1, "Change on keyup." );
1448 // test click on checkbox
1449 checkbox.trigger("change");
1450 equals( checkboxChange, 1, "Change on checkbox." );
1452 // test before activate on radio
1454 // test blur/focus on textarea
1455 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1456 jQuery("#body").delegate("#area1", "change", function() {
1460 textarea.val(oldVal + "foo");
1461 textarea.trigger("change");
1462 equals( textareaChange, 1, "Change on textarea." );
1464 textarea.val(oldVal);
1465 jQuery("#body").undelegate("#area1", "change");
1467 // test blur/focus on text
1468 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1469 jQuery("#body").delegate("#name", "change", function() {
1473 text.val(oldVal+"foo");
1474 text.trigger("change");
1475 equals( textChange, 1, "Change on text input." );
1477 text.val(oldTextVal);
1478 jQuery("#body").die("change");
1480 // test blur/focus on password
1481 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1482 jQuery("#body").delegate("#name", "change", function() {
1486 password.val(oldPasswordVal + "foo");
1487 password.trigger("change");
1488 equals( passwordChange, 1, "Change on password input." );
1490 password.val(oldPasswordVal);
1491 jQuery("#body").undelegate("#name", "change");
1493 // make sure die works
1497 jQuery("#body").undelegate("select[name='S1']", "change");
1498 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1499 select.trigger("change");
1500 equals( selectChange, 0, "Die on click works." );
1503 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1504 select.trigger("change");
1505 equals( selectChange, 0, "Die on keyup works." );
1507 // die specific checkbox
1508 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1509 checkbox.trigger("change");
1510 equals( checkboxChange, 1, "Die on checkbox." );
1513 test("delegate with submit", function() {
1514 var count1 = 0, count2 = 0;
1516 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1518 ev.preventDefault();
1521 jQuery(document).delegate("body", "submit", function(ev) {
1523 ev.preventDefault();
1526 if ( jQuery.support.submitBubbles ) {
1527 jQuery("#testForm input[name=sub1]")[0].click();
1531 jQuery("#testForm input[name=sub1]")[0].click();
1532 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1537 jQuery("#body").undelegate();
1538 jQuery(document).undelegate();
1541 test("Non DOM element events", function() {
1545 .bind('nonelementglobal', function(e) {
1546 ok( true, "Global event on non-DOM annonymos object triggered" );
1552 .bind('nonelementobj', function(e) {
1553 ok( true, "Event on non-DOM object triggered" );
1554 }).bind('nonelementglobal', function() {
1555 ok( true, "Global event on non-DOM object triggered" );
1558 jQuery(o).trigger('nonelementobj');
1559 jQuery.event.trigger('nonelementglobal');
1563 test("jQuery(function($) {})", function() {
1565 jQuery(function($) {
1566 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");
1571 test("event properties", function() {
1573 jQuery("#simon1").click(function(event) {
1574 ok( event.timeStamp, "assert event.timeStamp is present" );