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("unbind(type)", function() {
379 var $elem = jQuery("#firstp"),
383 ok( false, message );
386 message = "unbind passing function";
387 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
389 message = "unbind all from event";
390 $elem.bind('error', error).unbind('error').triggerHandler('error');
392 message = "unbind all";
393 $elem.bind('error', error).unbind().triggerHandler('error');
395 message = "unbind many with function";
396 $elem.bind('error error2',error)
397 .unbind('error error2', error )
398 .trigger('error').triggerHandler('error2');
400 message = "unbind many"; // #3538
401 $elem.bind('error error2',error)
402 .unbind('error error2')
403 .trigger('error').triggerHandler('error2');
405 message = "unbind without a type or handler";
406 $elem.bind("error error2.test",error)
408 .trigger("error").triggerHandler("error2");
411 test("unbind(eventObject)", function() {
414 var $elem = jQuery("#firstp"),
417 function assert( expected ){
419 $elem.trigger('foo').triggerHandler('bar');
420 equals( num, expected, "Check the right handlers are triggered" );
424 // This handler shouldn't be unbound
425 .bind('foo', function(){
428 .bind('foo', function(e){
433 .bind('bar', function(){
447 test("hover()", function() {
449 handler1 = function( event ) { ++times; },
450 handler2 = function( event ) { ++times; };
453 .hover(handler1, handler2)
454 .mouseenter().mouseleave()
455 .unbind("mouseenter", handler1)
456 .unbind("mouseleave", handler2)
458 .mouseenter().mouseleave()
459 .unbind("mouseenter mouseleave", handler1)
460 .mouseenter().mouseleave();
462 equals( times, 4, "hover handlers fired" );
465 test("trigger() shortcuts", function() {
467 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
468 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
469 equals( close.length, 0, "Context element does not exist, length must be zero" );
470 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
474 jQuery("#check1").click(function() {
475 ok( true, "click event handler for checkbox gets fired twice, see #815" );
479 jQuery('#firstp')[0].onclick = function(event) {
482 jQuery('#firstp').click();
483 equals( counter, 1, "Check that click, triggers onclick event handler also" );
485 var clickCounter = 0;
486 jQuery('#simon1')[0].onclick = function(event) {
489 jQuery('#simon1').click();
490 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
492 jQuery('<img />').load(function(){
493 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
497 test("trigger() bubbling", function() {
500 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
502 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
503 jQuery("html").bind("click", function(e){ html++; });
504 jQuery("body").bind("click", function(e){ body++; });
505 jQuery("#main").bind("click", function(e){ main++; });
506 jQuery("#ap").bind("click", function(){ ap++; return false; });
508 jQuery("html").trigger("click");
509 equals( doc, 1, "HTML bubble" );
510 equals( html, 1, "HTML bubble" );
512 jQuery("body").trigger("click");
513 equals( doc, 2, "Body bubble" );
514 equals( html, 2, "Body bubble" );
515 equals( body, 1, "Body bubble" );
517 jQuery("#main").trigger("click");
518 equals( doc, 3, "Main bubble" );
519 equals( html, 3, "Main bubble" );
520 equals( body, 2, "Main bubble" );
521 equals( main, 1, "Main bubble" );
523 jQuery("#ap").trigger("click");
524 equals( doc, 3, "ap bubble" );
525 equals( html, 3, "ap bubble" );
526 equals( body, 2, "ap bubble" );
527 equals( main, 1, "ap bubble" );
528 equals( ap, 1, "ap bubble" );
531 test("trigger(type, [data], [fn])", function() {
534 var handler = function(event, a, b, c) {
535 equals( event.type, "click", "check passed data" );
536 equals( a, 1, "check passed data" );
537 equals( b, "2", "check passed data" );
538 equals( c, "abc", "check passed data" );
542 var $elem = jQuery("#firstp");
544 // Simulate a "native" click
545 $elem[0].click = function(){
546 ok( true, "Native call was triggered" );
549 // Triggers handlrs and native
551 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
553 // Simulate a "native" click
554 $elem[0].click = function(){
555 ok( false, "Native call was triggered" );
558 // Trigger only the handlers (no native)
560 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
564 jQuery('#form input:first').hide().trigger('focus');
568 ok( pass, "Trigger focus on hidden element" );
572 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
576 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
578 var form = jQuery("<form action=''></form>").appendTo("body");
580 // Make sure it can be prevented locally
581 form.submit(function(){
582 ok( true, "Local bind still works." );
587 form.trigger("submit");
589 form.unbind("submit");
591 jQuery(document).submit(function(){
592 ok( true, "Make sure bubble works up to document." );
597 form.trigger("submit");
599 jQuery(document).unbind("submit");
604 test("jQuery.Event.currentTarget", function(){
607 test("trigger(eventObject, [data], [fn])", function() {
610 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
611 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
613 var event = jQuery.Event("noNew");
614 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
615 equals( event.type, "noNew", "Verify its type" );
617 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
618 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
619 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
621 event.preventDefault();
622 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
623 event.stopPropagation();
624 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
626 event.isPropagationStopped = function(){ return false };
627 event.stopImmediatePropagation();
628 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
629 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
631 $parent.bind('foo',function(e){
633 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
634 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
635 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
636 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
639 // test with an event object
640 event = new jQuery.Event("foo");
641 event.secret = 'boo!';
642 $child.trigger(event);
644 // test with a literal object
645 $child.trigger({type:'foo', secret:'boo!'});
650 ok( false, "This assertion shouldn't be reached");
653 $parent.bind('foo', error );
655 $child.bind('foo',function(e, a, b, c ){
656 equals( arguments.length, 4, "Check arguments length");
657 equals( a, 1, "Check first custom argument");
658 equals( b, 2, "Check second custom argument");
659 equals( c, 3, "Check third custom argument");
661 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
662 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
663 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
666 e.stopImmediatePropagation();
671 // We should add this back in when we want to test the order
672 // in which event handlers are iterated.
673 //$child.bind('foo', error );
675 event = new jQuery.Event("foo");
676 $child.trigger( event, [1,2,3] ).unbind();
677 equals( event.result, "result", "Check event.result attribute");
679 // Will error if it bubbles
680 $child.triggerHandler('foo');
683 $parent.unbind().remove();
686 test("jQuery.Event.currentTarget", function(){
690 $elem = jQuery('<button>a</button>').click(function(e){
691 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
695 $elem.trigger('click');
701 test("toggle(Function, Function, ...)", function() {
705 fn1 = function(e) { count++; },
706 fn2 = function(e) { count--; },
707 preventDefault = function(e) { e.preventDefault() },
708 link = jQuery('#mark');
709 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
710 equals( count, 1, "Check for toggle(fn, fn)" );
712 jQuery("#firstp").toggle(function () {
713 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
714 }, function() {}).trigger("click", [ 1, 2, 3 ]);
717 jQuery("#simon1").one("click", function() {
718 ok( true, "Execute event only once" );
719 jQuery(this).toggle(function() {
720 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
722 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
725 }).click().click().click();
740 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
742 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
744 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
746 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
748 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
750 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
752 $div.unbind('click',fns[0]);
753 var data = jQuery.data( $div[0], 'events' );
754 ok( !data, "Unbinding one function from toggle unbinds them all");
756 // Test Multi-Toggles
758 $div = jQuery("<div/>");
759 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
761 same( a, [1], "Check that a click worked." );
763 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
765 same( a, [1,2], "Check that a click worked with a second toggle." );
766 same( b, [1], "Check that a click worked with a second toggle." );
769 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
770 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
773 test(".live()/.die()", function() {
776 var submit = 0, div = 0, livea = 0, liveb = 0;
778 jQuery("div").live("submit", function(){ submit++; return false; });
779 jQuery("div").live("click", function(){ div++; });
780 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
781 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
783 // Nothing should trigger on the body
784 jQuery("body").trigger("click");
785 equals( submit, 0, "Click on body" );
786 equals( div, 0, "Click on body" );
787 equals( livea, 0, "Click on body" );
788 equals( liveb, 0, "Click on body" );
790 // This should trigger two events
791 submit = 0, div = 0, livea = 0, liveb = 0;
792 jQuery("div#nothiddendiv").trigger("click");
793 equals( submit, 0, "Click on div" );
794 equals( div, 1, "Click on div" );
795 equals( livea, 1, "Click on div" );
796 equals( liveb, 0, "Click on div" );
798 // This should trigger three events (w/ bubbling)
799 submit = 0, div = 0, livea = 0, liveb = 0;
800 jQuery("div#nothiddendivchild").trigger("click");
801 equals( submit, 0, "Click on inner div" );
802 equals( div, 2, "Click on inner div" );
803 equals( livea, 1, "Click on inner div" );
804 equals( liveb, 1, "Click on inner div" );
806 // This should trigger one submit
807 submit = 0, div = 0, livea = 0, liveb = 0;
808 jQuery("div#nothiddendivchild").trigger("submit");
809 equals( submit, 1, "Submit on div" );
810 equals( div, 0, "Submit on div" );
811 equals( livea, 0, "Submit on div" );
812 equals( liveb, 0, "Submit on div" );
814 // Make sure no other events were removed in the process
815 submit = 0, div = 0, livea = 0, liveb = 0;
816 jQuery("div#nothiddendivchild").trigger("click");
817 equals( submit, 0, "die Click on inner div" );
818 equals( div, 2, "die Click on inner div" );
819 equals( livea, 1, "die Click on inner div" );
820 equals( liveb, 1, "die Click on inner div" );
822 // Now make sure that the removal works
823 submit = 0, div = 0, livea = 0, liveb = 0;
824 jQuery("div#nothiddendivchild").die("click");
825 jQuery("div#nothiddendivchild").trigger("click");
826 equals( submit, 0, "die Click on inner div" );
827 equals( div, 2, "die Click on inner div" );
828 equals( livea, 1, "die Click on inner div" );
829 equals( liveb, 0, "die Click on inner div" );
831 // Make sure that the click wasn't removed too early
832 submit = 0, div = 0, livea = 0, liveb = 0;
833 jQuery("div#nothiddendiv").trigger("click");
834 equals( submit, 0, "die Click on inner div" );
835 equals( div, 1, "die Click on inner div" );
836 equals( livea, 1, "die Click on inner div" );
837 equals( liveb, 0, "die Click on inner div" );
839 // Make sure that stopPropgation doesn't stop live events
840 submit = 0, div = 0, livea = 0, liveb = 0;
841 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
842 jQuery("div#nothiddendivchild").trigger("click");
843 equals( submit, 0, "stopPropagation Click on inner div" );
844 equals( div, 1, "stopPropagation Click on inner div" );
845 equals( livea, 0, "stopPropagation Click on inner div" );
846 equals( liveb, 1, "stopPropagation Click on inner div" );
848 // Make sure click events only fire with primary click
849 submit = 0, div = 0, livea = 0, liveb = 0;
850 var event = jQuery.Event("click");
852 jQuery("div#nothiddendiv").trigger(event);
854 equals( livea, 0, "live secondary click" );
856 jQuery("div#nothiddendivchild").die("click");
857 jQuery("div#nothiddendiv").die("click");
858 jQuery("div").die("click");
859 jQuery("div").die("submit");
861 // Test binding with a different context
862 var clicked = 0, container = jQuery('#main')[0];
863 jQuery("#foo", container).live("click", function(e){ clicked++; });
864 jQuery("div").trigger('click');
865 jQuery("#foo").trigger('click');
866 jQuery("#main").trigger('click');
867 jQuery("body").trigger('click');
868 equals( clicked, 2, "live with a context" );
870 // Make sure the event is actually stored on the context
871 ok( jQuery.data(container, "events").live, "live with a context" );
873 // Test unbinding with a different context
874 jQuery("#foo", container).die("click");
875 jQuery("#foo").trigger('click');
876 equals( clicked, 2, "die with a context");
878 // Test binding with event data
879 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
880 jQuery("#foo").trigger("click").die("click");
882 // Test binding with trigger data
883 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
884 jQuery("#foo").trigger("click", true).die("click");
886 // Test binding with different this object
887 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
888 jQuery("#foo").trigger("click").die("click");
890 // Test binding with different this object, event data, and trigger data
891 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
892 equals( e.data, true, "live with with different this object, event data, and trigger data" );
893 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
894 equals( data, true, "live with with different this object, event data, and trigger data")
896 jQuery("#foo").trigger("click", true).die("click");
898 // Verify that return false prevents default action
899 jQuery("#anchor2").live("click", function(){ return false; });
900 var hash = window.location.hash;
901 jQuery("#anchor2").trigger("click");
902 equals( window.location.hash, hash, "return false worked" );
903 jQuery("#anchor2").die("click");
905 // Verify that .preventDefault() prevents default action
906 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
907 var hash = window.location.hash;
908 jQuery("#anchor2").trigger("click");
909 equals( window.location.hash, hash, "e.preventDefault() worked" );
910 jQuery("#anchor2").die("click");
912 // Test binding the same handler to multiple points
914 function callback(){ called++; return false; }
916 jQuery("#nothiddendiv").live("click", callback);
917 jQuery("#anchor2").live("click", callback);
919 jQuery("#nothiddendiv").trigger("click");
920 equals( called, 1, "Verify that only one click occurred." );
922 jQuery("#anchor2").trigger("click");
923 equals( called, 2, "Verify that only one click occurred." );
925 // Make sure that only one callback is removed
926 jQuery("#anchor2").die("click", callback);
928 jQuery("#nothiddendiv").trigger("click");
929 equals( called, 3, "Verify that only one click occurred." );
931 jQuery("#anchor2").trigger("click");
932 equals( called, 3, "Verify that no click occurred." );
934 // Make sure that it still works if the selector is the same,
935 // but the event type is different
936 jQuery("#nothiddendiv").live("foo", callback);
939 jQuery("#nothiddendiv").die("click", callback);
941 jQuery("#nothiddendiv").trigger("click");
942 equals( called, 3, "Verify that no click occurred." );
944 jQuery("#nothiddendiv").trigger("foo");
945 equals( called, 4, "Verify that one foo occurred." );
948 jQuery("#nothiddendiv").die("foo", callback);
950 // Make sure we don't loose the target by DOM modifications
951 // after the bubble already reached the liveHandler
952 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
954 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
955 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
957 jQuery("#nothiddendiv span").click();
958 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
959 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
962 jQuery("#nothiddendivchild").die("click");
964 // Verify that .live() ocurs and cancel buble in the same order as
965 // we would expect .bind() and .click() without delegation
966 var lived = 0, livee = 0;
968 // bind one pair in one order
969 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
970 jQuery('span#liveSpan1').live('click', function(){ livee++; });
972 jQuery('span#liveSpan1 a').click();
973 equals( lived, 1, "Verify that only one first handler occurred." );
974 equals( livee, 0, "Verify that second handler doesn't." );
976 // and one pair in inverse
977 jQuery('span#liveSpan2').live('click', function(){ livee++; });
978 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
982 jQuery('span#liveSpan2 a').click();
983 equals( lived, 1, "Verify that only one first handler occurred." );
984 equals( livee, 0, "Verify that second handler doesn't." );
987 jQuery("span#liveSpan1 a").die("click")
988 jQuery("span#liveSpan1").die("click");
989 jQuery("span#liveSpan2 a").die("click");
990 jQuery("span#liveSpan2").die("click");
992 // Test this, target and currentTarget are correct
993 jQuery('span#liveSpan1').live('click', function(e){
994 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
995 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
996 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
999 jQuery('span#liveSpan1 a').click();
1001 jQuery('span#liveSpan1').die('click');
1003 // Work with deep selectors
1006 function clickB(){ livee++; }
1008 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1009 jQuery("#nothiddendiv div").live("click", clickB);
1010 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1012 equals( livee, 0, "No clicks, deep selector." );
1015 jQuery("#nothiddendivchild").trigger("click");
1016 equals( livee, 2, "Click, deep selector." );
1019 jQuery("#nothiddendivchild").trigger("mouseover");
1020 equals( livee, 1, "Mouseover, deep selector." );
1022 jQuery("#nothiddendiv div").die("mouseover");
1025 jQuery("#nothiddendivchild").trigger("click");
1026 equals( livee, 2, "Click, deep selector." );
1029 jQuery("#nothiddendivchild").trigger("mouseover");
1030 equals( livee, 0, "Mouseover, deep selector." );
1032 jQuery("#nothiddendiv div").die("click", clickB);
1035 jQuery("#nothiddendivchild").trigger("click");
1036 equals( livee, 1, "Click, deep selector." );
1038 jQuery("#nothiddendiv div").die("click");
1040 jQuery("#nothiddendiv div").live("blur", function(){
1041 ok( true, "Live div trigger blur." );
1044 jQuery("#nothiddendiv div").trigger("blur");
1046 jQuery("#nothiddendiv div").die("blur");
1049 test("die all bound events", function(){
1053 var div = jQuery("div#nothiddendivchild");
1055 div.live("click submit", function(){ count++; });
1058 div.trigger("click");
1059 div.trigger("submit");
1061 equals( count, 0, "Make sure no events were triggered." );
1064 test("live with multiple events", function(){
1068 var div = jQuery("div#nothiddendivchild");
1070 div.live("click submit", function(){ count++; });
1072 div.trigger("click");
1073 div.trigger("submit");
1075 equals( count, 2, "Make sure both the click and submit were triggered." );
1078 test("live with namespaces", function(){
1081 var count1 = 0, count2 = 0;
1083 jQuery("#liveSpan1").live("foo.bar", function(){
1087 jQuery("#liveSpan2").live("foo.zed", function(){
1091 jQuery("#liveSpan1").trigger("foo.bar");
1092 equals( count1, 1, "Got live foo.bar" );
1094 jQuery("#liveSpan2").trigger("foo.zed");
1095 equals( count2, 1, "Got live foo.zed" );
1098 jQuery("#liveSpan2").die("foo.zed");
1099 jQuery("#liveSpan1").trigger("foo.bar");
1101 equals( count1, 2, "Got live foo.bar after dieing foo.zed" );
1103 jQuery("#liveSpan2").trigger("foo.zed");
1104 equals( count2, 1, "Got live foo.zed" );
1107 jQuery("#liveSpan1").die("foo.bar");
1109 jQuery("#liveSpan1").trigger("foo.bar");
1110 equals( count1, 2, "Did not respond to foo.bar after dieing it" );
1112 jQuery("#liveSpan2").trigger("foo.zed");
1113 equals( count2, 1, "Did not trigger foo.zed again" );
1116 test("live with change", function(){
1117 var selectChange = 0, checkboxChange = 0;
1119 var select = jQuery("select[name='S1']")
1120 select.live("change", function() {
1124 var checkbox = jQuery("#check2"),
1125 checkboxFunction = function(){
1128 checkbox.live("change", checkboxFunction);
1130 // test click on select
1132 // second click that changed it
1134 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1135 select.trigger("change");
1136 equals( selectChange, 1, "Change on click." );
1138 // test keys on select
1140 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1141 select.trigger("change");
1142 equals( selectChange, 1, "Change on keyup." );
1144 // test click on checkbox
1145 checkbox.trigger("change");
1146 equals( checkboxChange, 1, "Change on checkbox." );
1148 // test before activate on radio
1150 // test blur/focus on textarea
1151 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1152 textarea.live("change", function() {
1156 textarea.val(oldVal + "foo");
1157 textarea.trigger("change");
1158 equals( textareaChange, 1, "Change on textarea." );
1160 textarea.val(oldVal);
1161 textarea.die("change");
1163 // test blur/focus on text
1164 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1165 text.live("change", function() {
1169 text.val(oldVal+"foo");
1170 text.trigger("change");
1171 equals( textChange, 1, "Change on text input." );
1173 text.val(oldTextVal);
1176 // test blur/focus on password
1177 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1178 password.live("change", function() {
1182 password.val(oldPasswordVal + "foo");
1183 password.trigger("change");
1184 equals( passwordChange, 1, "Change on password input." );
1186 password.val(oldPasswordVal);
1187 password.die("change");
1189 // make sure die works
1193 select.die("change");
1194 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1195 select.trigger("change");
1196 equals( selectChange, 0, "Die on click works." );
1199 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1200 select.trigger("change");
1201 equals( selectChange, 0, "Die on keyup works." );
1203 // die specific checkbox
1204 checkbox.die("change", checkboxFunction);
1205 checkbox.trigger("change");
1206 equals( checkboxChange, 1, "Die on checkbox." );
1209 test("live with submit", function() {
1210 var count1 = 0, count2 = 0;
1212 jQuery("#testForm").live("submit", function(ev) {
1214 ev.preventDefault();
1217 jQuery("body").live("submit", function(ev) {
1219 ev.preventDefault();
1222 if ( jQuery.support.submitBubbles ) {
1223 jQuery("#testForm input[name=sub1]")[0].click();
1227 jQuery("#testForm input[name=sub1]")[0].click();
1228 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1233 jQuery("#testForm").die("submit");
1234 jQuery("body").die("submit");
1237 test(".delegate()/.undelegate()", function() {
1240 var submit = 0, div = 0, livea = 0, liveb = 0;
1242 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1243 jQuery("#body").delegate("div", "click", function(){ div++; });
1244 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1245 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1247 // Nothing should trigger on the body
1248 jQuery("body").trigger("click");
1249 equals( submit, 0, "Click on body" );
1250 equals( div, 0, "Click on body" );
1251 equals( livea, 0, "Click on body" );
1252 equals( liveb, 0, "Click on body" );
1254 // This should trigger two events
1255 submit = 0, div = 0, livea = 0, liveb = 0;
1256 jQuery("div#nothiddendiv").trigger("click");
1257 equals( submit, 0, "Click on div" );
1258 equals( div, 1, "Click on div" );
1259 equals( livea, 1, "Click on div" );
1260 equals( liveb, 0, "Click on div" );
1262 // This should trigger three events (w/ bubbling)
1263 submit = 0, div = 0, livea = 0, liveb = 0;
1264 jQuery("div#nothiddendivchild").trigger("click");
1265 equals( submit, 0, "Click on inner div" );
1266 equals( div, 2, "Click on inner div" );
1267 equals( livea, 1, "Click on inner div" );
1268 equals( liveb, 1, "Click on inner div" );
1270 // This should trigger one submit
1271 submit = 0, div = 0, livea = 0, liveb = 0;
1272 jQuery("div#nothiddendivchild").trigger("submit");
1273 equals( submit, 1, "Submit on div" );
1274 equals( div, 0, "Submit on div" );
1275 equals( livea, 0, "Submit on div" );
1276 equals( liveb, 0, "Submit on div" );
1278 // Make sure no other events were removed in the process
1279 submit = 0, div = 0, livea = 0, liveb = 0;
1280 jQuery("div#nothiddendivchild").trigger("click");
1281 equals( submit, 0, "undelegate Click on inner div" );
1282 equals( div, 2, "undelegate Click on inner div" );
1283 equals( livea, 1, "undelegate Click on inner div" );
1284 equals( liveb, 1, "undelegate Click on inner div" );
1286 // Now make sure that the removal works
1287 submit = 0, div = 0, livea = 0, liveb = 0;
1288 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1289 jQuery("div#nothiddendivchild").trigger("click");
1290 equals( submit, 0, "undelegate Click on inner div" );
1291 equals( div, 2, "undelegate Click on inner div" );
1292 equals( livea, 1, "undelegate Click on inner div" );
1293 equals( liveb, 0, "undelegate Click on inner div" );
1295 // Make sure that the click wasn't removed too early
1296 submit = 0, div = 0, livea = 0, liveb = 0;
1297 jQuery("div#nothiddendiv").trigger("click");
1298 equals( submit, 0, "undelegate Click on inner div" );
1299 equals( div, 1, "undelegate Click on inner div" );
1300 equals( livea, 1, "undelegate Click on inner div" );
1301 equals( liveb, 0, "undelegate Click on inner div" );
1303 // Make sure that stopPropgation doesn't stop live events
1304 submit = 0, div = 0, livea = 0, liveb = 0;
1305 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1306 jQuery("div#nothiddendivchild").trigger("click");
1307 equals( submit, 0, "stopPropagation Click on inner div" );
1308 equals( div, 1, "stopPropagation Click on inner div" );
1309 equals( livea, 0, "stopPropagation Click on inner div" );
1310 equals( liveb, 1, "stopPropagation Click on inner div" );
1312 // Make sure click events only fire with primary click
1313 submit = 0, div = 0, livea = 0, liveb = 0;
1314 var event = jQuery.Event("click");
1316 jQuery("div#nothiddendiv").trigger(event);
1318 equals( livea, 0, "delegate secondary click" );
1320 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1321 jQuery("#body").undelegate("div#nothiddendiv", "click");
1322 jQuery("#body").undelegate("div", "click");
1323 jQuery("#body").undelegate("div", "submit");
1325 // Test binding with a different context
1326 var clicked = 0, container = jQuery('#main')[0];
1327 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1328 jQuery("div").trigger('click');
1329 jQuery("#foo").trigger('click');
1330 jQuery("#main").trigger('click');
1331 jQuery("body").trigger('click');
1332 equals( clicked, 2, "delegate with a context" );
1334 // Make sure the event is actually stored on the context
1335 ok( jQuery.data(container, "events").live, "delegate with a context" );
1337 // Test unbinding with a different context
1338 jQuery("#main").undelegate("#foo", "click");
1339 jQuery("#foo").trigger('click');
1340 equals( clicked, 2, "undelegate with a context");
1342 // Test binding with event data
1343 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1344 jQuery("#foo").trigger("click");
1345 jQuery("#body").undelegate("#foo", "click");
1347 // Test binding with trigger data
1348 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1349 jQuery("#foo").trigger("click", true);
1350 jQuery("#body").undelegate("#foo", "click");
1352 // Test binding with different this object
1353 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1354 jQuery("#foo").trigger("click");
1355 jQuery("#body").undelegate("#foo", "click");
1357 // Test binding with different this object, event data, and trigger data
1358 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1359 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1360 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1361 equals( data, true, "delegate with with different this object, event data, and trigger data")
1362 }, { foo: "bar" }));
1363 jQuery("#foo").trigger("click", true);
1364 jQuery("#body").undelegate("#foo", "click");
1366 // Verify that return false prevents default action
1367 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1368 var hash = window.location.hash;
1369 jQuery("#anchor2").trigger("click");
1370 equals( window.location.hash, hash, "return false worked" );
1371 jQuery("#body").undelegate("#anchor2", "click");
1373 // Verify that .preventDefault() prevents default action
1374 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1375 var hash = window.location.hash;
1376 jQuery("#anchor2").trigger("click");
1377 equals( window.location.hash, hash, "e.preventDefault() worked" );
1378 jQuery("#body").undelegate("#anchor2", "click");
1380 // Test binding the same handler to multiple points
1382 function callback(){ called++; return false; }
1384 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1385 jQuery("#body").delegate("#anchor2", "click", callback);
1387 jQuery("#nothiddendiv").trigger("click");
1388 equals( called, 1, "Verify that only one click occurred." );
1390 jQuery("#anchor2").trigger("click");
1391 equals( called, 2, "Verify that only one click occurred." );
1393 // Make sure that only one callback is removed
1394 jQuery("#body").undelegate("#anchor2", "click", callback);
1396 jQuery("#nothiddendiv").trigger("click");
1397 equals( called, 3, "Verify that only one click occurred." );
1399 jQuery("#anchor2").trigger("click");
1400 equals( called, 3, "Verify that no click occurred." );
1402 // Make sure that it still works if the selector is the same,
1403 // but the event type is different
1404 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1407 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1409 jQuery("#nothiddendiv").trigger("click");
1410 equals( called, 3, "Verify that no click occurred." );
1412 jQuery("#nothiddendiv").trigger("foo");
1413 equals( called, 4, "Verify that one foo occurred." );
1416 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1418 // Make sure we don't loose the target by DOM modifications
1419 // after the bubble already reached the liveHandler
1420 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1422 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1423 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1425 jQuery("#nothiddendiv span").click();
1426 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1427 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1430 jQuery("#body").undelegate("#nothiddendivchild", "click");
1432 // Verify that .live() ocurs and cancel buble in the same order as
1433 // we would expect .bind() and .click() without delegation
1434 var lived = 0, livee = 0;
1436 // bind one pair in one order
1437 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1438 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1440 jQuery('span#liveSpan1 a').click();
1441 equals( lived, 1, "Verify that only one first handler occurred." );
1442 equals( livee, 0, "Verify that second handler doesn't." );
1444 // and one pair in inverse
1445 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1446 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1450 jQuery('span#liveSpan2 a').click();
1451 equals( lived, 1, "Verify that only one first handler occurred." );
1452 equals( livee, 0, "Verify that second handler doesn't." );
1455 jQuery("#body").undelegate("click");
1457 // Test this, target and currentTarget are correct
1458 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1459 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1460 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1461 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1464 jQuery('span#liveSpan1 a').click();
1466 jQuery("#body").undelegate('span#liveSpan1', 'click');
1468 // Work with deep selectors
1471 function clickB(){ livee++; }
1473 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1474 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1475 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1477 equals( livee, 0, "No clicks, deep selector." );
1480 jQuery("#nothiddendivchild").trigger("click");
1481 equals( livee, 2, "Click, deep selector." );
1484 jQuery("#nothiddendivchild").trigger("mouseover");
1485 equals( livee, 1, "Mouseover, deep selector." );
1487 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1490 jQuery("#nothiddendivchild").trigger("click");
1491 equals( livee, 2, "Click, deep selector." );
1494 jQuery("#nothiddendivchild").trigger("mouseover");
1495 equals( livee, 0, "Mouseover, deep selector." );
1497 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1500 jQuery("#nothiddendivchild").trigger("click");
1501 equals( livee, 1, "Click, deep selector." );
1503 jQuery("#body").undelegate("#nothiddendiv div", "click");
1506 test("undelegate all bound events", function(){
1510 var div = jQuery("#body");
1512 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1515 jQuery("div#nothiddendivchild").trigger("click");
1516 jQuery("div#nothiddendivchild").trigger("submit");
1518 equals( count, 0, "Make sure no events were triggered." );
1521 test("delegate with multiple events", function(){
1525 var div = jQuery("#body");
1527 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1529 jQuery("div#nothiddendivchild").trigger("click");
1530 jQuery("div#nothiddendivchild").trigger("submit");
1532 equals( count, 2, "Make sure both the click and submit were triggered." );
1534 jQuery("#body").undelegate();
1537 test("delegate with change", function(){
1538 var selectChange = 0, checkboxChange = 0;
1540 var select = jQuery("select[name='S1']");
1541 jQuery("#body").delegate("select[name='S1']", "change", function() {
1545 var checkbox = jQuery("#check2"),
1546 checkboxFunction = function(){
1549 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1551 // test click on select
1553 // second click that changed it
1555 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1556 select.trigger("change");
1557 equals( selectChange, 1, "Change on click." );
1559 // test keys on select
1561 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1562 select.trigger("change");
1563 equals( selectChange, 1, "Change on keyup." );
1565 // test click on checkbox
1566 checkbox.trigger("change");
1567 equals( checkboxChange, 1, "Change on checkbox." );
1569 // test before activate on radio
1571 // test blur/focus on textarea
1572 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1573 jQuery("#body").delegate("#area1", "change", function() {
1577 textarea.val(oldVal + "foo");
1578 textarea.trigger("change");
1579 equals( textareaChange, 1, "Change on textarea." );
1581 textarea.val(oldVal);
1582 jQuery("#body").undelegate("#area1", "change");
1584 // test blur/focus on text
1585 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1586 jQuery("#body").delegate("#name", "change", function() {
1590 text.val(oldVal+"foo");
1591 text.trigger("change");
1592 equals( textChange, 1, "Change on text input." );
1594 text.val(oldTextVal);
1595 jQuery("#body").die("change");
1597 // test blur/focus on password
1598 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1599 jQuery("#body").delegate("#name", "change", function() {
1603 password.val(oldPasswordVal + "foo");
1604 password.trigger("change");
1605 equals( passwordChange, 1, "Change on password input." );
1607 password.val(oldPasswordVal);
1608 jQuery("#body").undelegate("#name", "change");
1610 // make sure die works
1614 jQuery("#body").undelegate("select[name='S1']", "change");
1615 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1616 select.trigger("change");
1617 equals( selectChange, 0, "Die on click works." );
1620 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1621 select.trigger("change");
1622 equals( selectChange, 0, "Die on keyup works." );
1624 // die specific checkbox
1625 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1626 checkbox.trigger("change");
1627 equals( checkboxChange, 1, "Die on checkbox." );
1630 test("delegate with submit", function() {
1631 var count1 = 0, count2 = 0;
1633 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1635 ev.preventDefault();
1638 jQuery(document).delegate("body", "submit", function(ev) {
1640 ev.preventDefault();
1643 if ( jQuery.support.submitBubbles ) {
1644 jQuery("#testForm input[name=sub1]")[0].click();
1648 jQuery("#testForm input[name=sub1]")[0].click();
1649 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1654 jQuery("#body").undelegate();
1655 jQuery(document).undelegate();
1658 test("Non DOM element events", function() {
1662 .bind('nonelementglobal', function(e) {
1663 ok( true, "Global event on non-DOM annonymos object triggered" );
1669 .bind('nonelementobj', function(e) {
1670 ok( true, "Event on non-DOM object triggered" );
1671 }).bind('nonelementglobal', function() {
1672 ok( true, "Global event on non-DOM object triggered" );
1675 jQuery(o).trigger('nonelementobj');
1676 jQuery.event.trigger('nonelementglobal');
1680 test("jQuery(function($) {})", function() {
1682 jQuery(function($) {
1683 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");
1688 test("event properties", function() {
1690 jQuery("#simon1").click(function(event) {
1691 ok( event.timeStamp, "assert event.timeStamp is present" );