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, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
926 // Test this, target and currentTarget are correct
927 jQuery('span#liveSpan1').live('click', function(e){
928 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
929 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
930 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
933 jQuery('span#liveSpan1 a').click();
935 jQuery('span#liveSpan1').die('click');
937 // Work with deep selectors
940 function clickB(){ livee++; }
942 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
943 jQuery("#nothiddendiv div").live("click", clickB);
944 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
946 equals( livee, 0, "No clicks, deep selector." );
949 jQuery("#nothiddendivchild").trigger("click");
950 equals( livee, 2, "Click, deep selector." );
953 jQuery("#nothiddendivchild").trigger("mouseover");
954 equals( livee, 1, "Mouseover, deep selector." );
956 jQuery("#nothiddendiv div").die("mouseover");
959 jQuery("#nothiddendivchild").trigger("click");
960 equals( livee, 2, "Click, deep selector." );
963 jQuery("#nothiddendivchild").trigger("mouseover");
964 equals( livee, 0, "Mouseover, deep selector." );
966 jQuery("#nothiddendiv div").die("click", clickB);
969 jQuery("#nothiddendivchild").trigger("click");
970 equals( livee, 1, "Click, deep selector." );
972 jQuery("#nothiddendiv div").die("click");
975 test("die all bound events", function(){
979 var div = jQuery("div#nothiddendivchild");
981 div.live("click submit", function(){ count++; });
984 div.trigger("click");
985 div.trigger("submit");
987 equals( count, 0, "Make sure no events were triggered." );
990 test("live with multiple events", function(){
994 var div = jQuery("div#nothiddendivchild");
996 div.live("click submit", function(){ count++; });
998 div.trigger("click");
999 div.trigger("submit");
1001 equals( count, 2, "Make sure both the click and submit were triggered." );
1004 test("live with change", function(){
1005 var selectChange = 0, checkboxChange = 0;
1007 var select = jQuery("select[name='S1']")
1008 select.live("change", function() {
1012 var checkbox = jQuery("#check2"),
1013 checkboxFunction = function(){
1016 checkbox.live("change", checkboxFunction);
1018 // test click on select
1020 // second click that changed it
1022 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1023 select.trigger("change");
1024 equals( selectChange, 1, "Change on click." );
1026 // test keys on select
1028 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1029 select.trigger("change");
1030 equals( selectChange, 1, "Change on keyup." );
1032 // test click on checkbox
1033 checkbox.trigger("change");
1034 equals( checkboxChange, 1, "Change on checkbox." );
1036 // test before activate on radio
1038 // test blur/focus on textarea
1039 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1040 textarea.live("change", function() {
1044 textarea.val(oldVal + "foo");
1045 textarea.trigger("change");
1046 equals( textareaChange, 1, "Change on textarea." );
1048 textarea.val(oldVal);
1049 textarea.die("change");
1051 // test blur/focus on text
1052 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1053 text.live("change", function() {
1057 text.val(oldVal+"foo");
1058 text.trigger("change");
1059 equals( textChange, 1, "Change on text input." );
1061 text.val(oldTextVal);
1064 // test blur/focus on password
1065 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1066 password.live("change", function() {
1070 password.val(oldPasswordVal + "foo");
1071 password.trigger("change");
1072 equals( passwordChange, 1, "Change on password input." );
1074 password.val(oldPasswordVal);
1075 password.die("change");
1077 // make sure die works
1081 select.die("change");
1082 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1083 select.trigger("change");
1084 equals( selectChange, 0, "Die on click works." );
1087 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1088 select.trigger("change");
1089 equals( selectChange, 0, "Die on keyup works." );
1091 // die specific checkbox
1092 checkbox.die("change", checkboxFunction);
1093 checkbox.trigger("change");
1094 equals( checkboxChange, 1, "Die on checkbox." );
1097 test("live with submit", function() {
1098 var count1 = 0, count2 = 0;
1100 jQuery("#testForm").live("submit", function(ev) {
1102 ev.preventDefault();
1105 jQuery("body").live("submit", function(ev) {
1107 ev.preventDefault();
1110 if ( jQuery.support.submitBubbles ) {
1111 jQuery("#testForm input[name=sub1]")[0].click();
1115 jQuery("#testForm input[name=sub1]")[0].click();
1116 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1121 jQuery("#testForm").die("submit");
1122 jQuery("body").die("submit");
1125 test("live with focus/blur", function(){
1129 jQuery("<input type='text' id='livefb' />").appendTo("body");
1131 var $child = jQuery("#livefb"),
1136 pass[e.type] = true;
1139 $child.live("focus", worked);
1140 $child.live("blur", worked);
1145 ok(true, "Test live() with focus event");
1147 ok(true, "Cannot test focus because the window isn't focused");
1151 ok( true, "Test live() with blur event");
1153 ok(true, "Cannot test blur because the window isn't focused");
1156 $child.die("focus", worked);
1157 $child.die("blur", worked);
1159 window.scrollTo(0,0);
1162 test("Non DOM element events", function() {
1166 .bind('nonelementglobal', function(e) {
1167 ok( true, "Global event on non-DOM annonymos object triggered" );
1173 .bind('nonelementobj', function(e) {
1174 ok( true, "Event on non-DOM object triggered" );
1175 }).bind('nonelementglobal', function() {
1176 ok( true, "Global event on non-DOM object triggered" );
1179 jQuery(o).trigger('nonelementobj');
1180 jQuery.event.trigger('nonelementglobal');
1184 test("jQuery(function($) {})", function() {
1186 jQuery(function($) {
1187 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");
1192 test("event properties", function() {
1194 jQuery("#simon1").click(function(event) {
1195 ok( event.timeStamp, "assert event.timeStamp is present" );