3 test("bind(), with data", function() {
5 var handler = function(event) {
6 ok( event.data, "bind() with data, check passed data exists" );
7 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
9 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
11 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
14 test("bind(), with data, trigger with data", function() {
16 var handler = function(event, data) {
17 ok( event.data, "check passed data exists" );
18 equals( event.data.foo, "bar", "Check value of passed data" );
19 ok( data, "Check trigger data" );
20 equals( data.bar, "foo", "Check value of trigger data" );
22 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
25 test("bind(), multiple events at once", function() {
29 var handler = function(event) {
30 if (event.type == "click")
32 else if (event.type == "mouseover")
33 mouseoverCounter += 1;
35 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
36 equals( clickCounter, 1, "bind() with multiple events at once" );
37 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
40 test("bind(), multiple events at once and namespaces", function() {
45 var div = jQuery("<div/>").bind("focusin.a", function(e) {
46 equals( e.type, cur, "Verify right single event was fired." );
50 div.trigger("focusin.a");
52 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
53 equals( e.type, cur, "Verify right multi event was fired." );
54 equals( e.data, obj, "Make sure the data came in correctly." );
61 div.trigger("mouseover");
63 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
64 equals( e.type, cur, "Verify right multi event was fired." );
68 div.trigger("focusin.a");
71 div.trigger("focusout.b");
74 test("bind(), namespace with special add", function() {
77 var div = jQuery("<div/>").bind("test", function(e) {
78 ok( true, "Test event fired." );
83 jQuery.event.special.test = {
84 _default: function(e) {
85 equals( this, document, "Make sure we're at the top of the chain." );
86 equals( e.type, "test", "And that we're still dealing with a test event." );
87 equals( e.target, div[0], "And that the target is correct." );
90 teardown: function(){},
91 add: function( handleObj ) {
92 var handler = handleObj.handler;
93 handleObj.handler = function(e) {
95 handler.apply( this, arguments );
101 div.bind("test.a", {x: 1}, function(e) {
102 ok( !!e.xyz, "Make sure that the data is getting passed through." );
103 equals( e.data.x, 1, "Make sure data is attached properly." );
106 div.bind("test.b", {x: 2}, function(e) {
107 ok( !!e.xyz, "Make sure that the data is getting passed through." );
108 equals( e.data.x, 2, "Make sure data is attached properly." );
115 div.trigger("test.a");
118 div.trigger("test.b");
121 test("bind(), no data", function() {
123 var handler = function(event) {
124 ok ( !event.data, "Check that no data is added to the event object" );
126 jQuery("#firstp").bind("click", handler).trigger("click");
129 test("bind/one/unbind(Object)", function(){
132 var clickCounter = 0, mouseoverCounter = 0;
133 function handler(event) {
134 if (event.type == "click")
136 else if (event.type == "mouseover")
140 function handlerWithData(event) {
141 if (event.type == "click")
142 clickCounter += event.data;
143 else if (event.type == "mouseover")
144 mouseoverCounter += event.data;
148 $elem.trigger("click").trigger("mouseover");
151 var $elem = jQuery("#firstp")
159 click:handlerWithData,
160 mouseover:handlerWithData
165 equals( clickCounter, 3, "bind(Object)" );
166 equals( mouseoverCounter, 3, "bind(Object)" );
169 equals( clickCounter, 4, "bind(Object)" );
170 equals( mouseoverCounter, 4, "bind(Object)" );
172 jQuery("#firstp").unbind({
178 equals( clickCounter, 4, "bind(Object)" );
179 equals( mouseoverCounter, 4, "bind(Object)" );
182 test("bind(), iframes", function() {
183 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
184 var doc = jQuery("#loadediframe").contents();
186 jQuery("div", doc).bind("click", function() {
187 ok( true, "Binding to element inside iframe" );
188 }).click().unbind('click');
191 test("bind(), trigger change on select", function() {
194 function selectOnChange(event) {
195 equals( event.data, counter++, "Event.data is not a global event object" );
197 jQuery("#form select").each(function(i){
198 jQuery(this).bind('change', i, selectOnChange);
199 }).trigger('change');
202 test("bind(), namespaced events, cloned events", function() {
205 jQuery("#firstp").bind("custom.test",function(e){
206 ok(true, "Custom event triggered");
209 jQuery("#firstp").bind("click",function(e){
210 ok(true, "Normal click triggered");
213 jQuery("#firstp").bind("click.test",function(e){
214 ok(true, "Namespaced click triggered");
217 // Trigger both bound fn (2)
218 jQuery("#firstp").trigger("click");
220 // Trigger one bound fn (1)
221 jQuery("#firstp").trigger("click.test");
223 // Remove only the one fn
224 jQuery("#firstp").unbind("click.test");
226 // Trigger the remaining fn (1)
227 jQuery("#firstp").trigger("click");
229 // Remove the remaining fn
230 jQuery("#firstp").unbind(".test");
232 // Trigger the remaining fn (0)
233 jQuery("#firstp").trigger("custom");
235 // using contents will get comments regular, text, and comment nodes
236 jQuery("#nonnodes").contents().bind("tester", function () {
237 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
238 }).trigger("tester");
240 // Make sure events stick with appendTo'd elements (which are cloned) #2027
241 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
242 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
245 test("bind(), multi-namespaced events", function() {
257 function check(name, msg){
258 same(name, order.shift(), msg);
261 jQuery("#firstp").bind("custom.test",function(e){
262 check("custom.test", "Custom event triggered");
265 jQuery("#firstp").bind("custom.test2",function(e){
266 check("custom.test2", "Custom event triggered");
269 jQuery("#firstp").bind("click.test",function(e){
270 check("click.test", "Normal click triggered");
273 jQuery("#firstp").bind("click.test.abc",function(e){
274 check("click.test.abc", "Namespaced click triggered");
277 // Those would not trigger/unbind (#5303)
278 jQuery("#firstp").trigger("click.a.test");
279 jQuery("#firstp").unbind("click.a.test");
281 // Trigger both bound fn (1)
282 jQuery("#firstp").trigger("click.test.abc");
284 // Trigger one bound fn (1)
285 jQuery("#firstp").trigger("click.abc");
287 // Trigger two bound fn (2)
288 jQuery("#firstp").trigger("click.test");
290 // Remove only the one fn
291 jQuery("#firstp").unbind("click.abc");
293 // Trigger the remaining fn (1)
294 jQuery("#firstp").trigger("click");
296 // Remove the remaining fn
297 jQuery("#firstp").unbind(".test");
299 // Trigger the remaining fn (1)
300 jQuery("#firstp").trigger("custom");
303 test("bind(), with different this object", function() {
305 var thisObject = { myThis: true },
306 data = { myData: true },
307 handler1 = function( event ) {
308 equals( this, thisObject, "bind() with different this object" );
310 handler2 = function( event ) {
311 equals( this, thisObject, "bind() with different this object and data" );
312 equals( event.data, data, "bind() with different this object and data" );
316 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
317 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
319 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
322 test("unbind(type)", function() {
325 var $elem = jQuery("#firstp"),
329 ok( false, message );
332 message = "unbind passing function";
333 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
335 message = "unbind all from event";
336 $elem.bind('error', error).unbind('error').triggerHandler('error');
338 message = "unbind all";
339 $elem.bind('error', error).unbind().triggerHandler('error');
341 message = "unbind many with function";
342 $elem.bind('error error2',error)
343 .unbind('error error2', error )
344 .trigger('error').triggerHandler('error2');
346 message = "unbind many"; // #3538
347 $elem.bind('error error2',error)
348 .unbind('error error2')
349 .trigger('error').triggerHandler('error2');
351 message = "unbind without a type or handler";
352 $elem.bind("error error2.test",error)
354 .trigger("error").triggerHandler("error2");
357 test("unbind(eventObject)", function() {
360 var $elem = jQuery("#firstp"),
363 function assert( expected ){
365 $elem.trigger('foo').triggerHandler('bar');
366 equals( num, expected, "Check the right handlers are triggered" );
370 // This handler shouldn't be unbound
371 .bind('foo', function(){
374 .bind('foo', function(e){
379 .bind('bar', function(){
393 test("hover()", function() {
395 handler1 = function( event ) { ++times; },
396 handler2 = function( event ) { ++times; };
399 .hover(handler1, handler2)
400 .mouseenter().mouseleave()
401 .unbind("mouseenter", handler1)
402 .unbind("mouseleave", handler2)
404 .mouseenter().mouseleave()
405 .unbind("mouseenter mouseleave", handler1)
406 .mouseenter().mouseleave();
408 equals( times, 4, "hover handlers fired" );
411 test("trigger() shortcuts", function() {
413 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
414 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
415 equals( close.length, 0, "Context element does not exist, length must be zero" );
416 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
420 jQuery("#check1").click(function() {
421 ok( true, "click event handler for checkbox gets fired twice, see #815" );
425 jQuery('#firstp')[0].onclick = function(event) {
428 jQuery('#firstp').click();
429 equals( counter, 1, "Check that click, triggers onclick event handler also" );
431 var clickCounter = 0;
432 jQuery('#simon1')[0].onclick = function(event) {
435 jQuery('#simon1').click();
436 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
438 jQuery('<img />').load(function(){
439 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
443 test("trigger() bubbling", function() {
446 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
448 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
449 jQuery("html").bind("click", function(e){ html++; });
450 jQuery("body").bind("click", function(e){ body++; });
451 jQuery("#main").bind("click", function(e){ main++; });
452 jQuery("#ap").bind("click", function(){ ap++; return false; });
454 jQuery("html").trigger("click");
455 equals( doc, 1, "HTML bubble" );
456 equals( html, 1, "HTML bubble" );
458 jQuery("body").trigger("click");
459 equals( doc, 2, "Body bubble" );
460 equals( html, 2, "Body bubble" );
461 equals( body, 1, "Body bubble" );
463 jQuery("#main").trigger("click");
464 equals( doc, 3, "Main bubble" );
465 equals( html, 3, "Main bubble" );
466 equals( body, 2, "Main bubble" );
467 equals( main, 1, "Main bubble" );
469 jQuery("#ap").trigger("click");
470 equals( doc, 3, "ap bubble" );
471 equals( html, 3, "ap bubble" );
472 equals( body, 2, "ap bubble" );
473 equals( main, 1, "ap bubble" );
474 equals( ap, 1, "ap bubble" );
477 test("trigger(type, [data], [fn])", function() {
480 var handler = function(event, a, b, c) {
481 equals( event.type, "click", "check passed data" );
482 equals( a, 1, "check passed data" );
483 equals( b, "2", "check passed data" );
484 equals( c, "abc", "check passed data" );
488 var $elem = jQuery("#firstp");
490 // Simulate a "native" click
491 $elem[0].click = function(){
492 ok( true, "Native call was triggered" );
495 // Triggers handlrs and native
497 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
499 // Simulate a "native" click
500 $elem[0].click = function(){
501 ok( false, "Native call was triggered" );
504 // Trigger only the handlers (no native)
506 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
510 jQuery('#form input:first').hide().trigger('focus');
514 ok( pass, "Trigger focus on hidden element" );
518 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
522 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
524 var form = jQuery("<form action=''></form>").appendTo("body");
526 // Make sure it can be prevented locally
527 form.submit(function(){
528 ok( true, "Local bind still works." );
533 form.trigger("submit");
535 form.unbind("submit");
537 jQuery(document).submit(function(){
538 ok( true, "Make sure bubble works up to document." );
543 form.trigger("submit");
545 jQuery(document).unbind("submit");
550 test("jQuery.Event.currentTarget", function(){
553 test("trigger(eventObject, [data], [fn])", function() {
556 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
557 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
559 var event = jQuery.Event("noNew");
560 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
561 equals( event.type, "noNew", "Verify its type" );
563 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
564 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
565 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
567 event.preventDefault();
568 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
569 event.stopPropagation();
570 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
572 event.isPropagationStopped = function(){ return false };
573 event.stopImmediatePropagation();
574 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
575 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
577 $parent.bind('foo',function(e){
579 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
580 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
581 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
582 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
585 // test with an event object
586 event = new jQuery.Event("foo");
587 event.secret = 'boo!';
588 $child.trigger(event);
590 // test with a literal object
591 $child.trigger({type:'foo', secret:'boo!'});
596 ok( false, "This assertion shouldn't be reached");
599 $parent.bind('foo', error );
601 $child.bind('foo',function(e, a, b, c ){
602 equals( arguments.length, 4, "Check arguments length");
603 equals( a, 1, "Check first custom argument");
604 equals( b, 2, "Check second custom argument");
605 equals( c, 3, "Check third custom argument");
607 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
608 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
609 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
612 e.stopImmediatePropagation();
617 // We should add this back in when we want to test the order
618 // in which event handlers are iterated.
619 //$child.bind('foo', error );
621 event = new jQuery.Event("foo");
622 $child.trigger( event, [1,2,3] ).unbind();
623 equals( event.result, "result", "Check event.result attribute");
625 // Will error if it bubbles
626 $child.triggerHandler('foo');
629 $parent.unbind().remove();
632 test("jQuery.Event.currentTarget", function(){
636 $elem = jQuery('<button>a</button>').click(function(e){
637 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
641 $elem.trigger('click');
647 test("toggle(Function, Function, ...)", function() {
651 fn1 = function(e) { count++; },
652 fn2 = function(e) { count--; },
653 preventDefault = function(e) { e.preventDefault() },
654 link = jQuery('#mark');
655 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
656 equals( count, 1, "Check for toggle(fn, fn)" );
658 jQuery("#firstp").toggle(function () {
659 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
660 }, function() {}).trigger("click", [ 1, 2, 3 ]);
663 jQuery("#simon1").one("click", function() {
664 ok( true, "Execute event only once" );
665 jQuery(this).toggle(function() {
666 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
668 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
671 }).click().click().click();
686 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
688 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
690 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
692 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
694 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
696 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
698 $div.unbind('click',fns[0]);
699 var data = jQuery.data( $div[0], 'events' );
700 ok( !data, "Unbinding one function from toggle unbinds them all");
702 // Test Multi-Toggles
704 $div = jQuery("<div/>");
705 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
707 same( a, [1], "Check that a click worked." );
709 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
711 same( a, [1,2], "Check that a click worked with a second toggle." );
712 same( b, [1], "Check that a click worked with a second toggle." );
715 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
716 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
719 test(".live()/.die()", function() {
722 var submit = 0, div = 0, livea = 0, liveb = 0;
724 jQuery("div").live("submit", function(){ submit++; return false; });
725 jQuery("div").live("click", function(){ div++; });
726 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
727 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
729 // Nothing should trigger on the body
730 jQuery("body").trigger("click");
731 equals( submit, 0, "Click on body" );
732 equals( div, 0, "Click on body" );
733 equals( livea, 0, "Click on body" );
734 equals( liveb, 0, "Click on body" );
736 // This should trigger two events
737 jQuery("div#nothiddendiv").trigger("click");
738 equals( submit, 0, "Click on div" );
739 equals( div, 1, "Click on div" );
740 equals( livea, 1, "Click on div" );
741 equals( liveb, 0, "Click on div" );
743 // This should trigger three events (w/ bubbling)
744 jQuery("div#nothiddendivchild").trigger("click");
745 equals( submit, 0, "Click on inner div" );
746 equals( div, 2, "Click on inner div" );
747 equals( livea, 2, "Click on inner div" );
748 equals( liveb, 1, "Click on inner div" );
750 // This should trigger one submit
751 jQuery("div#nothiddendivchild").trigger("submit");
752 equals( submit, 1, "Submit on div" );
753 equals( div, 2, "Submit on div" );
754 equals( livea, 2, "Submit on div" );
755 equals( liveb, 1, "Submit on div" );
757 // Make sure no other events were removed in the process
758 jQuery("div#nothiddendivchild").trigger("click");
759 equals( submit, 1, "die Click on inner div" );
760 equals( div, 3, "die Click on inner div" );
761 equals( livea, 3, "die Click on inner div" );
762 equals( liveb, 2, "die Click on inner div" );
764 // Now make sure that the removal works
765 jQuery("div#nothiddendivchild").die("click");
766 jQuery("div#nothiddendivchild").trigger("click");
767 equals( submit, 1, "die Click on inner div" );
768 equals( div, 4, "die Click on inner div" );
769 equals( livea, 4, "die Click on inner div" );
770 equals( liveb, 2, "die Click on inner div" );
772 // Make sure that the click wasn't removed too early
773 jQuery("div#nothiddendiv").trigger("click");
774 equals( submit, 1, "die Click on inner div" );
775 equals( div, 5, "die Click on inner div" );
776 equals( livea, 5, "die Click on inner div" );
777 equals( liveb, 2, "die Click on inner div" );
779 // Make sure that stopPropgation doesn't stop live events
780 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
781 jQuery("div#nothiddendivchild").trigger("click");
782 equals( submit, 1, "stopPropagation Click on inner div" );
783 equals( div, 6, "stopPropagation Click on inner div" );
784 equals( livea, 6, "stopPropagation Click on inner div" );
785 equals( liveb, 3, "stopPropagation Click on inner div" );
787 // Make sure click events only fire with primary click
788 var event = jQuery.Event("click");
790 jQuery("div#nothiddendiv").trigger(event);
792 equals( livea, 6, "live secondary click" );
794 jQuery("div#nothiddendivchild").die("click");
795 jQuery("div#nothiddendiv").die("click");
796 jQuery("div").die("click");
797 jQuery("div").die("submit");
799 // Test binding with a different context
800 var clicked = 0, container = jQuery('#main')[0];
801 jQuery("#foo", container).live("click", function(e){ clicked++; });
802 jQuery("div").trigger('click');
803 jQuery("#foo").trigger('click');
804 jQuery("#main").trigger('click');
805 jQuery("body").trigger('click');
806 equals( clicked, 2, "live with a context" );
808 // Make sure the event is actually stored on the context
809 ok( jQuery.data(container, "events").live, "live with a context" );
811 // Test unbinding with a different context
812 jQuery("#foo", container).die("click");
813 jQuery("#foo").trigger('click');
814 equals( clicked, 2, "die with a context");
816 // Test binding with event data
817 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
818 jQuery("#foo").trigger("click").die("click");
820 // Test binding with trigger data
821 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
822 jQuery("#foo").trigger("click", true).die("click");
824 // Test binding with different this object
825 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
826 jQuery("#foo").trigger("click").die("click");
828 // Test binding with different this object, event data, and trigger data
829 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
830 equals( e.data, true, "live with with different this object, event data, and trigger data" );
831 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
832 equals( data, true, "live with with different this object, event data, and trigger data")
834 jQuery("#foo").trigger("click", true).die("click");
836 // Verify that return false prevents default action
837 jQuery("#anchor2").live("click", function(){ return false; });
838 var hash = window.location.hash;
839 jQuery("#anchor2").trigger("click");
840 equals( window.location.hash, hash, "return false worked" );
841 jQuery("#anchor2").die("click");
843 // Verify that .preventDefault() prevents default action
844 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
845 var hash = window.location.hash;
846 jQuery("#anchor2").trigger("click");
847 equals( window.location.hash, hash, "e.preventDefault() worked" );
848 jQuery("#anchor2").die("click");
850 // Test binding the same handler to multiple points
852 function callback(){ called++; return false; }
854 jQuery("#nothiddendiv").live("click", callback);
855 jQuery("#anchor2").live("click", callback);
857 jQuery("#nothiddendiv").trigger("click");
858 equals( called, 1, "Verify that only one click occurred." );
860 jQuery("#anchor2").trigger("click");
861 equals( called, 2, "Verify that only one click occurred." );
863 // Make sure that only one callback is removed
864 jQuery("#anchor2").die("click", callback);
866 jQuery("#nothiddendiv").trigger("click");
867 equals( called, 3, "Verify that only one click occurred." );
869 jQuery("#anchor2").trigger("click");
870 equals( called, 3, "Verify that no click occurred." );
872 // Make sure that it still works if the selector is the same,
873 // but the event type is different
874 jQuery("#nothiddendiv").live("foo", callback);
877 jQuery("#nothiddendiv").die("click", callback);
879 jQuery("#nothiddendiv").trigger("click");
880 equals( called, 3, "Verify that no click occurred." );
882 jQuery("#nothiddendiv").trigger("foo");
883 equals( called, 4, "Verify that one foo occurred." );
886 jQuery("#nothiddendiv").die("foo", callback);
888 // Make sure we don't loose the target by DOM modifications
889 // after the bubble already reached the liveHandler
890 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
892 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
893 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
895 jQuery("#nothiddendiv span").click();
896 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
897 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
900 jQuery("#nothiddendivchild").die("click");
902 // Verify that .live() ocurs and cancel buble in the same order as
903 // we would expect .bind() and .click() without delegation
904 var lived = 0, livee = 0;
906 // bind one pair in one order
907 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
908 jQuery('span#liveSpan1').live('click', function(){ livee++; });
910 jQuery('span#liveSpan1 a').click();
911 equals( lived, 1, "Verify that only one first handler occurred." );
912 equals( livee, 0, "Verify that second handler doesn't." );
914 // and one pair in inverse
915 jQuery('span#liveSpan2').live('click', function(){ livee++; });
916 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
920 jQuery('span#liveSpan2 a').click();
921 equals( lived, 1, "Verify that only one first handler occurred." );
922 equals( livee, 0, "Verify that second handler doesn't." );
925 jQuery("span#liveSpan1 a").die("click")
926 jQuery("span#liveSpan1").die("click");
927 jQuery("span#liveSpan2 a").die("click");
928 jQuery("span#liveSpan2").die("click");
930 // Test this, target and currentTarget are correct
931 jQuery('span#liveSpan1').live('click', function(e){
932 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
933 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
934 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
937 jQuery('span#liveSpan1 a').click();
939 jQuery('span#liveSpan1').die('click');
941 // Work with deep selectors
944 function clickB(){ livee++; }
946 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
947 jQuery("#nothiddendiv div").live("click", clickB);
948 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
950 equals( livee, 0, "No clicks, deep selector." );
953 jQuery("#nothiddendivchild").trigger("click");
954 equals( livee, 2, "Click, deep selector." );
957 jQuery("#nothiddendivchild").trigger("mouseover");
958 equals( livee, 1, "Mouseover, deep selector." );
960 jQuery("#nothiddendiv div").die("mouseover");
963 jQuery("#nothiddendivchild").trigger("click");
964 equals( livee, 2, "Click, deep selector." );
967 jQuery("#nothiddendivchild").trigger("mouseover");
968 equals( livee, 0, "Mouseover, deep selector." );
970 jQuery("#nothiddendiv div").die("click", clickB);
973 jQuery("#nothiddendivchild").trigger("click");
974 equals( livee, 1, "Click, deep selector." );
976 jQuery("#nothiddendiv div").die("click");
979 test("die all bound events", function(){
983 var div = jQuery("div#nothiddendivchild");
985 div.live("click submit", function(){ count++; });
988 div.trigger("click");
989 div.trigger("submit");
991 equals( count, 0, "Make sure no events were triggered." );
994 test("live with multiple events", function(){
998 var div = jQuery("div#nothiddendivchild");
1000 div.live("click submit", function(){ count++; });
1002 div.trigger("click");
1003 div.trigger("submit");
1005 equals( count, 2, "Make sure both the click and submit were triggered." );
1008 test("live with change", function(){
1009 var selectChange = 0, checkboxChange = 0;
1011 var select = jQuery("select[name='S1']")
1012 select.live("change", function() {
1016 var checkbox = jQuery("#check2"),
1017 checkboxFunction = function(){
1020 checkbox.live("change", checkboxFunction);
1022 // test click on select
1024 // second click that changed it
1026 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1027 select.trigger("change");
1028 equals( selectChange, 1, "Change on click." );
1030 // test keys on select
1032 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1033 select.trigger("change");
1034 equals( selectChange, 1, "Change on keyup." );
1036 // test click on checkbox
1037 checkbox.trigger("change");
1038 equals( checkboxChange, 1, "Change on checkbox." );
1040 // test before activate on radio
1042 // test blur/focus on textarea
1043 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1044 textarea.live("change", function() {
1048 textarea.val(oldVal + "foo");
1049 textarea.trigger("change");
1050 equals( textareaChange, 1, "Change on textarea." );
1052 textarea.val(oldVal);
1053 textarea.die("change");
1055 // test blur/focus on text
1056 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1057 text.live("change", function() {
1061 text.val(oldVal+"foo");
1062 text.trigger("change");
1063 equals( textChange, 1, "Change on text input." );
1065 text.val(oldTextVal);
1068 // test blur/focus on password
1069 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1070 password.live("change", function() {
1074 password.val(oldPasswordVal + "foo");
1075 password.trigger("change");
1076 equals( passwordChange, 1, "Change on password input." );
1078 password.val(oldPasswordVal);
1079 password.die("change");
1081 // make sure die works
1085 select.die("change");
1086 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1087 select.trigger("change");
1088 equals( selectChange, 0, "Die on click works." );
1091 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1092 select.trigger("change");
1093 equals( selectChange, 0, "Die on keyup works." );
1095 // die specific checkbox
1096 checkbox.die("change", checkboxFunction);
1097 checkbox.trigger("change");
1098 equals( checkboxChange, 1, "Die on checkbox." );
1101 test("live with submit", function() {
1102 var count1 = 0, count2 = 0;
1104 jQuery("#testForm").live("submit", function(ev) {
1106 ev.preventDefault();
1109 jQuery("body").live("submit", function(ev) {
1111 ev.preventDefault();
1114 if ( jQuery.support.submitBubbles ) {
1115 jQuery("#testForm input[name=sub1]")[0].click();
1119 jQuery("#testForm input[name=sub1]")[0].click();
1120 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1125 jQuery("#testForm").die("submit");
1126 jQuery("body").die("submit");
1129 test(".delegate()/.undelegate()", function() {
1132 var submit = 0, div = 0, livea = 0, liveb = 0;
1134 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1135 jQuery("#body").delegate("div", "click", function(){ div++; });
1136 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1137 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1139 // Nothing should trigger on the body
1140 jQuery("body").trigger("click");
1141 equals( submit, 0, "Click on body" );
1142 equals( div, 0, "Click on body" );
1143 equals( livea, 0, "Click on body" );
1144 equals( liveb, 0, "Click on body" );
1146 // This should trigger two events
1147 jQuery("div#nothiddendiv").trigger("click");
1148 equals( submit, 0, "Click on div" );
1149 equals( div, 1, "Click on div" );
1150 equals( livea, 1, "Click on div" );
1151 equals( liveb, 0, "Click on div" );
1153 // This should trigger three events (w/ bubbling)
1154 jQuery("div#nothiddendivchild").trigger("click");
1155 equals( submit, 0, "Click on inner div" );
1156 equals( div, 2, "Click on inner div" );
1157 equals( livea, 2, "Click on inner div" );
1158 equals( liveb, 1, "Click on inner div" );
1160 // This should trigger one submit
1161 jQuery("div#nothiddendivchild").trigger("submit");
1162 equals( submit, 1, "Submit on div" );
1163 equals( div, 2, "Submit on div" );
1164 equals( livea, 2, "Submit on div" );
1165 equals( liveb, 1, "Submit on div" );
1167 // Make sure no other events were removed in the process
1168 jQuery("div#nothiddendivchild").trigger("click");
1169 equals( submit, 1, "undelegate Click on inner div" );
1170 equals( div, 3, "undelegate Click on inner div" );
1171 equals( livea, 3, "undelegate Click on inner div" );
1172 equals( liveb, 2, "undelegate Click on inner div" );
1174 // Now make sure that the removal works
1175 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1176 jQuery("div#nothiddendivchild").trigger("click");
1177 equals( submit, 1, "undelegate Click on inner div" );
1178 equals( div, 4, "undelegate Click on inner div" );
1179 equals( livea, 4, "undelegate Click on inner div" );
1180 equals( liveb, 2, "undelegate Click on inner div" );
1182 // Make sure that the click wasn't removed too early
1183 jQuery("div#nothiddendiv").trigger("click");
1184 equals( submit, 1, "undelegate Click on inner div" );
1185 equals( div, 5, "undelegate Click on inner div" );
1186 equals( livea, 5, "undelegate Click on inner div" );
1187 equals( liveb, 2, "undelegate Click on inner div" );
1189 // Make sure that stopPropgation doesn't stop live events
1190 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1191 jQuery("div#nothiddendivchild").trigger("click");
1192 equals( submit, 1, "stopPropagation Click on inner div" );
1193 equals( div, 6, "stopPropagation Click on inner div" );
1194 equals( livea, 6, "stopPropagation Click on inner div" );
1195 equals( liveb, 3, "stopPropagation Click on inner div" );
1197 // Make sure click events only fire with primary click
1198 var event = jQuery.Event("click");
1200 jQuery("div#nothiddendiv").trigger(event);
1202 equals( livea, 6, "delegate secondary click" );
1204 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1205 jQuery("#body").undelegate("div#nothiddendiv", "click");
1206 jQuery("#body").undelegate("div", "click");
1207 jQuery("#body").undelegate("div", "submit");
1209 // Test binding with a different context
1210 var clicked = 0, container = jQuery('#main')[0];
1211 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1212 jQuery("div").trigger('click');
1213 jQuery("#foo").trigger('click');
1214 jQuery("#main").trigger('click');
1215 jQuery("body").trigger('click');
1216 equals( clicked, 2, "delegate with a context" );
1218 // Make sure the event is actually stored on the context
1219 ok( jQuery.data(container, "events").live, "delegate with a context" );
1221 // Test unbinding with a different context
1222 jQuery("#main").undelegate("#foo", "click");
1223 jQuery("#foo").trigger('click');
1224 equals( clicked, 2, "undelegate with a context");
1226 // Test binding with event data
1227 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1228 jQuery("#foo").trigger("click");
1229 jQuery("#body").undelegate("#foo", "click");
1231 // Test binding with trigger data
1232 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1233 jQuery("#foo").trigger("click", true);
1234 jQuery("#body").undelegate("#foo", "click");
1236 // Test binding with different this object
1237 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1238 jQuery("#foo").trigger("click");
1239 jQuery("#body").undelegate("#foo", "click");
1241 // Test binding with different this object, event data, and trigger data
1242 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1243 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1244 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1245 equals( data, true, "delegate with with different this object, event data, and trigger data")
1246 }, { foo: "bar" }));
1247 jQuery("#foo").trigger("click", true);
1248 jQuery("#body").undelegate("#foo", "click");
1250 // Verify that return false prevents default action
1251 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1252 var hash = window.location.hash;
1253 jQuery("#anchor2").trigger("click");
1254 equals( window.location.hash, hash, "return false worked" );
1255 jQuery("#body").undelegate("#anchor2", "click");
1257 // Verify that .preventDefault() prevents default action
1258 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1259 var hash = window.location.hash;
1260 jQuery("#anchor2").trigger("click");
1261 equals( window.location.hash, hash, "e.preventDefault() worked" );
1262 jQuery("#body").undelegate("#anchor2", "click");
1264 // Test binding the same handler to multiple points
1266 function callback(){ called++; return false; }
1268 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1269 jQuery("#body").delegate("#anchor2", "click", callback);
1271 jQuery("#nothiddendiv").trigger("click");
1272 equals( called, 1, "Verify that only one click occurred." );
1274 jQuery("#anchor2").trigger("click");
1275 equals( called, 2, "Verify that only one click occurred." );
1277 // Make sure that only one callback is removed
1278 jQuery("#body").undelegate("#anchor2", "click", callback);
1280 jQuery("#nothiddendiv").trigger("click");
1281 equals( called, 3, "Verify that only one click occurred." );
1283 jQuery("#anchor2").trigger("click");
1284 equals( called, 3, "Verify that no click occurred." );
1286 // Make sure that it still works if the selector is the same,
1287 // but the event type is different
1288 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1291 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1293 jQuery("#nothiddendiv").trigger("click");
1294 equals( called, 3, "Verify that no click occurred." );
1296 jQuery("#nothiddendiv").trigger("foo");
1297 equals( called, 4, "Verify that one foo occurred." );
1300 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1302 // Make sure we don't loose the target by DOM modifications
1303 // after the bubble already reached the liveHandler
1304 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1306 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1307 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1309 jQuery("#nothiddendiv span").click();
1310 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1311 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1314 jQuery("#body").undelegate("#nothiddendivchild", "click");
1316 // Verify that .live() ocurs and cancel buble in the same order as
1317 // we would expect .bind() and .click() without delegation
1318 var lived = 0, livee = 0;
1320 // bind one pair in one order
1321 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1322 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1324 jQuery('span#liveSpan1 a').click();
1325 equals( lived, 1, "Verify that only one first handler occurred." );
1326 equals( livee, 0, "Verify that second handler doesn't." );
1328 // and one pair in inverse
1329 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1330 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1334 jQuery('span#liveSpan2 a').click();
1335 equals( lived, 1, "Verify that only one first handler occurred." );
1336 equals( livee, 0, "Verify that second handler doesn't." );
1339 jQuery("#body").undelegate("click");
1341 // Test this, target and currentTarget are correct
1342 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1343 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1344 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1345 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1348 jQuery('span#liveSpan1 a').click();
1350 jQuery("#body").undelegate('span#liveSpan1', 'click');
1352 // Work with deep selectors
1355 function clickB(){ livee++; }
1357 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1358 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1359 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1361 equals( livee, 0, "No clicks, deep selector." );
1364 jQuery("#nothiddendivchild").trigger("click");
1365 equals( livee, 2, "Click, deep selector." );
1368 jQuery("#nothiddendivchild").trigger("mouseover");
1369 equals( livee, 1, "Mouseover, deep selector." );
1371 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1374 jQuery("#nothiddendivchild").trigger("click");
1375 equals( livee, 2, "Click, deep selector." );
1378 jQuery("#nothiddendivchild").trigger("mouseover");
1379 equals( livee, 0, "Mouseover, deep selector." );
1381 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1384 jQuery("#nothiddendivchild").trigger("click");
1385 equals( livee, 1, "Click, deep selector." );
1387 jQuery("#body").undelegate("#nothiddendiv div", "click");
1390 test("undelegate all bound events", function(){
1394 var div = jQuery("#body");
1396 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1399 jQuery("div#nothiddendivchild").trigger("click");
1400 jQuery("div#nothiddendivchild").trigger("submit");
1402 equals( count, 0, "Make sure no events were triggered." );
1405 test("delegate with multiple events", function(){
1409 var div = jQuery("#body");
1411 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1413 jQuery("div#nothiddendivchild").trigger("click");
1414 jQuery("div#nothiddendivchild").trigger("submit");
1416 equals( count, 2, "Make sure both the click and submit were triggered." );
1418 jQuery("#body").undelegate();
1421 test("delegate with change", function(){
1422 var selectChange = 0, checkboxChange = 0;
1424 var select = jQuery("select[name='S1']");
1425 jQuery("#body").delegate("select[name='S1']", "change", function() {
1429 var checkbox = jQuery("#check2"),
1430 checkboxFunction = function(){
1433 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1435 // test click on select
1437 // second click that changed it
1439 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1440 select.trigger("change");
1441 equals( selectChange, 1, "Change on click." );
1443 // test keys on select
1445 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1446 select.trigger("change");
1447 equals( selectChange, 1, "Change on keyup." );
1449 // test click on checkbox
1450 checkbox.trigger("change");
1451 equals( checkboxChange, 1, "Change on checkbox." );
1453 // test before activate on radio
1455 // test blur/focus on textarea
1456 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1457 jQuery("#body").delegate("#area1", "change", function() {
1461 textarea.val(oldVal + "foo");
1462 textarea.trigger("change");
1463 equals( textareaChange, 1, "Change on textarea." );
1465 textarea.val(oldVal);
1466 jQuery("#body").undelegate("#area1", "change");
1468 // test blur/focus on text
1469 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1470 jQuery("#body").delegate("#name", "change", function() {
1474 text.val(oldVal+"foo");
1475 text.trigger("change");
1476 equals( textChange, 1, "Change on text input." );
1478 text.val(oldTextVal);
1479 jQuery("#body").die("change");
1481 // test blur/focus on password
1482 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1483 jQuery("#body").delegate("#name", "change", function() {
1487 password.val(oldPasswordVal + "foo");
1488 password.trigger("change");
1489 equals( passwordChange, 1, "Change on password input." );
1491 password.val(oldPasswordVal);
1492 jQuery("#body").undelegate("#name", "change");
1494 // make sure die works
1498 jQuery("#body").undelegate("select[name='S1']", "change");
1499 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1500 select.trigger("change");
1501 equals( selectChange, 0, "Die on click works." );
1504 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1505 select.trigger("change");
1506 equals( selectChange, 0, "Die on keyup works." );
1508 // die specific checkbox
1509 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1510 checkbox.trigger("change");
1511 equals( checkboxChange, 1, "Die on checkbox." );
1514 test("delegate with submit", function() {
1515 var count1 = 0, count2 = 0;
1517 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1519 ev.preventDefault();
1522 jQuery(document).delegate("body", "submit", function(ev) {
1524 ev.preventDefault();
1527 if ( jQuery.support.submitBubbles ) {
1528 jQuery("#testForm input[name=sub1]")[0].click();
1532 jQuery("#testForm input[name=sub1]")[0].click();
1533 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1538 jQuery("#body").undelegate();
1539 jQuery(document).undelegate();
1542 test("Non DOM element events", function() {
1546 .bind('nonelementglobal', function(e) {
1547 ok( true, "Global event on non-DOM annonymos object triggered" );
1553 .bind('nonelementobj', function(e) {
1554 ok( true, "Event on non-DOM object triggered" );
1555 }).bind('nonelementglobal', function() {
1556 ok( true, "Global event on non-DOM object triggered" );
1559 jQuery(o).trigger('nonelementobj');
1560 jQuery.event.trigger('nonelementglobal');
1564 test("jQuery(function($) {})", function() {
1566 jQuery(function($) {
1567 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");
1572 test("event properties", function() {
1574 jQuery("#simon1").click(function(event) {
1575 ok( event.timeStamp, "assert event.timeStamp is present" );