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(), no data", function() {
42 var handler = function(event) {
43 ok ( !event.data, "Check that no data is added to the event object" );
45 jQuery("#firstp").bind("click", handler).trigger("click");
48 test("bind/one/unbind(Object)", function(){
51 var clickCounter = 0, mouseoverCounter = 0;
52 function handler(event) {
53 if (event.type == "click")
55 else if (event.type == "mouseover")
59 function handlerWithData(event) {
60 if (event.type == "click")
61 clickCounter += event.data;
62 else if (event.type == "mouseover")
63 mouseoverCounter += event.data;
67 $elem.trigger("click").trigger("mouseover");
70 var $elem = jQuery("#firstp")
78 click:handlerWithData,
79 mouseover:handlerWithData
84 equals( clickCounter, 3, "bind(Object)" );
85 equals( mouseoverCounter, 3, "bind(Object)" );
88 equals( clickCounter, 4, "bind(Object)" );
89 equals( mouseoverCounter, 4, "bind(Object)" );
91 jQuery("#firstp").unbind({
97 equals( clickCounter, 4, "bind(Object)" );
98 equals( mouseoverCounter, 4, "bind(Object)" );
101 test("bind(), iframes", function() {
102 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
103 var doc = jQuery("#loadediframe").contents();
105 jQuery("div", doc).bind("click", function() {
106 ok( true, "Binding to element inside iframe" );
107 }).click().unbind('click');
110 test("bind(), trigger change on select", function() {
113 function selectOnChange(event) {
114 equals( event.data, counter++, "Event.data is not a global event object" );
116 jQuery("#form select").each(function(i){
117 jQuery(this).bind('change', i, selectOnChange);
118 }).trigger('change');
121 test("bind(), namespaced events, cloned events", function() {
124 jQuery("#firstp").bind("custom.test",function(e){
125 ok(true, "Custom event triggered");
128 jQuery("#firstp").bind("click",function(e){
129 ok(true, "Normal click triggered");
132 jQuery("#firstp").bind("click.test",function(e){
133 ok(true, "Namespaced click triggered");
136 // Trigger both bound fn (2)
137 jQuery("#firstp").trigger("click");
139 // Trigger one bound fn (1)
140 jQuery("#firstp").trigger("click.test");
142 // Remove only the one fn
143 jQuery("#firstp").unbind("click.test");
145 // Trigger the remaining fn (1)
146 jQuery("#firstp").trigger("click");
148 // Remove the remaining fn
149 jQuery("#firstp").unbind(".test");
151 // Trigger the remaining fn (0)
152 jQuery("#firstp").trigger("custom");
154 // using contents will get comments regular, text, and comment nodes
155 jQuery("#nonnodes").contents().bind("tester", function () {
156 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
157 }).trigger("tester");
159 // Make sure events stick with appendTo'd elements (which are cloned) #2027
160 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
161 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
164 test("bind(), multi-namespaced events", function() {
176 function check(name, msg){
177 same(name, order.shift(), msg);
180 jQuery("#firstp").bind("custom.test",function(e){
181 check("custom.test", "Custom event triggered");
184 jQuery("#firstp").bind("custom.test2",function(e){
185 check("custom.test2", "Custom event triggered");
188 jQuery("#firstp").bind("click.test",function(e){
189 check("click.test", "Normal click triggered");
192 jQuery("#firstp").bind("click.test.abc",function(e){
193 check("click.test.abc", "Namespaced click triggered");
196 // Those would not trigger/unbind (#5303)
197 jQuery("#firstp").trigger("click.a.test");
198 jQuery("#firstp").unbind("click.a.test");
200 // Trigger both bound fn (1)
201 jQuery("#firstp").trigger("click.test.abc");
203 // Trigger one bound fn (1)
204 jQuery("#firstp").trigger("click.abc");
206 // Trigger two bound fn (2)
207 jQuery("#firstp").trigger("click.test");
209 // Remove only the one fn
210 jQuery("#firstp").unbind("click.abc");
212 // Trigger the remaining fn (1)
213 jQuery("#firstp").trigger("click");
215 // Remove the remaining fn
216 jQuery("#firstp").unbind(".test");
218 // Trigger the remaining fn (1)
219 jQuery("#firstp").trigger("custom");
222 test("bind(), with different this object", function() {
224 var thisObject = { myThis: true },
225 data = { myData: true },
226 handler1 = function( event ) {
227 equals( this, thisObject, "bind() with different this object" );
229 handler2 = function( event ) {
230 equals( this, thisObject, "bind() with different this object and data" );
231 equals( event.data, data, "bind() with different this object and data" );
235 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
236 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
238 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
241 test("unbind(type)", function() {
244 var $elem = jQuery("#firstp"),
248 ok( false, message );
251 message = "unbind passing function";
252 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
254 message = "unbind all from event";
255 $elem.bind('error', error).unbind('error').triggerHandler('error');
257 message = "unbind all";
258 $elem.bind('error', error).unbind().triggerHandler('error');
260 message = "unbind many with function";
261 $elem.bind('error error2',error)
262 .unbind('error error2', error )
263 .trigger('error').triggerHandler('error2');
265 message = "unbind many"; // #3538
266 $elem.bind('error error2',error)
267 .unbind('error error2')
268 .trigger('error').triggerHandler('error2');
270 message = "unbind without a type or handler";
271 $elem.bind("error error2.test",error)
273 .trigger("error").triggerHandler("error2");
276 test("unbind(eventObject)", function() {
279 var $elem = jQuery("#firstp"),
282 function assert( expected ){
284 $elem.trigger('foo').triggerHandler('bar');
285 equals( num, expected, "Check the right handlers are triggered" );
289 // This handler shouldn't be unbound
290 .bind('foo', function(){
293 .bind('foo', function(e){
298 .bind('bar', function(){
312 test("hover()", function() {
314 handler1 = function( event ) { ++times; },
315 handler2 = function( event ) { ++times; };
318 .hover(handler1, handler2)
319 .mouseenter().mouseleave()
320 .unbind("mouseenter", handler1)
321 .unbind("mouseleave", handler2)
323 .mouseenter().mouseleave()
324 .unbind("mouseenter mouseleave", handler1)
325 .mouseenter().mouseleave();
327 equals( times, 4, "hover handlers fired" );
330 test("trigger() shortcuts", function() {
332 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
333 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
334 equals( close.length, 0, "Context element does not exist, length must be zero" );
335 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
339 jQuery("#check1").click(function() {
340 ok( true, "click event handler for checkbox gets fired twice, see #815" );
344 jQuery('#firstp')[0].onclick = function(event) {
347 jQuery('#firstp').click();
348 equals( counter, 1, "Check that click, triggers onclick event handler also" );
350 var clickCounter = 0;
351 jQuery('#simon1')[0].onclick = function(event) {
354 jQuery('#simon1').click();
355 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
357 jQuery('<img />').load(function(){
358 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
362 test("trigger() bubbling", function() {
365 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
367 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
368 jQuery("html").bind("click", function(e){ html++; });
369 jQuery("body").bind("click", function(e){ body++; });
370 jQuery("#main").bind("click", function(e){ main++; });
371 jQuery("#ap").bind("click", function(){ ap++; return false; });
373 jQuery("html").trigger("click");
374 equals( doc, 1, "HTML bubble" );
375 equals( html, 1, "HTML bubble" );
377 jQuery("body").trigger("click");
378 equals( doc, 2, "Body bubble" );
379 equals( html, 2, "Body bubble" );
380 equals( body, 1, "Body bubble" );
382 jQuery("#main").trigger("click");
383 equals( doc, 3, "Main bubble" );
384 equals( html, 3, "Main bubble" );
385 equals( body, 2, "Main bubble" );
386 equals( main, 1, "Main bubble" );
388 jQuery("#ap").trigger("click");
389 equals( doc, 3, "ap bubble" );
390 equals( html, 3, "ap bubble" );
391 equals( body, 2, "ap bubble" );
392 equals( main, 1, "ap bubble" );
393 equals( ap, 1, "ap bubble" );
396 test("trigger(type, [data], [fn])", function() {
399 var handler = function(event, a, b, c) {
400 equals( event.type, "click", "check passed data" );
401 equals( a, 1, "check passed data" );
402 equals( b, "2", "check passed data" );
403 equals( c, "abc", "check passed data" );
407 var $elem = jQuery("#firstp");
409 // Simulate a "native" click
410 $elem[0].click = function(){
411 ok( true, "Native call was triggered" );
414 // Triggers handlrs and native
416 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
418 // Simulate a "native" click
419 $elem[0].click = function(){
420 ok( false, "Native call was triggered" );
423 // Trigger only the handlers (no native)
425 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
429 jQuery('#form input:first').hide().trigger('focus');
433 ok( pass, "Trigger focus on hidden element" );
437 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
441 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
443 var form = jQuery("<form action=''></form>").appendTo("body");
445 // Make sure it can be prevented locally
446 form.submit(function(){
447 ok( true, "Local bind still works." );
452 form.trigger("submit");
454 form.unbind("submit");
456 jQuery(document).submit(function(){
457 ok( true, "Make sure bubble works up to document." );
462 form.trigger("submit");
464 jQuery(document).unbind("submit");
469 test("jQuery.Event.currentTarget", function(){
472 test("trigger(eventObject, [data], [fn])", function() {
475 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
476 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
478 var event = jQuery.Event("noNew");
479 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
480 equals( event.type, "noNew", "Verify its type" );
482 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
483 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
484 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
486 event.preventDefault();
487 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
488 event.stopPropagation();
489 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
491 event.isPropagationStopped = function(){ return false };
492 event.stopImmediatePropagation();
493 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
494 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
496 $parent.bind('foo',function(e){
498 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
499 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
500 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
501 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
504 // test with an event object
505 event = new jQuery.Event("foo");
506 event.secret = 'boo!';
507 $child.trigger(event);
509 // test with a literal object
510 $child.trigger({type:'foo', secret:'boo!'});
515 ok( false, "This assertion shouldn't be reached");
518 $parent.bind('foo', error );
520 $child.bind('foo',function(e, a, b, c ){
521 equals( arguments.length, 4, "Check arguments length");
522 equals( a, 1, "Check first custom argument");
523 equals( b, 2, "Check second custom argument");
524 equals( c, 3, "Check third custom argument");
526 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
527 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
528 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
531 e.stopImmediatePropagation();
536 // We should add this back in when we want to test the order
537 // in which event handlers are iterated.
538 //$child.bind('foo', error );
540 event = new jQuery.Event("foo");
541 $child.trigger( event, [1,2,3] ).unbind();
542 equals( event.result, "result", "Check event.result attribute");
544 // Will error if it bubbles
545 $child.triggerHandler('foo');
548 $parent.unbind().remove();
551 test("jQuery.Event.currentTarget", function(){
555 $elem = jQuery('<button>a</button>').click(function(e){
556 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
560 $elem.trigger('click');
566 test("toggle(Function, Function, ...)", function() {
570 fn1 = function(e) { count++; },
571 fn2 = function(e) { count--; },
572 preventDefault = function(e) { e.preventDefault() },
573 link = jQuery('#mark');
574 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
575 equals( count, 1, "Check for toggle(fn, fn)" );
577 jQuery("#firstp").toggle(function () {
578 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
579 }, function() {}).trigger("click", [ 1, 2, 3 ]);
582 jQuery("#simon1").one("click", function() {
583 ok( true, "Execute event only once" );
584 jQuery(this).toggle(function() {
585 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
587 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
590 }).click().click().click();
605 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
607 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
609 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
611 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
613 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
615 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
617 $div.unbind('click',fns[0]);
618 var data = jQuery.data( $div[0], 'events' );
619 ok( !data, "Unbinding one function from toggle unbinds them all");
621 // Test Multi-Toggles
623 $div = jQuery("<div/>");
624 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
626 same( a, [1], "Check that a click worked." );
628 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
630 same( a, [1,2], "Check that a click worked with a second toggle." );
631 same( b, [1], "Check that a click worked with a second toggle." );
634 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
635 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
638 test(".live()/.die()", function() {
641 var submit = 0, div = 0, livea = 0, liveb = 0;
643 jQuery("div").live("submit", function(){ submit++; return false; });
644 jQuery("div").live("click", function(){ div++; });
645 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
646 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
648 // Nothing should trigger on the body
649 jQuery("body").trigger("click");
650 equals( submit, 0, "Click on body" );
651 equals( div, 0, "Click on body" );
652 equals( livea, 0, "Click on body" );
653 equals( liveb, 0, "Click on body" );
655 // This should trigger two events
656 jQuery("div#nothiddendiv").trigger("click");
657 equals( submit, 0, "Click on div" );
658 equals( div, 1, "Click on div" );
659 equals( livea, 1, "Click on div" );
660 equals( liveb, 0, "Click on div" );
662 // This should trigger three events (w/ bubbling)
663 jQuery("div#nothiddendivchild").trigger("click");
664 equals( submit, 0, "Click on inner div" );
665 equals( div, 2, "Click on inner div" );
666 equals( livea, 2, "Click on inner div" );
667 equals( liveb, 1, "Click on inner div" );
669 // This should trigger one submit
670 jQuery("div#nothiddendivchild").trigger("submit");
671 equals( submit, 1, "Submit on div" );
672 equals( div, 2, "Submit on div" );
673 equals( livea, 2, "Submit on div" );
674 equals( liveb, 1, "Submit on div" );
676 // Make sure no other events were removed in the process
677 jQuery("div#nothiddendivchild").trigger("click");
678 equals( submit, 1, "die Click on inner div" );
679 equals( div, 3, "die Click on inner div" );
680 equals( livea, 3, "die Click on inner div" );
681 equals( liveb, 2, "die Click on inner div" );
683 // Now make sure that the removal works
684 jQuery("div#nothiddendivchild").die("click");
685 jQuery("div#nothiddendivchild").trigger("click");
686 equals( submit, 1, "die Click on inner div" );
687 equals( div, 4, "die Click on inner div" );
688 equals( livea, 4, "die Click on inner div" );
689 equals( liveb, 2, "die Click on inner div" );
691 // Make sure that the click wasn't removed too early
692 jQuery("div#nothiddendiv").trigger("click");
693 equals( submit, 1, "die Click on inner div" );
694 equals( div, 5, "die Click on inner div" );
695 equals( livea, 5, "die Click on inner div" );
696 equals( liveb, 2, "die Click on inner div" );
698 // Make sure that stopPropgation doesn't stop live events
699 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
700 jQuery("div#nothiddendivchild").trigger("click");
701 equals( submit, 1, "stopPropagation Click on inner div" );
702 equals( div, 6, "stopPropagation Click on inner div" );
703 equals( livea, 6, "stopPropagation Click on inner div" );
704 equals( liveb, 3, "stopPropagation Click on inner div" );
706 // Make sure click events only fire with primary click
707 var event = jQuery.Event("click");
709 jQuery("div#nothiddendiv").trigger(event);
711 equals( livea, 6, "live secondary click" );
713 jQuery("div#nothiddendivchild").die("click");
714 jQuery("div#nothiddendiv").die("click");
715 jQuery("div").die("click");
716 jQuery("div").die("submit");
718 // Test binding with a different context
719 var clicked = 0, container = jQuery('#main')[0];
720 jQuery("#foo", container).live("click", function(e){ clicked++; });
721 jQuery("div").trigger('click');
722 jQuery("#foo").trigger('click');
723 jQuery("#main").trigger('click');
724 jQuery("body").trigger('click');
725 equals( clicked, 2, "live with a context" );
727 // Make sure the event is actually stored on the context
728 ok( jQuery.data(container, "events").live, "live with a context" );
730 // Test unbinding with a different context
731 jQuery("#foo", container).die("click");
732 jQuery("#foo").trigger('click');
733 equals( clicked, 2, "die with a context");
735 // Test binding with event data
736 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
737 jQuery("#foo").trigger("click").die("click");
739 // Test binding with trigger data
740 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
741 jQuery("#foo").trigger("click", true).die("click");
743 // Test binding with different this object
744 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
745 jQuery("#foo").trigger("click").die("click");
747 // Test binding with different this object, event data, and trigger data
748 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
749 equals( e.data, true, "live with with different this object, event data, and trigger data" );
750 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
751 equals( data, true, "live with with different this object, event data, and trigger data")
753 jQuery("#foo").trigger("click", true).die("click");
755 // Verify that return false prevents default action
756 jQuery("#anchor2").live("click", function(){ return false; });
757 var hash = window.location.hash;
758 jQuery("#anchor2").trigger("click");
759 equals( window.location.hash, hash, "return false worked" );
760 jQuery("#anchor2").die("click");
762 // Verify that .preventDefault() prevents default action
763 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
764 var hash = window.location.hash;
765 jQuery("#anchor2").trigger("click");
766 equals( window.location.hash, hash, "e.preventDefault() worked" );
767 jQuery("#anchor2").die("click");
769 // Test binding the same handler to multiple points
771 function callback(){ called++; return false; }
773 jQuery("#nothiddendiv").live("click", callback);
774 jQuery("#anchor2").live("click", callback);
776 jQuery("#nothiddendiv").trigger("click");
777 equals( called, 1, "Verify that only one click occurred." );
779 jQuery("#anchor2").trigger("click");
780 equals( called, 2, "Verify that only one click occurred." );
782 // Make sure that only one callback is removed
783 jQuery("#anchor2").die("click", callback);
785 jQuery("#nothiddendiv").trigger("click");
786 equals( called, 3, "Verify that only one click occurred." );
788 jQuery("#anchor2").trigger("click");
789 equals( called, 3, "Verify that no click occurred." );
791 // Make sure that it still works if the selector is the same,
792 // but the event type is different
793 jQuery("#nothiddendiv").live("foo", callback);
796 jQuery("#nothiddendiv").die("click", callback);
798 jQuery("#nothiddendiv").trigger("click");
799 equals( called, 3, "Verify that no click occurred." );
801 jQuery("#nothiddendiv").trigger("foo");
802 equals( called, 4, "Verify that one foo occurred." );
805 jQuery("#nothiddendiv").die("foo", callback);
807 // Make sure we don't loose the target by DOM modifications
808 // after the bubble already reached the liveHandler
809 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
811 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
812 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
814 jQuery("#nothiddendiv span").click();
815 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
816 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
819 jQuery("#nothiddendivchild").die("click");
821 // Verify that .live() ocurs and cancel buble in the same order as
822 // we would expect .bind() and .click() without delegation
823 var lived = 0, livee = 0;
825 // bind one pair in one order
826 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
827 jQuery('span#liveSpan1').live('click', function(){ livee++; });
829 jQuery('span#liveSpan1 a').click();
830 equals( lived, 1, "Verify that only one first handler occurred." );
831 equals( livee, 0, "Verify that second handler doesn't." );
833 // and one pair in inverse
834 jQuery('span#liveSpan2').live('click', function(){ livee++; });
835 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
839 jQuery('span#liveSpan2 a').click();
840 equals( lived, 1, "Verify that only one first handler occurred." );
841 equals( livee, 0, "Verify that second handler doesn't." );
844 jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
846 // Test this, target and currentTarget are correct
847 jQuery('span#liveSpan1').live('click', function(e){
848 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
849 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
850 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
853 jQuery('span#liveSpan1 a').click();
855 jQuery('span#liveSpan1').die('click');
857 // Work with deep selectors
860 function clickB(){ livee++; }
862 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
863 jQuery("#nothiddendiv div").live("click", clickB);
864 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
866 equals( livee, 0, "No clicks, deep selector." );
869 jQuery("#nothiddendivchild").trigger("click");
870 equals( livee, 2, "Click, deep selector." );
873 jQuery("#nothiddendivchild").trigger("mouseover");
874 equals( livee, 1, "Mouseover, deep selector." );
876 jQuery("#nothiddendiv div").die("mouseover");
879 jQuery("#nothiddendivchild").trigger("click");
880 equals( livee, 2, "Click, deep selector." );
883 jQuery("#nothiddendivchild").trigger("mouseover");
884 equals( livee, 0, "Mouseover, deep selector." );
886 jQuery("#nothiddendiv div").die("click", clickB);
889 jQuery("#nothiddendivchild").trigger("click");
890 equals( livee, 1, "Click, deep selector." );
892 jQuery("#nothiddendiv div").die("click");
895 test("die all bound events", function(){
899 var div = jQuery("div#nothiddendivchild");
901 div.live("click submit", function(){ count++; });
904 div.trigger("click");
905 div.trigger("submit");
907 equals( count, 0, "Make sure no events were triggered." );
910 test("live with multiple events", function(){
914 var div = jQuery("div#nothiddendivchild");
916 div.live("click submit", function(){ count++; });
918 div.trigger("click");
919 div.trigger("submit");
921 equals( count, 2, "Make sure both the click and submit were triggered." );
924 test("live with change", function(){
925 var selectChange = 0, checkboxChange = 0;
927 var select = jQuery("select[name='S1']")
928 select.live("change", function() {
932 var checkbox = jQuery("#check2"),
933 checkboxFunction = function(){
936 checkbox.live("change", checkboxFunction);
938 // test click on select
940 // second click that changed it
942 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
943 select.trigger("change");
944 equals( selectChange, 1, "Change on click." );
946 // test keys on select
948 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
949 select.trigger("change");
950 equals( selectChange, 1, "Change on keyup." );
952 // test click on checkbox
953 checkbox.trigger("change");
954 equals( checkboxChange, 1, "Change on checkbox." );
956 // test before activate on radio
958 // test blur/focus on textarea
959 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
960 textarea.live("change", function() {
964 textarea.val(oldVal + "foo");
965 textarea.trigger("change");
966 equals( textareaChange, 1, "Change on textarea." );
968 textarea.val(oldVal);
969 textarea.die("change");
971 // test blur/focus on text
972 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
973 text.live("change", function() {
977 text.val(oldVal+"foo");
978 text.trigger("change");
979 equals( textChange, 1, "Change on text input." );
981 text.val(oldTextVal);
984 // test blur/focus on password
985 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
986 password.live("change", function() {
990 password.val(oldPasswordVal + "foo");
991 password.trigger("change");
992 equals( passwordChange, 1, "Change on password input." );
994 password.val(oldPasswordVal);
995 password.die("change");
997 // make sure die works
1001 select.die("change");
1002 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1003 select.trigger("change");
1004 equals( selectChange, 0, "Die on click works." );
1007 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1008 select.trigger("change");
1009 equals( selectChange, 0, "Die on keyup works." );
1011 // die specific checkbox
1012 checkbox.die("change", checkboxFunction);
1013 checkbox.trigger("change");
1014 equals( checkboxChange, 1, "Die on checkbox." );
1017 test("live with submit", function() {
1018 var count1 = 0, count2 = 0;
1020 jQuery("#testForm").live("submit", function(ev) {
1022 ev.preventDefault();
1025 jQuery("body").live("submit", function(ev) {
1027 ev.preventDefault();
1030 if ( jQuery.support.submitBubbles ) {
1031 jQuery("#testForm input[name=sub1]")[0].click();
1035 jQuery("#testForm input[name=sub1]")[0].click();
1036 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1041 jQuery("#testForm").die("submit");
1042 jQuery("body").die("submit");
1045 test("live with focus/blur", function(){
1049 jQuery("<input type='text' id='livefb' />").appendTo("body");
1051 var $child = jQuery("#livefb"),
1056 pass[e.type] = true;
1059 $child.live("focus", worked);
1060 $child.live("blur", worked);
1065 ok(true, "Test live() with focus event");
1067 ok(true, "Cannot test focus because the window isn't focused");
1071 ok( true, "Test live() with blur event");
1073 ok(true, "Cannot test blur because the window isn't focused");
1076 $child.die("focus", worked);
1077 $child.die("blur", worked);
1079 window.scrollTo(0,0);
1082 test("Non DOM element events", function() {
1086 .bind('nonelementglobal', function(e) {
1087 ok( true, "Global event on non-DOM annonymos object triggered" );
1093 .bind('nonelementobj', function(e) {
1094 ok( true, "Event on non-DOM object triggered" );
1095 }).bind('nonelementglobal', function() {
1096 ok( true, "Global event on non-DOM object triggered" );
1099 jQuery(o).trigger('nonelementobj');
1100 jQuery.event.trigger('nonelementglobal');
1104 test("jQuery(function($) {})", function() {
1106 jQuery(function($) {
1107 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");
1112 test("event properties", function() {
1114 jQuery("#simon1").click(function(event) {
1115 ok( event.timeStamp, "assert event.timeStamp is present" );