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 = {
85 teardown: function(){},
86 add: function( handler, data, namespaces ) {
89 handler.apply( this, arguments );
95 div.bind("test.a", {x: 1}, function(e) {
96 ok( !!e.xyz, "Make sure that the data is getting passed through." );
97 equals( e.data.x, 1, "Make sure data is attached properly." );
100 div.bind("test.b", {x: 2}, function(e) {
101 ok( !!e.xyz, "Make sure that the data is getting passed through." );
102 equals( e.data.x, 2, "Make sure data is attached properly." );
109 div.trigger("test.a");
112 div.trigger("test.b");
115 test("bind(), no data", function() {
117 var handler = function(event) {
118 ok ( !event.data, "Check that no data is added to the event object" );
120 jQuery("#firstp").bind("click", handler).trigger("click");
123 test("bind/one/unbind(Object)", function(){
126 var clickCounter = 0, mouseoverCounter = 0;
127 function handler(event) {
128 if (event.type == "click")
130 else if (event.type == "mouseover")
134 function handlerWithData(event) {
135 if (event.type == "click")
136 clickCounter += event.data;
137 else if (event.type == "mouseover")
138 mouseoverCounter += event.data;
142 $elem.trigger("click").trigger("mouseover");
145 var $elem = jQuery("#firstp")
153 click:handlerWithData,
154 mouseover:handlerWithData
159 equals( clickCounter, 3, "bind(Object)" );
160 equals( mouseoverCounter, 3, "bind(Object)" );
163 equals( clickCounter, 4, "bind(Object)" );
164 equals( mouseoverCounter, 4, "bind(Object)" );
166 jQuery("#firstp").unbind({
172 equals( clickCounter, 4, "bind(Object)" );
173 equals( mouseoverCounter, 4, "bind(Object)" );
176 test("bind(), iframes", function() {
177 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
178 var doc = jQuery("#loadediframe").contents();
180 jQuery("div", doc).bind("click", function() {
181 ok( true, "Binding to element inside iframe" );
182 }).click().unbind('click');
185 test("bind(), trigger change on select", function() {
188 function selectOnChange(event) {
189 equals( event.data, counter++, "Event.data is not a global event object" );
191 jQuery("#form select").each(function(i){
192 jQuery(this).bind('change', i, selectOnChange);
193 }).trigger('change');
196 test("bind(), namespaced events, cloned events", function() {
199 jQuery("#firstp").bind("custom.test",function(e){
200 ok(true, "Custom event triggered");
203 jQuery("#firstp").bind("click",function(e){
204 ok(true, "Normal click triggered");
207 jQuery("#firstp").bind("click.test",function(e){
208 ok(true, "Namespaced click triggered");
211 // Trigger both bound fn (2)
212 jQuery("#firstp").trigger("click");
214 // Trigger one bound fn (1)
215 jQuery("#firstp").trigger("click.test");
217 // Remove only the one fn
218 jQuery("#firstp").unbind("click.test");
220 // Trigger the remaining fn (1)
221 jQuery("#firstp").trigger("click");
223 // Remove the remaining fn
224 jQuery("#firstp").unbind(".test");
226 // Trigger the remaining fn (0)
227 jQuery("#firstp").trigger("custom");
229 // using contents will get comments regular, text, and comment nodes
230 jQuery("#nonnodes").contents().bind("tester", function () {
231 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
232 }).trigger("tester");
234 // Make sure events stick with appendTo'd elements (which are cloned) #2027
235 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
236 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
239 test("bind(), multi-namespaced events", function() {
251 function check(name, msg){
252 same(name, order.shift(), msg);
255 jQuery("#firstp").bind("custom.test",function(e){
256 check("custom.test", "Custom event triggered");
259 jQuery("#firstp").bind("custom.test2",function(e){
260 check("custom.test2", "Custom event triggered");
263 jQuery("#firstp").bind("click.test",function(e){
264 check("click.test", "Normal click triggered");
267 jQuery("#firstp").bind("click.test.abc",function(e){
268 check("click.test.abc", "Namespaced click triggered");
271 // Those would not trigger/unbind (#5303)
272 jQuery("#firstp").trigger("click.a.test");
273 jQuery("#firstp").unbind("click.a.test");
275 // Trigger both bound fn (1)
276 jQuery("#firstp").trigger("click.test.abc");
278 // Trigger one bound fn (1)
279 jQuery("#firstp").trigger("click.abc");
281 // Trigger two bound fn (2)
282 jQuery("#firstp").trigger("click.test");
284 // Remove only the one fn
285 jQuery("#firstp").unbind("click.abc");
287 // Trigger the remaining fn (1)
288 jQuery("#firstp").trigger("click");
290 // Remove the remaining fn
291 jQuery("#firstp").unbind(".test");
293 // Trigger the remaining fn (1)
294 jQuery("#firstp").trigger("custom");
297 test("bind(), with different this object", function() {
299 var thisObject = { myThis: true },
300 data = { myData: true },
301 handler1 = function( event ) {
302 equals( this, thisObject, "bind() with different this object" );
304 handler2 = function( event ) {
305 equals( this, thisObject, "bind() with different this object and data" );
306 equals( event.data, data, "bind() with different this object and data" );
310 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
311 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
313 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
316 test("unbind(type)", function() {
319 var $elem = jQuery("#firstp"),
323 ok( false, message );
326 message = "unbind passing function";
327 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
329 message = "unbind all from event";
330 $elem.bind('error', error).unbind('error').triggerHandler('error');
332 message = "unbind all";
333 $elem.bind('error', error).unbind().triggerHandler('error');
335 message = "unbind many with function";
336 $elem.bind('error error2',error)
337 .unbind('error error2', error )
338 .trigger('error').triggerHandler('error2');
340 message = "unbind many"; // #3538
341 $elem.bind('error error2',error)
342 .unbind('error error2')
343 .trigger('error').triggerHandler('error2');
345 message = "unbind without a type or handler";
346 $elem.bind("error error2.test",error)
348 .trigger("error").triggerHandler("error2");
351 test("unbind(eventObject)", function() {
354 var $elem = jQuery("#firstp"),
357 function assert( expected ){
359 $elem.trigger('foo').triggerHandler('bar');
360 equals( num, expected, "Check the right handlers are triggered" );
364 // This handler shouldn't be unbound
365 .bind('foo', function(){
368 .bind('foo', function(e){
373 .bind('bar', function(){
387 test("hover()", function() {
389 handler1 = function( event ) { ++times; },
390 handler2 = function( event ) { ++times; };
393 .hover(handler1, handler2)
394 .mouseenter().mouseleave()
395 .unbind("mouseenter", handler1)
396 .unbind("mouseleave", handler2)
398 .mouseenter().mouseleave()
399 .unbind("mouseenter mouseleave", handler1)
400 .mouseenter().mouseleave();
402 equals( times, 4, "hover handlers fired" );
405 test("trigger() shortcuts", function() {
407 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
408 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
409 equals( close.length, 0, "Context element does not exist, length must be zero" );
410 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
414 jQuery("#check1").click(function() {
415 ok( true, "click event handler for checkbox gets fired twice, see #815" );
419 jQuery('#firstp')[0].onclick = function(event) {
422 jQuery('#firstp').click();
423 equals( counter, 1, "Check that click, triggers onclick event handler also" );
425 var clickCounter = 0;
426 jQuery('#simon1')[0].onclick = function(event) {
429 jQuery('#simon1').click();
430 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
432 jQuery('<img />').load(function(){
433 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
437 test("trigger() bubbling", function() {
440 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
442 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
443 jQuery("html").bind("click", function(e){ html++; });
444 jQuery("body").bind("click", function(e){ body++; });
445 jQuery("#main").bind("click", function(e){ main++; });
446 jQuery("#ap").bind("click", function(){ ap++; return false; });
448 jQuery("html").trigger("click");
449 equals( doc, 1, "HTML bubble" );
450 equals( html, 1, "HTML bubble" );
452 jQuery("body").trigger("click");
453 equals( doc, 2, "Body bubble" );
454 equals( html, 2, "Body bubble" );
455 equals( body, 1, "Body bubble" );
457 jQuery("#main").trigger("click");
458 equals( doc, 3, "Main bubble" );
459 equals( html, 3, "Main bubble" );
460 equals( body, 2, "Main bubble" );
461 equals( main, 1, "Main bubble" );
463 jQuery("#ap").trigger("click");
464 equals( doc, 3, "ap bubble" );
465 equals( html, 3, "ap bubble" );
466 equals( body, 2, "ap bubble" );
467 equals( main, 1, "ap bubble" );
468 equals( ap, 1, "ap bubble" );
471 test("trigger(type, [data], [fn])", function() {
474 var handler = function(event, a, b, c) {
475 equals( event.type, "click", "check passed data" );
476 equals( a, 1, "check passed data" );
477 equals( b, "2", "check passed data" );
478 equals( c, "abc", "check passed data" );
482 var $elem = jQuery("#firstp");
484 // Simulate a "native" click
485 $elem[0].click = function(){
486 ok( true, "Native call was triggered" );
489 // Triggers handlrs and native
491 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
493 // Simulate a "native" click
494 $elem[0].click = function(){
495 ok( false, "Native call was triggered" );
498 // Trigger only the handlers (no native)
500 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
504 jQuery('#form input:first').hide().trigger('focus');
508 ok( pass, "Trigger focus on hidden element" );
512 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
516 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
518 var form = jQuery("<form action=''></form>").appendTo("body");
520 // Make sure it can be prevented locally
521 form.submit(function(){
522 ok( true, "Local bind still works." );
527 form.trigger("submit");
529 form.unbind("submit");
531 jQuery(document).submit(function(){
532 ok( true, "Make sure bubble works up to document." );
537 form.trigger("submit");
539 jQuery(document).unbind("submit");
544 test("jQuery.Event.currentTarget", function(){
547 test("trigger(eventObject, [data], [fn])", function() {
550 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
551 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
553 var event = jQuery.Event("noNew");
554 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
555 equals( event.type, "noNew", "Verify its type" );
557 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
558 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
559 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
561 event.preventDefault();
562 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
563 event.stopPropagation();
564 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
566 event.isPropagationStopped = function(){ return false };
567 event.stopImmediatePropagation();
568 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
569 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
571 $parent.bind('foo',function(e){
573 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
574 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
575 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
576 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
579 // test with an event object
580 event = new jQuery.Event("foo");
581 event.secret = 'boo!';
582 $child.trigger(event);
584 // test with a literal object
585 $child.trigger({type:'foo', secret:'boo!'});
590 ok( false, "This assertion shouldn't be reached");
593 $parent.bind('foo', error );
595 $child.bind('foo',function(e, a, b, c ){
596 equals( arguments.length, 4, "Check arguments length");
597 equals( a, 1, "Check first custom argument");
598 equals( b, 2, "Check second custom argument");
599 equals( c, 3, "Check third custom argument");
601 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
602 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
603 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
606 e.stopImmediatePropagation();
611 // We should add this back in when we want to test the order
612 // in which event handlers are iterated.
613 //$child.bind('foo', error );
615 event = new jQuery.Event("foo");
616 $child.trigger( event, [1,2,3] ).unbind();
617 equals( event.result, "result", "Check event.result attribute");
619 // Will error if it bubbles
620 $child.triggerHandler('foo');
623 $parent.unbind().remove();
626 test("jQuery.Event.currentTarget", function(){
630 $elem = jQuery('<button>a</button>').click(function(e){
631 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
635 $elem.trigger('click');
641 test("toggle(Function, Function, ...)", function() {
645 fn1 = function(e) { count++; },
646 fn2 = function(e) { count--; },
647 preventDefault = function(e) { e.preventDefault() },
648 link = jQuery('#mark');
649 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
650 equals( count, 1, "Check for toggle(fn, fn)" );
652 jQuery("#firstp").toggle(function () {
653 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
654 }, function() {}).trigger("click", [ 1, 2, 3 ]);
657 jQuery("#simon1").one("click", function() {
658 ok( true, "Execute event only once" );
659 jQuery(this).toggle(function() {
660 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
662 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
665 }).click().click().click();
680 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
682 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
684 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
686 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
688 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
690 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
692 $div.unbind('click',fns[0]);
693 var data = jQuery.data( $div[0], 'events' );
694 ok( !data, "Unbinding one function from toggle unbinds them all");
696 // Test Multi-Toggles
698 $div = jQuery("<div/>");
699 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
701 same( a, [1], "Check that a click worked." );
703 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
705 same( a, [1,2], "Check that a click worked with a second toggle." );
706 same( b, [1], "Check that a click worked with a second toggle." );
709 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
710 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
713 test(".live()/.die()", function() {
716 var submit = 0, div = 0, livea = 0, liveb = 0;
718 jQuery("div").live("submit", function(){ submit++; return false; });
719 jQuery("div").live("click", function(){ div++; });
720 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
721 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
723 // Nothing should trigger on the body
724 jQuery("body").trigger("click");
725 equals( submit, 0, "Click on body" );
726 equals( div, 0, "Click on body" );
727 equals( livea, 0, "Click on body" );
728 equals( liveb, 0, "Click on body" );
730 // This should trigger two events
731 jQuery("div#nothiddendiv").trigger("click");
732 equals( submit, 0, "Click on div" );
733 equals( div, 1, "Click on div" );
734 equals( livea, 1, "Click on div" );
735 equals( liveb, 0, "Click on div" );
737 // This should trigger three events (w/ bubbling)
738 jQuery("div#nothiddendivchild").trigger("click");
739 equals( submit, 0, "Click on inner div" );
740 equals( div, 2, "Click on inner div" );
741 equals( livea, 2, "Click on inner div" );
742 equals( liveb, 1, "Click on inner div" );
744 // This should trigger one submit
745 jQuery("div#nothiddendivchild").trigger("submit");
746 equals( submit, 1, "Submit on div" );
747 equals( div, 2, "Submit on div" );
748 equals( livea, 2, "Submit on div" );
749 equals( liveb, 1, "Submit on div" );
751 // Make sure no other events were removed in the process
752 jQuery("div#nothiddendivchild").trigger("click");
753 equals( submit, 1, "die Click on inner div" );
754 equals( div, 3, "die Click on inner div" );
755 equals( livea, 3, "die Click on inner div" );
756 equals( liveb, 2, "die Click on inner div" );
758 // Now make sure that the removal works
759 jQuery("div#nothiddendivchild").die("click");
760 jQuery("div#nothiddendivchild").trigger("click");
761 equals( submit, 1, "die Click on inner div" );
762 equals( div, 4, "die Click on inner div" );
763 equals( livea, 4, "die Click on inner div" );
764 equals( liveb, 2, "die Click on inner div" );
766 // Make sure that the click wasn't removed too early
767 jQuery("div#nothiddendiv").trigger("click");
768 equals( submit, 1, "die Click on inner div" );
769 equals( div, 5, "die Click on inner div" );
770 equals( livea, 5, "die Click on inner div" );
771 equals( liveb, 2, "die Click on inner div" );
773 // Make sure that stopPropgation doesn't stop live events
774 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
775 jQuery("div#nothiddendivchild").trigger("click");
776 equals( submit, 1, "stopPropagation Click on inner div" );
777 equals( div, 6, "stopPropagation Click on inner div" );
778 equals( livea, 6, "stopPropagation Click on inner div" );
779 equals( liveb, 3, "stopPropagation Click on inner div" );
781 // Make sure click events only fire with primary click
782 var event = jQuery.Event("click");
784 jQuery("div#nothiddendiv").trigger(event);
786 equals( livea, 6, "live secondary click" );
788 jQuery("div#nothiddendivchild").die("click");
789 jQuery("div#nothiddendiv").die("click");
790 jQuery("div").die("click");
791 jQuery("div").die("submit");
793 // Test binding with a different context
794 var clicked = 0, container = jQuery('#main')[0];
795 jQuery("#foo", container).live("click", function(e){ clicked++; });
796 jQuery("div").trigger('click');
797 jQuery("#foo").trigger('click');
798 jQuery("#main").trigger('click');
799 jQuery("body").trigger('click');
800 equals( clicked, 2, "live with a context" );
802 // Make sure the event is actually stored on the context
803 ok( jQuery.data(container, "events").live, "live with a context" );
805 // Test unbinding with a different context
806 jQuery("#foo", container).die("click");
807 jQuery("#foo").trigger('click');
808 equals( clicked, 2, "die with a context");
810 // Test binding with event data
811 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
812 jQuery("#foo").trigger("click").die("click");
814 // Test binding with trigger data
815 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
816 jQuery("#foo").trigger("click", true).die("click");
818 // Test binding with different this object
819 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
820 jQuery("#foo").trigger("click").die("click");
822 // Test binding with different this object, event data, and trigger data
823 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
824 equals( e.data, true, "live with with different this object, event data, and trigger data" );
825 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
826 equals( data, true, "live with with different this object, event data, and trigger data")
828 jQuery("#foo").trigger("click", true).die("click");
830 // Verify that return false prevents default action
831 jQuery("#anchor2").live("click", function(){ return false; });
832 var hash = window.location.hash;
833 jQuery("#anchor2").trigger("click");
834 equals( window.location.hash, hash, "return false worked" );
835 jQuery("#anchor2").die("click");
837 // Verify that .preventDefault() prevents default action
838 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
839 var hash = window.location.hash;
840 jQuery("#anchor2").trigger("click");
841 equals( window.location.hash, hash, "e.preventDefault() worked" );
842 jQuery("#anchor2").die("click");
844 // Test binding the same handler to multiple points
846 function callback(){ called++; return false; }
848 jQuery("#nothiddendiv").live("click", callback);
849 jQuery("#anchor2").live("click", callback);
851 jQuery("#nothiddendiv").trigger("click");
852 equals( called, 1, "Verify that only one click occurred." );
854 jQuery("#anchor2").trigger("click");
855 equals( called, 2, "Verify that only one click occurred." );
857 // Make sure that only one callback is removed
858 jQuery("#anchor2").die("click", callback);
860 jQuery("#nothiddendiv").trigger("click");
861 equals( called, 3, "Verify that only one click occurred." );
863 jQuery("#anchor2").trigger("click");
864 equals( called, 3, "Verify that no click occurred." );
866 // Make sure that it still works if the selector is the same,
867 // but the event type is different
868 jQuery("#nothiddendiv").live("foo", callback);
871 jQuery("#nothiddendiv").die("click", callback);
873 jQuery("#nothiddendiv").trigger("click");
874 equals( called, 3, "Verify that no click occurred." );
876 jQuery("#nothiddendiv").trigger("foo");
877 equals( called, 4, "Verify that one foo occurred." );
880 jQuery("#nothiddendiv").die("foo", callback);
882 // Make sure we don't loose the target by DOM modifications
883 // after the bubble already reached the liveHandler
884 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
886 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
887 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
889 jQuery("#nothiddendiv span").click();
890 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
891 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
894 jQuery("#nothiddendivchild").die("click");
896 // Verify that .live() ocurs and cancel buble in the same order as
897 // we would expect .bind() and .click() without delegation
898 var lived = 0, livee = 0;
900 // bind one pair in one order
901 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
902 jQuery('span#liveSpan1').live('click', function(){ livee++; });
904 jQuery('span#liveSpan1 a').click();
905 equals( lived, 1, "Verify that only one first handler occurred." );
906 equals( livee, 0, "Verify that second handler doesn't." );
908 // and one pair in inverse
909 jQuery('span#liveSpan2').live('click', function(){ livee++; });
910 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
914 jQuery('span#liveSpan2 a').click();
915 equals( lived, 1, "Verify that only one first handler occurred." );
916 equals( livee, 0, "Verify that second handler doesn't." );
919 jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
921 // Test this, target and currentTarget are correct
922 jQuery('span#liveSpan1').live('click', function(e){
923 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
924 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
925 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
928 jQuery('span#liveSpan1 a').click();
930 jQuery('span#liveSpan1').die('click');
932 // Work with deep selectors
935 function clickB(){ livee++; }
937 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
938 jQuery("#nothiddendiv div").live("click", clickB);
939 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
941 equals( livee, 0, "No clicks, deep selector." );
944 jQuery("#nothiddendivchild").trigger("click");
945 equals( livee, 2, "Click, deep selector." );
948 jQuery("#nothiddendivchild").trigger("mouseover");
949 equals( livee, 1, "Mouseover, deep selector." );
951 jQuery("#nothiddendiv div").die("mouseover");
954 jQuery("#nothiddendivchild").trigger("click");
955 equals( livee, 2, "Click, deep selector." );
958 jQuery("#nothiddendivchild").trigger("mouseover");
959 equals( livee, 0, "Mouseover, deep selector." );
961 jQuery("#nothiddendiv div").die("click", clickB);
964 jQuery("#nothiddendivchild").trigger("click");
965 equals( livee, 1, "Click, deep selector." );
967 jQuery("#nothiddendiv div").die("click");
970 test("die all bound events", function(){
974 var div = jQuery("div#nothiddendivchild");
976 div.live("click submit", function(){ count++; });
979 div.trigger("click");
980 div.trigger("submit");
982 equals( count, 0, "Make sure no events were triggered." );
985 test("live with multiple events", function(){
989 var div = jQuery("div#nothiddendivchild");
991 div.live("click submit", function(){ count++; });
993 div.trigger("click");
994 div.trigger("submit");
996 equals( count, 2, "Make sure both the click and submit were triggered." );
999 test("live with change", function(){
1000 var selectChange = 0, checkboxChange = 0;
1002 var select = jQuery("select[name='S1']")
1003 select.live("change", function() {
1007 var checkbox = jQuery("#check2"),
1008 checkboxFunction = function(){
1011 checkbox.live("change", checkboxFunction);
1013 // test click on select
1015 // second click that changed it
1017 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1018 select.trigger("change");
1019 equals( selectChange, 1, "Change on click." );
1021 // test keys on select
1023 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1024 select.trigger("change");
1025 equals( selectChange, 1, "Change on keyup." );
1027 // test click on checkbox
1028 checkbox.trigger("change");
1029 equals( checkboxChange, 1, "Change on checkbox." );
1031 // test before activate on radio
1033 // test blur/focus on textarea
1034 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1035 textarea.live("change", function() {
1039 textarea.val(oldVal + "foo");
1040 textarea.trigger("change");
1041 equals( textareaChange, 1, "Change on textarea." );
1043 textarea.val(oldVal);
1044 textarea.die("change");
1046 // test blur/focus on text
1047 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1048 text.live("change", function() {
1052 text.val(oldVal+"foo");
1053 text.trigger("change");
1054 equals( textChange, 1, "Change on text input." );
1056 text.val(oldTextVal);
1059 // test blur/focus on password
1060 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1061 password.live("change", function() {
1065 password.val(oldPasswordVal + "foo");
1066 password.trigger("change");
1067 equals( passwordChange, 1, "Change on password input." );
1069 password.val(oldPasswordVal);
1070 password.die("change");
1072 // make sure die works
1076 select.die("change");
1077 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1078 select.trigger("change");
1079 equals( selectChange, 0, "Die on click works." );
1082 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1083 select.trigger("change");
1084 equals( selectChange, 0, "Die on keyup works." );
1086 // die specific checkbox
1087 checkbox.die("change", checkboxFunction);
1088 checkbox.trigger("change");
1089 equals( checkboxChange, 1, "Die on checkbox." );
1092 test("live with submit", function() {
1093 var count1 = 0, count2 = 0;
1095 jQuery("#testForm").live("submit", function(ev) {
1097 ev.preventDefault();
1100 jQuery("body").live("submit", function(ev) {
1102 ev.preventDefault();
1105 if ( jQuery.support.submitBubbles ) {
1106 jQuery("#testForm input[name=sub1]")[0].click();
1110 jQuery("#testForm input[name=sub1]")[0].click();
1111 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1116 jQuery("#testForm").die("submit");
1117 jQuery("body").die("submit");
1120 test("live with focus/blur", function(){
1124 jQuery("<input type='text' id='livefb' />").appendTo("body");
1126 var $child = jQuery("#livefb"),
1131 pass[e.type] = true;
1134 $child.live("focus", worked);
1135 $child.live("blur", worked);
1140 ok(true, "Test live() with focus event");
1142 ok(true, "Cannot test focus because the window isn't focused");
1146 ok( true, "Test live() with blur event");
1148 ok(true, "Cannot test blur because the window isn't focused");
1151 $child.die("focus", worked);
1152 $child.die("blur", worked);
1154 window.scrollTo(0,0);
1157 test("Non DOM element events", function() {
1161 .bind('nonelementglobal', function(e) {
1162 ok( true, "Global event on non-DOM annonymos object triggered" );
1168 .bind('nonelementobj', function(e) {
1169 ok( true, "Event on non-DOM object triggered" );
1170 }).bind('nonelementglobal', function() {
1171 ok( true, "Global event on non-DOM object triggered" );
1174 jQuery(o).trigger('nonelementobj');
1175 jQuery.event.trigger('nonelementglobal');
1179 test("jQuery(function($) {})", function() {
1181 jQuery(function($) {
1182 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");
1187 test("event properties", function() {
1189 jQuery("#simon1").click(function(event) {
1190 ok( event.timeStamp, "assert event.timeStamp is present" );