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." );
91 ok(true, "Teardown called.");
93 add: function( handleObj ) {
94 var handler = handleObj.handler;
95 handleObj.handler = function(e) {
97 handler.apply( this, arguments );
101 ok(true, "Remove called.");
105 div.bind("test.a", {x: 1}, function(e) {
106 ok( !!e.xyz, "Make sure that the data is getting passed through." );
107 equals( e.data.x, 1, "Make sure data is attached properly." );
110 div.bind("test.b", {x: 2}, function(e) {
111 ok( !!e.xyz, "Make sure that the data is getting passed through." );
112 equals( e.data.x, 2, "Make sure data is attached properly." );
119 div.trigger("test.a");
122 div.trigger("test.b");
127 div = jQuery("<div/>").bind("test", function(e) {
128 ok( true, "Test event fired." );
132 div.appendTo("#main").remove();
134 delete jQuery.event.special.test;
137 test("bind(), no data", function() {
139 var handler = function(event) {
140 ok ( !event.data, "Check that no data is added to the event object" );
142 jQuery("#firstp").bind("click", handler).trigger("click");
145 test("bind/one/unbind(Object)", function(){
148 var clickCounter = 0, mouseoverCounter = 0;
149 function handler(event) {
150 if (event.type == "click")
152 else if (event.type == "mouseover")
156 function handlerWithData(event) {
157 if (event.type == "click")
158 clickCounter += event.data;
159 else if (event.type == "mouseover")
160 mouseoverCounter += event.data;
164 $elem.trigger("click").trigger("mouseover");
167 var $elem = jQuery("#firstp")
175 click:handlerWithData,
176 mouseover:handlerWithData
181 equals( clickCounter, 3, "bind(Object)" );
182 equals( mouseoverCounter, 3, "bind(Object)" );
185 equals( clickCounter, 4, "bind(Object)" );
186 equals( mouseoverCounter, 4, "bind(Object)" );
188 jQuery("#firstp").unbind({
194 equals( clickCounter, 4, "bind(Object)" );
195 equals( mouseoverCounter, 4, "bind(Object)" );
198 test("bind(), iframes", function() {
199 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
200 var doc = jQuery("#loadediframe").contents();
202 jQuery("div", doc).bind("click", function() {
203 ok( true, "Binding to element inside iframe" );
204 }).click().unbind('click');
207 test("bind(), trigger change on select", function() {
210 function selectOnChange(event) {
211 equals( event.data, counter++, "Event.data is not a global event object" );
213 jQuery("#form select").each(function(i){
214 jQuery(this).bind('change', i, selectOnChange);
215 }).trigger('change');
218 test("bind(), namespaced events, cloned events", function() {
221 jQuery("#firstp").bind("custom.test",function(e){
222 ok(true, "Custom event triggered");
225 jQuery("#firstp").bind("click",function(e){
226 ok(true, "Normal click triggered");
229 jQuery("#firstp").bind("click.test",function(e){
230 ok(true, "Namespaced click triggered");
233 // Trigger both bound fn (2)
234 jQuery("#firstp").trigger("click");
236 // Trigger one bound fn (1)
237 jQuery("#firstp").trigger("click.test");
239 // Remove only the one fn
240 jQuery("#firstp").unbind("click.test");
242 // Trigger the remaining fn (1)
243 jQuery("#firstp").trigger("click");
245 // Remove the remaining fn
246 jQuery("#firstp").unbind(".test");
248 // Trigger the remaining fn (0)
249 jQuery("#firstp").trigger("custom");
251 // using contents will get comments regular, text, and comment nodes
252 jQuery("#nonnodes").contents().bind("tester", function () {
253 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
254 }).trigger("tester");
256 // Make sure events stick with appendTo'd elements (which are cloned) #2027
257 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
258 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
261 test("bind(), multi-namespaced events", function() {
273 function check(name, msg){
274 same(name, order.shift(), msg);
277 jQuery("#firstp").bind("custom.test",function(e){
278 check("custom.test", "Custom event triggered");
281 jQuery("#firstp").bind("custom.test2",function(e){
282 check("custom.test2", "Custom event triggered");
285 jQuery("#firstp").bind("click.test",function(e){
286 check("click.test", "Normal click triggered");
289 jQuery("#firstp").bind("click.test.abc",function(e){
290 check("click.test.abc", "Namespaced click triggered");
293 // Those would not trigger/unbind (#5303)
294 jQuery("#firstp").trigger("click.a.test");
295 jQuery("#firstp").unbind("click.a.test");
297 // Trigger both bound fn (1)
298 jQuery("#firstp").trigger("click.test.abc");
300 // Trigger one bound fn (1)
301 jQuery("#firstp").trigger("click.abc");
303 // Trigger two bound fn (2)
304 jQuery("#firstp").trigger("click.test");
306 // Remove only the one fn
307 jQuery("#firstp").unbind("click.abc");
309 // Trigger the remaining fn (1)
310 jQuery("#firstp").trigger("click");
312 // Remove the remaining fn
313 jQuery("#firstp").unbind(".test");
315 // Trigger the remaining fn (1)
316 jQuery("#firstp").trigger("custom");
319 test("bind(), with same function", function() {
322 var count = 0 , func = function(){
326 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
327 jQuery("#liveHandlerOrder").trigger("foo.bar");
329 equals(count, 1, "Verify binding function with multiple namespaces." );
331 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
332 jQuery("#liveHandlerOrder").trigger("foo.bar");
334 equals(count, 1, "Verify that removing events still work." );
337 test("bind(), make sure order is maintained", function() {
340 var elem = jQuery("#firstp"), log = [], check = [];
342 for ( var i = 0; i < 100; i++ ) (function(i){
343 elem.bind( "click", function(){
350 elem.trigger("click");
352 equals( log.join(","), check.join(","), "Make sure order was maintained." );
354 elem.unbind("click");
357 test("bind(), with different this object", function() {
359 var thisObject = { myThis: true },
360 data = { myData: true },
361 handler1 = function( event ) {
362 equals( this, thisObject, "bind() with different this object" );
364 handler2 = function( event ) {
365 equals( this, thisObject, "bind() with different this object and data" );
366 equals( event.data, data, "bind() with different this object and data" );
370 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
371 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
373 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
376 test("bind()/trigger()/unbind() on plain object", function() {
381 // Make sure it doesn't complain when no events are found
382 jQuery(obj).trigger("test");
384 // Make sure it doesn't complain when no events are found
385 jQuery(obj).unbind("test");
387 jQuery(obj).bind("test", function(){
388 ok( true, "Custom event run." );
391 ok( jQuery(obj).data("events"), "Object has events bound." );
394 jQuery(obj).trigger("test");
396 jQuery(obj).unbind("test");
399 jQuery(obj).trigger("test");
401 // Make sure it doesn't complain when no events are found
402 jQuery(obj).unbind("test");
405 test("unbind(type)", function() {
408 var $elem = jQuery("#firstp"),
412 ok( false, message );
415 message = "unbind passing function";
416 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
418 message = "unbind all from event";
419 $elem.bind('error', error).unbind('error').triggerHandler('error');
421 message = "unbind all";
422 $elem.bind('error', error).unbind().triggerHandler('error');
424 message = "unbind many with function";
425 $elem.bind('error error2',error)
426 .unbind('error error2', error )
427 .trigger('error').triggerHandler('error2');
429 message = "unbind many"; // #3538
430 $elem.bind('error error2',error)
431 .unbind('error error2')
432 .trigger('error').triggerHandler('error2');
434 message = "unbind without a type or handler";
435 $elem.bind("error error2.test",error)
437 .trigger("error").triggerHandler("error2");
440 test("unbind(eventObject)", function() {
443 var $elem = jQuery("#firstp"),
446 function assert( expected ){
448 $elem.trigger('foo').triggerHandler('bar');
449 equals( num, expected, "Check the right handlers are triggered" );
453 // This handler shouldn't be unbound
454 .bind('foo', function(){
457 .bind('foo', function(e){
462 .bind('bar', function(){
476 test("hover()", function() {
478 handler1 = function( event ) { ++times; },
479 handler2 = function( event ) { ++times; };
482 .hover(handler1, handler2)
483 .mouseenter().mouseleave()
484 .unbind("mouseenter", handler1)
485 .unbind("mouseleave", handler2)
487 .mouseenter().mouseleave()
488 .unbind("mouseenter mouseleave", handler1)
489 .mouseenter().mouseleave();
491 equals( times, 4, "hover handlers fired" );
494 test("trigger() shortcuts", function() {
496 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
497 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
498 equals( close.length, 0, "Context element does not exist, length must be zero" );
499 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
503 jQuery("#check1").click(function() {
504 ok( true, "click event handler for checkbox gets fired twice, see #815" );
508 jQuery('#firstp')[0].onclick = function(event) {
511 jQuery('#firstp').click();
512 equals( counter, 1, "Check that click, triggers onclick event handler also" );
514 var clickCounter = 0;
515 jQuery('#simon1')[0].onclick = function(event) {
518 jQuery('#simon1').click();
519 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
521 jQuery('<img />').load(function(){
522 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
526 test("trigger() bubbling", function() {
529 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
531 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
532 jQuery("html").bind("click", function(e){ html++; });
533 jQuery("body").bind("click", function(e){ body++; });
534 jQuery("#main").bind("click", function(e){ main++; });
535 jQuery("#ap").bind("click", function(){ ap++; return false; });
537 jQuery("html").trigger("click");
538 equals( doc, 1, "HTML bubble" );
539 equals( html, 1, "HTML bubble" );
541 jQuery("body").trigger("click");
542 equals( doc, 2, "Body bubble" );
543 equals( html, 2, "Body bubble" );
544 equals( body, 1, "Body bubble" );
546 jQuery("#main").trigger("click");
547 equals( doc, 3, "Main bubble" );
548 equals( html, 3, "Main bubble" );
549 equals( body, 2, "Main bubble" );
550 equals( main, 1, "Main bubble" );
552 jQuery("#ap").trigger("click");
553 equals( doc, 3, "ap bubble" );
554 equals( html, 3, "ap bubble" );
555 equals( body, 2, "ap bubble" );
556 equals( main, 1, "ap bubble" );
557 equals( ap, 1, "ap bubble" );
560 test("trigger(type, [data], [fn])", function() {
563 var handler = function(event, a, b, c) {
564 equals( event.type, "click", "check passed data" );
565 equals( a, 1, "check passed data" );
566 equals( b, "2", "check passed data" );
567 equals( c, "abc", "check passed data" );
571 var $elem = jQuery("#firstp");
573 // Simulate a "native" click
574 $elem[0].click = function(){
575 ok( true, "Native call was triggered" );
578 // Triggers handlrs and native
580 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
582 // Simulate a "native" click
583 $elem[0].click = function(){
584 ok( false, "Native call was triggered" );
587 // Trigger only the handlers (no native)
589 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
593 jQuery('#form input:first').hide().trigger('focus');
597 ok( pass, "Trigger focus on hidden element" );
601 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
605 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
607 var form = jQuery("<form action=''></form>").appendTo("body");
609 // Make sure it can be prevented locally
610 form.submit(function(){
611 ok( true, "Local bind still works." );
616 form.trigger("submit");
618 form.unbind("submit");
620 jQuery(document).submit(function(){
621 ok( true, "Make sure bubble works up to document." );
626 form.trigger("submit");
628 jQuery(document).unbind("submit");
633 test("jQuery.Event.currentTarget", function(){
636 test("trigger(eventObject, [data], [fn])", function() {
639 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
640 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
642 var event = jQuery.Event("noNew");
643 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
644 equals( event.type, "noNew", "Verify its type" );
646 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
647 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
648 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
650 event.preventDefault();
651 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
652 event.stopPropagation();
653 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
655 event.isPropagationStopped = function(){ return false };
656 event.stopImmediatePropagation();
657 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
658 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
660 $parent.bind('foo',function(e){
662 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
663 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
664 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
665 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
668 // test with an event object
669 event = new jQuery.Event("foo");
670 event.secret = 'boo!';
671 $child.trigger(event);
673 // test with a literal object
674 $child.trigger({type:'foo', secret:'boo!'});
679 ok( false, "This assertion shouldn't be reached");
682 $parent.bind('foo', error );
684 $child.bind('foo',function(e, a, b, c ){
685 equals( arguments.length, 4, "Check arguments length");
686 equals( a, 1, "Check first custom argument");
687 equals( b, 2, "Check second custom argument");
688 equals( c, 3, "Check third custom argument");
690 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
691 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
692 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
695 e.stopImmediatePropagation();
700 // We should add this back in when we want to test the order
701 // in which event handlers are iterated.
702 //$child.bind('foo', error );
704 event = new jQuery.Event("foo");
705 $child.trigger( event, [1,2,3] ).unbind();
706 equals( event.result, "result", "Check event.result attribute");
708 // Will error if it bubbles
709 $child.triggerHandler('foo');
712 $parent.unbind().remove();
715 test("jQuery.Event.currentTarget", function(){
719 $elem = jQuery('<button>a</button>').click(function(e){
720 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
724 $elem.trigger('click');
730 test("toggle(Function, Function, ...)", function() {
734 fn1 = function(e) { count++; },
735 fn2 = function(e) { count--; },
736 preventDefault = function(e) { e.preventDefault() },
737 link = jQuery('#mark');
738 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
739 equals( count, 1, "Check for toggle(fn, fn)" );
741 jQuery("#firstp").toggle(function () {
742 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
743 }, function() {}).trigger("click", [ 1, 2, 3 ]);
746 jQuery("#simon1").one("click", function() {
747 ok( true, "Execute event only once" );
748 jQuery(this).toggle(function() {
749 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
751 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
754 }).click().click().click();
769 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
771 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
773 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
775 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
777 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
779 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
781 $div.unbind('click',fns[0]);
782 var data = jQuery.data( $div[0], 'events' );
783 ok( !data, "Unbinding one function from toggle unbinds them all");
785 // Test Multi-Toggles
787 $div = jQuery("<div/>");
788 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
790 same( a, [1], "Check that a click worked." );
792 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
794 same( a, [1,2], "Check that a click worked with a second toggle." );
795 same( b, [1], "Check that a click worked with a second toggle." );
798 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
799 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
802 test(".live()/.die()", function() {
805 var submit = 0, div = 0, livea = 0, liveb = 0;
807 jQuery("div").live("submit", function(){ submit++; return false; });
808 jQuery("div").live("click", function(){ div++; });
809 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
810 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
812 // Nothing should trigger on the body
813 jQuery("body").trigger("click");
814 equals( submit, 0, "Click on body" );
815 equals( div, 0, "Click on body" );
816 equals( livea, 0, "Click on body" );
817 equals( liveb, 0, "Click on body" );
819 // This should trigger two events
820 submit = 0, div = 0, livea = 0, liveb = 0;
821 jQuery("div#nothiddendiv").trigger("click");
822 equals( submit, 0, "Click on div" );
823 equals( div, 1, "Click on div" );
824 equals( livea, 1, "Click on div" );
825 equals( liveb, 0, "Click on div" );
827 // This should trigger three events (w/ bubbling)
828 submit = 0, div = 0, livea = 0, liveb = 0;
829 jQuery("div#nothiddendivchild").trigger("click");
830 equals( submit, 0, "Click on inner div" );
831 equals( div, 2, "Click on inner div" );
832 equals( livea, 1, "Click on inner div" );
833 equals( liveb, 1, "Click on inner div" );
835 // This should trigger one submit
836 submit = 0, div = 0, livea = 0, liveb = 0;
837 jQuery("div#nothiddendivchild").trigger("submit");
838 equals( submit, 1, "Submit on div" );
839 equals( div, 0, "Submit on div" );
840 equals( livea, 0, "Submit on div" );
841 equals( liveb, 0, "Submit on div" );
843 // Make sure no other events were removed in the process
844 submit = 0, div = 0, livea = 0, liveb = 0;
845 jQuery("div#nothiddendivchild").trigger("click");
846 equals( submit, 0, "die Click on inner div" );
847 equals( div, 2, "die Click on inner div" );
848 equals( livea, 1, "die Click on inner div" );
849 equals( liveb, 1, "die Click on inner div" );
851 // Now make sure that the removal works
852 submit = 0, div = 0, livea = 0, liveb = 0;
853 jQuery("div#nothiddendivchild").die("click");
854 jQuery("div#nothiddendivchild").trigger("click");
855 equals( submit, 0, "die Click on inner div" );
856 equals( div, 2, "die Click on inner div" );
857 equals( livea, 1, "die Click on inner div" );
858 equals( liveb, 0, "die Click on inner div" );
860 // Make sure that the click wasn't removed too early
861 submit = 0, div = 0, livea = 0, liveb = 0;
862 jQuery("div#nothiddendiv").trigger("click");
863 equals( submit, 0, "die Click on inner div" );
864 equals( div, 1, "die Click on inner div" );
865 equals( livea, 1, "die Click on inner div" );
866 equals( liveb, 0, "die Click on inner div" );
868 // Make sure that stopPropgation doesn't stop live events
869 submit = 0, div = 0, livea = 0, liveb = 0;
870 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
871 jQuery("div#nothiddendivchild").trigger("click");
872 equals( submit, 0, "stopPropagation Click on inner div" );
873 equals( div, 1, "stopPropagation Click on inner div" );
874 equals( livea, 0, "stopPropagation Click on inner div" );
875 equals( liveb, 1, "stopPropagation Click on inner div" );
877 // Make sure click events only fire with primary click
878 submit = 0, div = 0, livea = 0, liveb = 0;
879 var event = jQuery.Event("click");
881 jQuery("div#nothiddendiv").trigger(event);
883 equals( livea, 0, "live secondary click" );
885 jQuery("div#nothiddendivchild").die("click");
886 jQuery("div#nothiddendiv").die("click");
887 jQuery("div").die("click");
888 jQuery("div").die("submit");
890 // Test binding with a different context
891 var clicked = 0, container = jQuery('#main')[0];
892 jQuery("#foo", container).live("click", function(e){ clicked++; });
893 jQuery("div").trigger('click');
894 jQuery("#foo").trigger('click');
895 jQuery("#main").trigger('click');
896 jQuery("body").trigger('click');
897 equals( clicked, 2, "live with a context" );
899 // Make sure the event is actually stored on the context
900 ok( jQuery.data(container, "events").live, "live with a context" );
902 // Test unbinding with a different context
903 jQuery("#foo", container).die("click");
904 jQuery("#foo").trigger('click');
905 equals( clicked, 2, "die with a context");
907 // Test binding with event data
908 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
909 jQuery("#foo").trigger("click").die("click");
911 // Test binding with trigger data
912 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
913 jQuery("#foo").trigger("click", true).die("click");
915 // Test binding with different this object
916 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
917 jQuery("#foo").trigger("click").die("click");
919 // Test binding with different this object, event data, and trigger data
920 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
921 equals( e.data, true, "live with with different this object, event data, and trigger data" );
922 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
923 equals( data, true, "live with with different this object, event data, and trigger data")
925 jQuery("#foo").trigger("click", true).die("click");
927 // Verify that return false prevents default action
928 jQuery("#anchor2").live("click", function(){ return false; });
929 var hash = window.location.hash;
930 jQuery("#anchor2").trigger("click");
931 equals( window.location.hash, hash, "return false worked" );
932 jQuery("#anchor2").die("click");
934 // Verify that .preventDefault() prevents default action
935 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
936 var hash = window.location.hash;
937 jQuery("#anchor2").trigger("click");
938 equals( window.location.hash, hash, "e.preventDefault() worked" );
939 jQuery("#anchor2").die("click");
941 // Test binding the same handler to multiple points
943 function callback(){ called++; return false; }
945 jQuery("#nothiddendiv").live("click", callback);
946 jQuery("#anchor2").live("click", callback);
948 jQuery("#nothiddendiv").trigger("click");
949 equals( called, 1, "Verify that only one click occurred." );
951 jQuery("#anchor2").trigger("click");
952 equals( called, 2, "Verify that only one click occurred." );
954 // Make sure that only one callback is removed
955 jQuery("#anchor2").die("click", callback);
957 jQuery("#nothiddendiv").trigger("click");
958 equals( called, 3, "Verify that only one click occurred." );
960 jQuery("#anchor2").trigger("click");
961 equals( called, 3, "Verify that no click occurred." );
963 // Make sure that it still works if the selector is the same,
964 // but the event type is different
965 jQuery("#nothiddendiv").live("foo", callback);
968 jQuery("#nothiddendiv").die("click", callback);
970 jQuery("#nothiddendiv").trigger("click");
971 equals( called, 3, "Verify that no click occurred." );
973 jQuery("#nothiddendiv").trigger("foo");
974 equals( called, 4, "Verify that one foo occurred." );
977 jQuery("#nothiddendiv").die("foo", callback);
979 // Make sure we don't loose the target by DOM modifications
980 // after the bubble already reached the liveHandler
981 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
983 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
984 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
986 jQuery("#nothiddendiv span").click();
987 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
988 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
991 jQuery("#nothiddendivchild").die("click");
993 // Verify that .live() ocurs and cancel buble in the same order as
994 // we would expect .bind() and .click() without delegation
995 var lived = 0, livee = 0;
997 // bind one pair in one order
998 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
999 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1001 jQuery('span#liveSpan1 a').click();
1002 equals( lived, 1, "Verify that only one first handler occurred." );
1003 equals( livee, 0, "Verify that second handler doesn't." );
1005 // and one pair in inverse
1006 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1007 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1011 jQuery('span#liveSpan2 a').click();
1012 equals( lived, 1, "Verify that only one first handler occurred." );
1013 equals( livee, 0, "Verify that second handler doesn't." );
1016 jQuery("span#liveSpan1 a").die("click")
1017 jQuery("span#liveSpan1").die("click");
1018 jQuery("span#liveSpan2 a").die("click");
1019 jQuery("span#liveSpan2").die("click");
1021 // Test this, target and currentTarget are correct
1022 jQuery('span#liveSpan1').live('click', function(e){
1023 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1024 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1025 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1028 jQuery('span#liveSpan1 a').click();
1030 jQuery('span#liveSpan1').die('click');
1032 // Work with deep selectors
1035 function clickB(){ livee++; }
1037 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1038 jQuery("#nothiddendiv div").live("click", clickB);
1039 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1041 equals( livee, 0, "No clicks, deep selector." );
1044 jQuery("#nothiddendivchild").trigger("click");
1045 equals( livee, 2, "Click, deep selector." );
1048 jQuery("#nothiddendivchild").trigger("mouseover");
1049 equals( livee, 1, "Mouseover, deep selector." );
1051 jQuery("#nothiddendiv div").die("mouseover");
1054 jQuery("#nothiddendivchild").trigger("click");
1055 equals( livee, 2, "Click, deep selector." );
1058 jQuery("#nothiddendivchild").trigger("mouseover");
1059 equals( livee, 0, "Mouseover, deep selector." );
1061 jQuery("#nothiddendiv div").die("click", clickB);
1064 jQuery("#nothiddendivchild").trigger("click");
1065 equals( livee, 1, "Click, deep selector." );
1067 jQuery("#nothiddendiv div").die("click");
1069 jQuery("#nothiddendiv div").live("blur", function(){
1070 ok( true, "Live div trigger blur." );
1073 jQuery("#nothiddendiv div").trigger("blur");
1075 jQuery("#nothiddendiv div").die("blur");
1078 test("die all bound events", function(){
1082 var div = jQuery("div#nothiddendivchild");
1084 div.live("click submit", function(){ count++; });
1087 div.trigger("click");
1088 div.trigger("submit");
1090 equals( count, 0, "Make sure no events were triggered." );
1093 test("live with multiple events", function(){
1097 var div = jQuery("div#nothiddendivchild");
1099 div.live("click submit", function(){ count++; });
1101 div.trigger("click");
1102 div.trigger("submit");
1104 equals( count, 2, "Make sure both the click and submit were triggered." );
1107 test("live with namespaces", function(){
1110 var count1 = 0, count2 = 0;
1112 jQuery("#liveSpan1").live("foo.bar", function(){
1116 jQuery("#liveSpan2").live("foo.zed", function(){
1120 jQuery("#liveSpan1").trigger("foo.bar");
1121 equals( count1, 1, "Got live foo.bar" );
1123 jQuery("#liveSpan2").trigger("foo.zed");
1124 equals( count2, 1, "Got live foo.zed" );
1127 jQuery("#liveSpan2").die("foo.zed");
1128 jQuery("#liveSpan1").trigger("foo.bar");
1130 equals( count1, 2, "Got live foo.bar after dieing foo.zed" );
1132 jQuery("#liveSpan2").trigger("foo.zed");
1133 equals( count2, 1, "Got live foo.zed" );
1136 jQuery("#liveSpan1").die("foo.bar");
1138 jQuery("#liveSpan1").trigger("foo.bar");
1139 equals( count1, 2, "Did not respond to foo.bar after dieing it" );
1141 jQuery("#liveSpan2").trigger("foo.zed");
1142 equals( count2, 1, "Did not trigger foo.zed again" );
1145 test("live with change", function(){
1146 var selectChange = 0, checkboxChange = 0;
1148 var select = jQuery("select[name='S1']")
1149 select.live("change", function() {
1153 var checkbox = jQuery("#check2"),
1154 checkboxFunction = function(){
1157 checkbox.live("change", checkboxFunction);
1159 // test click on select
1161 // second click that changed it
1163 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1164 select.trigger("change");
1165 equals( selectChange, 1, "Change on click." );
1167 // test keys on select
1169 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1170 select.trigger("change");
1171 equals( selectChange, 1, "Change on keyup." );
1173 // test click on checkbox
1174 checkbox.trigger("change");
1175 equals( checkboxChange, 1, "Change on checkbox." );
1177 // test before activate on radio
1179 // test blur/focus on textarea
1180 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1181 textarea.live("change", function() {
1185 textarea.val(oldVal + "foo");
1186 textarea.trigger("change");
1187 equals( textareaChange, 1, "Change on textarea." );
1189 textarea.val(oldVal);
1190 textarea.die("change");
1192 // test blur/focus on text
1193 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1194 text.live("change", function() {
1198 text.val(oldVal+"foo");
1199 text.trigger("change");
1200 equals( textChange, 1, "Change on text input." );
1202 text.val(oldTextVal);
1205 // test blur/focus on password
1206 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1207 password.live("change", function() {
1211 password.val(oldPasswordVal + "foo");
1212 password.trigger("change");
1213 equals( passwordChange, 1, "Change on password input." );
1215 password.val(oldPasswordVal);
1216 password.die("change");
1218 // make sure die works
1222 select.die("change");
1223 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1224 select.trigger("change");
1225 equals( selectChange, 0, "Die on click works." );
1228 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1229 select.trigger("change");
1230 equals( selectChange, 0, "Die on keyup works." );
1232 // die specific checkbox
1233 checkbox.die("change", checkboxFunction);
1234 checkbox.trigger("change");
1235 equals( checkboxChange, 1, "Die on checkbox." );
1238 test("live with submit", function() {
1239 var count1 = 0, count2 = 0;
1241 jQuery("#testForm").live("submit", function(ev) {
1243 ev.preventDefault();
1246 jQuery("body").live("submit", function(ev) {
1248 ev.preventDefault();
1251 if ( jQuery.support.submitBubbles ) {
1252 jQuery("#testForm input[name=sub1]")[0].click();
1256 jQuery("#testForm input[name=sub1]")[0].click();
1257 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1262 jQuery("#testForm").die("submit");
1263 jQuery("body").die("submit");
1266 test(".delegate()/.undelegate()", function() {
1269 var submit = 0, div = 0, livea = 0, liveb = 0;
1271 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1272 jQuery("#body").delegate("div", "click", function(){ div++; });
1273 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1274 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1276 // Nothing should trigger on the body
1277 jQuery("body").trigger("click");
1278 equals( submit, 0, "Click on body" );
1279 equals( div, 0, "Click on body" );
1280 equals( livea, 0, "Click on body" );
1281 equals( liveb, 0, "Click on body" );
1283 // This should trigger two events
1284 submit = 0, div = 0, livea = 0, liveb = 0;
1285 jQuery("div#nothiddendiv").trigger("click");
1286 equals( submit, 0, "Click on div" );
1287 equals( div, 1, "Click on div" );
1288 equals( livea, 1, "Click on div" );
1289 equals( liveb, 0, "Click on div" );
1291 // This should trigger three events (w/ bubbling)
1292 submit = 0, div = 0, livea = 0, liveb = 0;
1293 jQuery("div#nothiddendivchild").trigger("click");
1294 equals( submit, 0, "Click on inner div" );
1295 equals( div, 2, "Click on inner div" );
1296 equals( livea, 1, "Click on inner div" );
1297 equals( liveb, 1, "Click on inner div" );
1299 // This should trigger one submit
1300 submit = 0, div = 0, livea = 0, liveb = 0;
1301 jQuery("div#nothiddendivchild").trigger("submit");
1302 equals( submit, 1, "Submit on div" );
1303 equals( div, 0, "Submit on div" );
1304 equals( livea, 0, "Submit on div" );
1305 equals( liveb, 0, "Submit on div" );
1307 // Make sure no other events were removed in the process
1308 submit = 0, div = 0, livea = 0, liveb = 0;
1309 jQuery("div#nothiddendivchild").trigger("click");
1310 equals( submit, 0, "undelegate Click on inner div" );
1311 equals( div, 2, "undelegate Click on inner div" );
1312 equals( livea, 1, "undelegate Click on inner div" );
1313 equals( liveb, 1, "undelegate Click on inner div" );
1315 // Now make sure that the removal works
1316 submit = 0, div = 0, livea = 0, liveb = 0;
1317 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1318 jQuery("div#nothiddendivchild").trigger("click");
1319 equals( submit, 0, "undelegate Click on inner div" );
1320 equals( div, 2, "undelegate Click on inner div" );
1321 equals( livea, 1, "undelegate Click on inner div" );
1322 equals( liveb, 0, "undelegate Click on inner div" );
1324 // Make sure that the click wasn't removed too early
1325 submit = 0, div = 0, livea = 0, liveb = 0;
1326 jQuery("div#nothiddendiv").trigger("click");
1327 equals( submit, 0, "undelegate Click on inner div" );
1328 equals( div, 1, "undelegate Click on inner div" );
1329 equals( livea, 1, "undelegate Click on inner div" );
1330 equals( liveb, 0, "undelegate Click on inner div" );
1332 // Make sure that stopPropgation doesn't stop live events
1333 submit = 0, div = 0, livea = 0, liveb = 0;
1334 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1335 jQuery("div#nothiddendivchild").trigger("click");
1336 equals( submit, 0, "stopPropagation Click on inner div" );
1337 equals( div, 1, "stopPropagation Click on inner div" );
1338 equals( livea, 0, "stopPropagation Click on inner div" );
1339 equals( liveb, 1, "stopPropagation Click on inner div" );
1341 // Make sure click events only fire with primary click
1342 submit = 0, div = 0, livea = 0, liveb = 0;
1343 var event = jQuery.Event("click");
1345 jQuery("div#nothiddendiv").trigger(event);
1347 equals( livea, 0, "delegate secondary click" );
1349 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1350 jQuery("#body").undelegate("div#nothiddendiv", "click");
1351 jQuery("#body").undelegate("div", "click");
1352 jQuery("#body").undelegate("div", "submit");
1354 // Test binding with a different context
1355 var clicked = 0, container = jQuery('#main')[0];
1356 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1357 jQuery("div").trigger('click');
1358 jQuery("#foo").trigger('click');
1359 jQuery("#main").trigger('click');
1360 jQuery("body").trigger('click');
1361 equals( clicked, 2, "delegate with a context" );
1363 // Make sure the event is actually stored on the context
1364 ok( jQuery.data(container, "events").live, "delegate with a context" );
1366 // Test unbinding with a different context
1367 jQuery("#main").undelegate("#foo", "click");
1368 jQuery("#foo").trigger('click');
1369 equals( clicked, 2, "undelegate with a context");
1371 // Test binding with event data
1372 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1373 jQuery("#foo").trigger("click");
1374 jQuery("#body").undelegate("#foo", "click");
1376 // Test binding with trigger data
1377 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1378 jQuery("#foo").trigger("click", true);
1379 jQuery("#body").undelegate("#foo", "click");
1381 // Test binding with different this object
1382 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1383 jQuery("#foo").trigger("click");
1384 jQuery("#body").undelegate("#foo", "click");
1386 // Test binding with different this object, event data, and trigger data
1387 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1388 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1389 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1390 equals( data, true, "delegate with with different this object, event data, and trigger data")
1391 }, { foo: "bar" }));
1392 jQuery("#foo").trigger("click", true);
1393 jQuery("#body").undelegate("#foo", "click");
1395 // Verify that return false prevents default action
1396 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1397 var hash = window.location.hash;
1398 jQuery("#anchor2").trigger("click");
1399 equals( window.location.hash, hash, "return false worked" );
1400 jQuery("#body").undelegate("#anchor2", "click");
1402 // Verify that .preventDefault() prevents default action
1403 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1404 var hash = window.location.hash;
1405 jQuery("#anchor2").trigger("click");
1406 equals( window.location.hash, hash, "e.preventDefault() worked" );
1407 jQuery("#body").undelegate("#anchor2", "click");
1409 // Test binding the same handler to multiple points
1411 function callback(){ called++; return false; }
1413 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1414 jQuery("#body").delegate("#anchor2", "click", callback);
1416 jQuery("#nothiddendiv").trigger("click");
1417 equals( called, 1, "Verify that only one click occurred." );
1419 jQuery("#anchor2").trigger("click");
1420 equals( called, 2, "Verify that only one click occurred." );
1422 // Make sure that only one callback is removed
1423 jQuery("#body").undelegate("#anchor2", "click", callback);
1425 jQuery("#nothiddendiv").trigger("click");
1426 equals( called, 3, "Verify that only one click occurred." );
1428 jQuery("#anchor2").trigger("click");
1429 equals( called, 3, "Verify that no click occurred." );
1431 // Make sure that it still works if the selector is the same,
1432 // but the event type is different
1433 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1436 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1438 jQuery("#nothiddendiv").trigger("click");
1439 equals( called, 3, "Verify that no click occurred." );
1441 jQuery("#nothiddendiv").trigger("foo");
1442 equals( called, 4, "Verify that one foo occurred." );
1445 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1447 // Make sure we don't loose the target by DOM modifications
1448 // after the bubble already reached the liveHandler
1449 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1451 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1452 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1454 jQuery("#nothiddendiv span").click();
1455 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1456 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1459 jQuery("#body").undelegate("#nothiddendivchild", "click");
1461 // Verify that .live() ocurs and cancel buble in the same order as
1462 // we would expect .bind() and .click() without delegation
1463 var lived = 0, livee = 0;
1465 // bind one pair in one order
1466 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1467 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1469 jQuery('span#liveSpan1 a').click();
1470 equals( lived, 1, "Verify that only one first handler occurred." );
1471 equals( livee, 0, "Verify that second handler doesn't." );
1473 // and one pair in inverse
1474 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1475 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1479 jQuery('span#liveSpan2 a').click();
1480 equals( lived, 1, "Verify that only one first handler occurred." );
1481 equals( livee, 0, "Verify that second handler doesn't." );
1484 jQuery("#body").undelegate("click");
1486 // Test this, target and currentTarget are correct
1487 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1488 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1489 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1490 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1493 jQuery('span#liveSpan1 a').click();
1495 jQuery("#body").undelegate('span#liveSpan1', 'click');
1497 // Work with deep selectors
1500 function clickB(){ livee++; }
1502 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1503 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1504 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1506 equals( livee, 0, "No clicks, deep selector." );
1509 jQuery("#nothiddendivchild").trigger("click");
1510 equals( livee, 2, "Click, deep selector." );
1513 jQuery("#nothiddendivchild").trigger("mouseover");
1514 equals( livee, 1, "Mouseover, deep selector." );
1516 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1519 jQuery("#nothiddendivchild").trigger("click");
1520 equals( livee, 2, "Click, deep selector." );
1523 jQuery("#nothiddendivchild").trigger("mouseover");
1524 equals( livee, 0, "Mouseover, deep selector." );
1526 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1529 jQuery("#nothiddendivchild").trigger("click");
1530 equals( livee, 1, "Click, deep selector." );
1532 jQuery("#body").undelegate("#nothiddendiv div", "click");
1535 test("undelegate all bound events", function(){
1539 var div = jQuery("#body");
1541 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1544 jQuery("div#nothiddendivchild").trigger("click");
1545 jQuery("div#nothiddendivchild").trigger("submit");
1547 equals( count, 0, "Make sure no events were triggered." );
1550 test("delegate with multiple events", function(){
1554 var div = jQuery("#body");
1556 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1558 jQuery("div#nothiddendivchild").trigger("click");
1559 jQuery("div#nothiddendivchild").trigger("submit");
1561 equals( count, 2, "Make sure both the click and submit were triggered." );
1563 jQuery("#body").undelegate();
1566 test("delegate with change", function(){
1567 var selectChange = 0, checkboxChange = 0;
1569 var select = jQuery("select[name='S1']");
1570 jQuery("#body").delegate("select[name='S1']", "change", function() {
1574 var checkbox = jQuery("#check2"),
1575 checkboxFunction = function(){
1578 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1580 // test click on select
1582 // second click that changed it
1584 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1585 select.trigger("change");
1586 equals( selectChange, 1, "Change on click." );
1588 // test keys on select
1590 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1591 select.trigger("change");
1592 equals( selectChange, 1, "Change on keyup." );
1594 // test click on checkbox
1595 checkbox.trigger("change");
1596 equals( checkboxChange, 1, "Change on checkbox." );
1598 // test before activate on radio
1600 // test blur/focus on textarea
1601 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1602 jQuery("#body").delegate("#area1", "change", function() {
1606 textarea.val(oldVal + "foo");
1607 textarea.trigger("change");
1608 equals( textareaChange, 1, "Change on textarea." );
1610 textarea.val(oldVal);
1611 jQuery("#body").undelegate("#area1", "change");
1613 // test blur/focus on text
1614 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1615 jQuery("#body").delegate("#name", "change", function() {
1619 text.val(oldVal+"foo");
1620 text.trigger("change");
1621 equals( textChange, 1, "Change on text input." );
1623 text.val(oldTextVal);
1624 jQuery("#body").die("change");
1626 // test blur/focus on password
1627 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1628 jQuery("#body").delegate("#name", "change", function() {
1632 password.val(oldPasswordVal + "foo");
1633 password.trigger("change");
1634 equals( passwordChange, 1, "Change on password input." );
1636 password.val(oldPasswordVal);
1637 jQuery("#body").undelegate("#name", "change");
1639 // make sure die works
1643 jQuery("#body").undelegate("select[name='S1']", "change");
1644 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1645 select.trigger("change");
1646 equals( selectChange, 0, "Die on click works." );
1649 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1650 select.trigger("change");
1651 equals( selectChange, 0, "Die on keyup works." );
1653 // die specific checkbox
1654 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1655 checkbox.trigger("change");
1656 equals( checkboxChange, 1, "Die on checkbox." );
1659 test("delegate with submit", function() {
1660 var count1 = 0, count2 = 0;
1662 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1664 ev.preventDefault();
1667 jQuery(document).delegate("body", "submit", function(ev) {
1669 ev.preventDefault();
1672 if ( jQuery.support.submitBubbles ) {
1673 jQuery("#testForm input[name=sub1]")[0].click();
1677 jQuery("#testForm input[name=sub1]")[0].click();
1678 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1683 jQuery("#body").undelegate();
1684 jQuery(document).undelegate();
1687 test("Non DOM element events", function() {
1691 .bind('nonelementglobal', function(e) {
1692 ok( true, "Global event on non-DOM annonymos object triggered" );
1698 .bind('nonelementobj', function(e) {
1699 ok( true, "Event on non-DOM object triggered" );
1700 }).bind('nonelementglobal', function() {
1701 ok( true, "Global event on non-DOM object triggered" );
1704 jQuery(o).trigger('nonelementobj');
1705 jQuery.event.trigger('nonelementglobal');
1709 test("jQuery(function($) {})", function() {
1711 jQuery(function($) {
1712 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");
1717 test("event properties", function() {
1719 jQuery("#simon1").click(function(event) {
1720 ok( event.timeStamp, "assert event.timeStamp is present" );