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(), no data", function() {
76 var handler = function(event) {
77 ok ( !event.data, "Check that no data is added to the event object" );
79 jQuery("#firstp").bind("click", handler).trigger("click");
82 test("bind/one/unbind(Object)", function(){
85 var clickCounter = 0, mouseoverCounter = 0;
86 function handler(event) {
87 if (event.type == "click")
89 else if (event.type == "mouseover")
93 function handlerWithData(event) {
94 if (event.type == "click")
95 clickCounter += event.data;
96 else if (event.type == "mouseover")
97 mouseoverCounter += event.data;
101 $elem.trigger("click").trigger("mouseover");
104 var $elem = jQuery("#firstp")
112 click:handlerWithData,
113 mouseover:handlerWithData
118 equals( clickCounter, 3, "bind(Object)" );
119 equals( mouseoverCounter, 3, "bind(Object)" );
122 equals( clickCounter, 4, "bind(Object)" );
123 equals( mouseoverCounter, 4, "bind(Object)" );
125 jQuery("#firstp").unbind({
131 equals( clickCounter, 4, "bind(Object)" );
132 equals( mouseoverCounter, 4, "bind(Object)" );
135 test("bind(), iframes", function() {
136 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
137 var doc = jQuery("#loadediframe").contents();
139 jQuery("div", doc).bind("click", function() {
140 ok( true, "Binding to element inside iframe" );
141 }).click().unbind('click');
144 test("bind(), trigger change on select", function() {
147 function selectOnChange(event) {
148 equals( event.data, counter++, "Event.data is not a global event object" );
150 jQuery("#form select").each(function(i){
151 jQuery(this).bind('change', i, selectOnChange);
152 }).trigger('change');
155 test("bind(), namespaced events, cloned events", function() {
158 jQuery("#firstp").bind("custom.test",function(e){
159 ok(true, "Custom event triggered");
162 jQuery("#firstp").bind("click",function(e){
163 ok(true, "Normal click triggered");
166 jQuery("#firstp").bind("click.test",function(e){
167 ok(true, "Namespaced click triggered");
170 // Trigger both bound fn (2)
171 jQuery("#firstp").trigger("click");
173 // Trigger one bound fn (1)
174 jQuery("#firstp").trigger("click.test");
176 // Remove only the one fn
177 jQuery("#firstp").unbind("click.test");
179 // Trigger the remaining fn (1)
180 jQuery("#firstp").trigger("click");
182 // Remove the remaining fn
183 jQuery("#firstp").unbind(".test");
185 // Trigger the remaining fn (0)
186 jQuery("#firstp").trigger("custom");
188 // using contents will get comments regular, text, and comment nodes
189 jQuery("#nonnodes").contents().bind("tester", function () {
190 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
191 }).trigger("tester");
193 // Make sure events stick with appendTo'd elements (which are cloned) #2027
194 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
195 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
198 test("bind(), multi-namespaced events", function() {
210 function check(name, msg){
211 same(name, order.shift(), msg);
214 jQuery("#firstp").bind("custom.test",function(e){
215 check("custom.test", "Custom event triggered");
218 jQuery("#firstp").bind("custom.test2",function(e){
219 check("custom.test2", "Custom event triggered");
222 jQuery("#firstp").bind("click.test",function(e){
223 check("click.test", "Normal click triggered");
226 jQuery("#firstp").bind("click.test.abc",function(e){
227 check("click.test.abc", "Namespaced click triggered");
230 // Those would not trigger/unbind (#5303)
231 jQuery("#firstp").trigger("click.a.test");
232 jQuery("#firstp").unbind("click.a.test");
234 // Trigger both bound fn (1)
235 jQuery("#firstp").trigger("click.test.abc");
237 // Trigger one bound fn (1)
238 jQuery("#firstp").trigger("click.abc");
240 // Trigger two bound fn (2)
241 jQuery("#firstp").trigger("click.test");
243 // Remove only the one fn
244 jQuery("#firstp").unbind("click.abc");
246 // Trigger the remaining fn (1)
247 jQuery("#firstp").trigger("click");
249 // Remove the remaining fn
250 jQuery("#firstp").unbind(".test");
252 // Trigger the remaining fn (1)
253 jQuery("#firstp").trigger("custom");
256 test("bind(), with different this object", function() {
258 var thisObject = { myThis: true },
259 data = { myData: true },
260 handler1 = function( event ) {
261 equals( this, thisObject, "bind() with different this object" );
263 handler2 = function( event ) {
264 equals( this, thisObject, "bind() with different this object and data" );
265 equals( event.data, data, "bind() with different this object and data" );
269 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
270 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
272 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
275 test("unbind(type)", function() {
278 var $elem = jQuery("#firstp"),
282 ok( false, message );
285 message = "unbind passing function";
286 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
288 message = "unbind all from event";
289 $elem.bind('error', error).unbind('error').triggerHandler('error');
291 message = "unbind all";
292 $elem.bind('error', error).unbind().triggerHandler('error');
294 message = "unbind many with function";
295 $elem.bind('error error2',error)
296 .unbind('error error2', error )
297 .trigger('error').triggerHandler('error2');
299 message = "unbind many"; // #3538
300 $elem.bind('error error2',error)
301 .unbind('error error2')
302 .trigger('error').triggerHandler('error2');
304 message = "unbind without a type or handler";
305 $elem.bind("error error2.test",error)
307 .trigger("error").triggerHandler("error2");
310 test("unbind(eventObject)", function() {
313 var $elem = jQuery("#firstp"),
316 function assert( expected ){
318 $elem.trigger('foo').triggerHandler('bar');
319 equals( num, expected, "Check the right handlers are triggered" );
323 // This handler shouldn't be unbound
324 .bind('foo', function(){
327 .bind('foo', function(e){
332 .bind('bar', function(){
346 test("hover()", function() {
348 handler1 = function( event ) { ++times; },
349 handler2 = function( event ) { ++times; };
352 .hover(handler1, handler2)
353 .mouseenter().mouseleave()
354 .unbind("mouseenter", handler1)
355 .unbind("mouseleave", handler2)
357 .mouseenter().mouseleave()
358 .unbind("mouseenter mouseleave", handler1)
359 .mouseenter().mouseleave();
361 equals( times, 4, "hover handlers fired" );
364 test("trigger() shortcuts", function() {
366 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
367 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
368 equals( close.length, 0, "Context element does not exist, length must be zero" );
369 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
373 jQuery("#check1").click(function() {
374 ok( true, "click event handler for checkbox gets fired twice, see #815" );
378 jQuery('#firstp')[0].onclick = function(event) {
381 jQuery('#firstp').click();
382 equals( counter, 1, "Check that click, triggers onclick event handler also" );
384 var clickCounter = 0;
385 jQuery('#simon1')[0].onclick = function(event) {
388 jQuery('#simon1').click();
389 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
391 jQuery('<img />').load(function(){
392 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
396 test("trigger() bubbling", function() {
399 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
401 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
402 jQuery("html").bind("click", function(e){ html++; });
403 jQuery("body").bind("click", function(e){ body++; });
404 jQuery("#main").bind("click", function(e){ main++; });
405 jQuery("#ap").bind("click", function(){ ap++; return false; });
407 jQuery("html").trigger("click");
408 equals( doc, 1, "HTML bubble" );
409 equals( html, 1, "HTML bubble" );
411 jQuery("body").trigger("click");
412 equals( doc, 2, "Body bubble" );
413 equals( html, 2, "Body bubble" );
414 equals( body, 1, "Body bubble" );
416 jQuery("#main").trigger("click");
417 equals( doc, 3, "Main bubble" );
418 equals( html, 3, "Main bubble" );
419 equals( body, 2, "Main bubble" );
420 equals( main, 1, "Main bubble" );
422 jQuery("#ap").trigger("click");
423 equals( doc, 3, "ap bubble" );
424 equals( html, 3, "ap bubble" );
425 equals( body, 2, "ap bubble" );
426 equals( main, 1, "ap bubble" );
427 equals( ap, 1, "ap bubble" );
430 test("trigger(type, [data], [fn])", function() {
433 var handler = function(event, a, b, c) {
434 equals( event.type, "click", "check passed data" );
435 equals( a, 1, "check passed data" );
436 equals( b, "2", "check passed data" );
437 equals( c, "abc", "check passed data" );
441 var $elem = jQuery("#firstp");
443 // Simulate a "native" click
444 $elem[0].click = function(){
445 ok( true, "Native call was triggered" );
448 // Triggers handlrs and native
450 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
452 // Simulate a "native" click
453 $elem[0].click = function(){
454 ok( false, "Native call was triggered" );
457 // Trigger only the handlers (no native)
459 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
463 jQuery('#form input:first').hide().trigger('focus');
467 ok( pass, "Trigger focus on hidden element" );
471 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
475 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
477 var form = jQuery("<form action=''></form>").appendTo("body");
479 // Make sure it can be prevented locally
480 form.submit(function(){
481 ok( true, "Local bind still works." );
486 form.trigger("submit");
488 form.unbind("submit");
490 jQuery(document).submit(function(){
491 ok( true, "Make sure bubble works up to document." );
496 form.trigger("submit");
498 jQuery(document).unbind("submit");
503 test("jQuery.Event.currentTarget", function(){
506 test("trigger(eventObject, [data], [fn])", function() {
509 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
510 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
512 var event = jQuery.Event("noNew");
513 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
514 equals( event.type, "noNew", "Verify its type" );
516 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
517 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
518 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
520 event.preventDefault();
521 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
522 event.stopPropagation();
523 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
525 event.isPropagationStopped = function(){ return false };
526 event.stopImmediatePropagation();
527 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
528 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
530 $parent.bind('foo',function(e){
532 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
533 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
534 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
535 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
538 // test with an event object
539 event = new jQuery.Event("foo");
540 event.secret = 'boo!';
541 $child.trigger(event);
543 // test with a literal object
544 $child.trigger({type:'foo', secret:'boo!'});
549 ok( false, "This assertion shouldn't be reached");
552 $parent.bind('foo', error );
554 $child.bind('foo',function(e, a, b, c ){
555 equals( arguments.length, 4, "Check arguments length");
556 equals( a, 1, "Check first custom argument");
557 equals( b, 2, "Check second custom argument");
558 equals( c, 3, "Check third custom argument");
560 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
561 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
562 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
565 e.stopImmediatePropagation();
570 // We should add this back in when we want to test the order
571 // in which event handlers are iterated.
572 //$child.bind('foo', error );
574 event = new jQuery.Event("foo");
575 $child.trigger( event, [1,2,3] ).unbind();
576 equals( event.result, "result", "Check event.result attribute");
578 // Will error if it bubbles
579 $child.triggerHandler('foo');
582 $parent.unbind().remove();
585 test("jQuery.Event.currentTarget", function(){
589 $elem = jQuery('<button>a</button>').click(function(e){
590 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
594 $elem.trigger('click');
600 test("toggle(Function, Function, ...)", function() {
604 fn1 = function(e) { count++; },
605 fn2 = function(e) { count--; },
606 preventDefault = function(e) { e.preventDefault() },
607 link = jQuery('#mark');
608 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
609 equals( count, 1, "Check for toggle(fn, fn)" );
611 jQuery("#firstp").toggle(function () {
612 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
613 }, function() {}).trigger("click", [ 1, 2, 3 ]);
616 jQuery("#simon1").one("click", function() {
617 ok( true, "Execute event only once" );
618 jQuery(this).toggle(function() {
619 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
621 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
624 }).click().click().click();
639 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
641 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
643 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
645 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
647 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
649 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
651 $div.unbind('click',fns[0]);
652 var data = jQuery.data( $div[0], 'events' );
653 ok( !data, "Unbinding one function from toggle unbinds them all");
655 // Test Multi-Toggles
657 $div = jQuery("<div/>");
658 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
660 same( a, [1], "Check that a click worked." );
662 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
664 same( a, [1,2], "Check that a click worked with a second toggle." );
665 same( b, [1], "Check that a click worked with a second toggle." );
668 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
669 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
672 test(".live()/.die()", function() {
675 var submit = 0, div = 0, livea = 0, liveb = 0;
677 jQuery("div").live("submit", function(){ submit++; return false; });
678 jQuery("div").live("click", function(){ div++; });
679 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
680 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
682 // Nothing should trigger on the body
683 jQuery("body").trigger("click");
684 equals( submit, 0, "Click on body" );
685 equals( div, 0, "Click on body" );
686 equals( livea, 0, "Click on body" );
687 equals( liveb, 0, "Click on body" );
689 // This should trigger two events
690 jQuery("div#nothiddendiv").trigger("click");
691 equals( submit, 0, "Click on div" );
692 equals( div, 1, "Click on div" );
693 equals( livea, 1, "Click on div" );
694 equals( liveb, 0, "Click on div" );
696 // This should trigger three events (w/ bubbling)
697 jQuery("div#nothiddendivchild").trigger("click");
698 equals( submit, 0, "Click on inner div" );
699 equals( div, 2, "Click on inner div" );
700 equals( livea, 2, "Click on inner div" );
701 equals( liveb, 1, "Click on inner div" );
703 // This should trigger one submit
704 jQuery("div#nothiddendivchild").trigger("submit");
705 equals( submit, 1, "Submit on div" );
706 equals( div, 2, "Submit on div" );
707 equals( livea, 2, "Submit on div" );
708 equals( liveb, 1, "Submit on div" );
710 // Make sure no other events were removed in the process
711 jQuery("div#nothiddendivchild").trigger("click");
712 equals( submit, 1, "die Click on inner div" );
713 equals( div, 3, "die Click on inner div" );
714 equals( livea, 3, "die Click on inner div" );
715 equals( liveb, 2, "die Click on inner div" );
717 // Now make sure that the removal works
718 jQuery("div#nothiddendivchild").die("click");
719 jQuery("div#nothiddendivchild").trigger("click");
720 equals( submit, 1, "die Click on inner div" );
721 equals( div, 4, "die Click on inner div" );
722 equals( livea, 4, "die Click on inner div" );
723 equals( liveb, 2, "die Click on inner div" );
725 // Make sure that the click wasn't removed too early
726 jQuery("div#nothiddendiv").trigger("click");
727 equals( submit, 1, "die Click on inner div" );
728 equals( div, 5, "die Click on inner div" );
729 equals( livea, 5, "die Click on inner div" );
730 equals( liveb, 2, "die Click on inner div" );
732 // Make sure that stopPropgation doesn't stop live events
733 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
734 jQuery("div#nothiddendivchild").trigger("click");
735 equals( submit, 1, "stopPropagation Click on inner div" );
736 equals( div, 6, "stopPropagation Click on inner div" );
737 equals( livea, 6, "stopPropagation Click on inner div" );
738 equals( liveb, 3, "stopPropagation Click on inner div" );
740 // Make sure click events only fire with primary click
741 var event = jQuery.Event("click");
743 jQuery("div#nothiddendiv").trigger(event);
745 equals( livea, 6, "live secondary click" );
747 jQuery("div#nothiddendivchild").die("click");
748 jQuery("div#nothiddendiv").die("click");
749 jQuery("div").die("click");
750 jQuery("div").die("submit");
752 // Test binding with a different context
753 var clicked = 0, container = jQuery('#main')[0];
754 jQuery("#foo", container).live("click", function(e){ clicked++; });
755 jQuery("div").trigger('click');
756 jQuery("#foo").trigger('click');
757 jQuery("#main").trigger('click');
758 jQuery("body").trigger('click');
759 equals( clicked, 2, "live with a context" );
761 // Make sure the event is actually stored on the context
762 ok( jQuery.data(container, "events").live, "live with a context" );
764 // Test unbinding with a different context
765 jQuery("#foo", container).die("click");
766 jQuery("#foo").trigger('click');
767 equals( clicked, 2, "die with a context");
769 // Test binding with event data
770 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
771 jQuery("#foo").trigger("click").die("click");
773 // Test binding with trigger data
774 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
775 jQuery("#foo").trigger("click", true).die("click");
777 // Test binding with different this object
778 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
779 jQuery("#foo").trigger("click").die("click");
781 // Test binding with different this object, event data, and trigger data
782 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
783 equals( e.data, true, "live with with different this object, event data, and trigger data" );
784 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
785 equals( data, true, "live with with different this object, event data, and trigger data")
787 jQuery("#foo").trigger("click", true).die("click");
789 // Verify that return false prevents default action
790 jQuery("#anchor2").live("click", function(){ return false; });
791 var hash = window.location.hash;
792 jQuery("#anchor2").trigger("click");
793 equals( window.location.hash, hash, "return false worked" );
794 jQuery("#anchor2").die("click");
796 // Verify that .preventDefault() prevents default action
797 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
798 var hash = window.location.hash;
799 jQuery("#anchor2").trigger("click");
800 equals( window.location.hash, hash, "e.preventDefault() worked" );
801 jQuery("#anchor2").die("click");
803 // Test binding the same handler to multiple points
805 function callback(){ called++; return false; }
807 jQuery("#nothiddendiv").live("click", callback);
808 jQuery("#anchor2").live("click", callback);
810 jQuery("#nothiddendiv").trigger("click");
811 equals( called, 1, "Verify that only one click occurred." );
813 jQuery("#anchor2").trigger("click");
814 equals( called, 2, "Verify that only one click occurred." );
816 // Make sure that only one callback is removed
817 jQuery("#anchor2").die("click", callback);
819 jQuery("#nothiddendiv").trigger("click");
820 equals( called, 3, "Verify that only one click occurred." );
822 jQuery("#anchor2").trigger("click");
823 equals( called, 3, "Verify that no click occurred." );
825 // Make sure that it still works if the selector is the same,
826 // but the event type is different
827 jQuery("#nothiddendiv").live("foo", callback);
830 jQuery("#nothiddendiv").die("click", callback);
832 jQuery("#nothiddendiv").trigger("click");
833 equals( called, 3, "Verify that no click occurred." );
835 jQuery("#nothiddendiv").trigger("foo");
836 equals( called, 4, "Verify that one foo occurred." );
839 jQuery("#nothiddendiv").die("foo", callback);
841 // Make sure we don't loose the target by DOM modifications
842 // after the bubble already reached the liveHandler
843 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
845 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
846 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
848 jQuery("#nothiddendiv span").click();
849 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
850 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
853 jQuery("#nothiddendivchild").die("click");
855 // Verify that .live() ocurs and cancel buble in the same order as
856 // we would expect .bind() and .click() without delegation
857 var lived = 0, livee = 0;
859 // bind one pair in one order
860 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
861 jQuery('span#liveSpan1').live('click', function(){ livee++; });
863 jQuery('span#liveSpan1 a').click();
864 equals( lived, 1, "Verify that only one first handler occurred." );
865 equals( livee, 0, "Verify that second handler doesn't." );
867 // and one pair in inverse
868 jQuery('span#liveSpan2').live('click', function(){ livee++; });
869 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
873 jQuery('span#liveSpan2 a').click();
874 equals( lived, 1, "Verify that only one first handler occurred." );
875 equals( livee, 0, "Verify that second handler doesn't." );
878 jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
880 // Test this, target and currentTarget are correct
881 jQuery('span#liveSpan1').live('click', function(e){
882 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
883 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
884 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
887 jQuery('span#liveSpan1 a').click();
889 jQuery('span#liveSpan1').die('click');
891 // Work with deep selectors
894 function clickB(){ livee++; }
896 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
897 jQuery("#nothiddendiv div").live("click", clickB);
898 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
900 equals( livee, 0, "No clicks, deep selector." );
903 jQuery("#nothiddendivchild").trigger("click");
904 equals( livee, 2, "Click, deep selector." );
907 jQuery("#nothiddendivchild").trigger("mouseover");
908 equals( livee, 1, "Mouseover, deep selector." );
910 jQuery("#nothiddendiv div").die("mouseover");
913 jQuery("#nothiddendivchild").trigger("click");
914 equals( livee, 2, "Click, deep selector." );
917 jQuery("#nothiddendivchild").trigger("mouseover");
918 equals( livee, 0, "Mouseover, deep selector." );
920 jQuery("#nothiddendiv div").die("click", clickB);
923 jQuery("#nothiddendivchild").trigger("click");
924 equals( livee, 1, "Click, deep selector." );
926 jQuery("#nothiddendiv div").die("click");
929 test("die all bound events", function(){
933 var div = jQuery("div#nothiddendivchild");
935 div.live("click submit", function(){ count++; });
938 div.trigger("click");
939 div.trigger("submit");
941 equals( count, 0, "Make sure no events were triggered." );
944 test("live with multiple events", function(){
948 var div = jQuery("div#nothiddendivchild");
950 div.live("click submit", function(){ count++; });
952 div.trigger("click");
953 div.trigger("submit");
955 equals( count, 2, "Make sure both the click and submit were triggered." );
958 test("live with change", function(){
959 var selectChange = 0, checkboxChange = 0;
961 var select = jQuery("select[name='S1']")
962 select.live("change", function() {
966 var checkbox = jQuery("#check2"),
967 checkboxFunction = function(){
970 checkbox.live("change", checkboxFunction);
972 // test click on select
974 // second click that changed it
976 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
977 select.trigger("change");
978 equals( selectChange, 1, "Change on click." );
980 // test keys on select
982 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
983 select.trigger("change");
984 equals( selectChange, 1, "Change on keyup." );
986 // test click on checkbox
987 checkbox.trigger("change");
988 equals( checkboxChange, 1, "Change on checkbox." );
990 // test before activate on radio
992 // test blur/focus on textarea
993 var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
994 textarea.live("change", function() {
998 textarea.val(oldVal + "foo");
999 textarea.trigger("change");
1000 equals( textareaChange, 1, "Change on textarea." );
1002 textarea.val(oldVal);
1003 textarea.die("change");
1005 // test blur/focus on text
1006 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1007 text.live("change", function() {
1011 text.val(oldVal+"foo");
1012 text.trigger("change");
1013 equals( textChange, 1, "Change on text input." );
1015 text.val(oldTextVal);
1018 // test blur/focus on password
1019 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1020 password.live("change", function() {
1024 password.val(oldPasswordVal + "foo");
1025 password.trigger("change");
1026 equals( passwordChange, 1, "Change on password input." );
1028 password.val(oldPasswordVal);
1029 password.die("change");
1031 // make sure die works
1035 select.die("change");
1036 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1037 select.trigger("change");
1038 equals( selectChange, 0, "Die on click works." );
1041 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1042 select.trigger("change");
1043 equals( selectChange, 0, "Die on keyup works." );
1045 // die specific checkbox
1046 checkbox.die("change", checkboxFunction);
1047 checkbox.trigger("change");
1048 equals( checkboxChange, 1, "Die on checkbox." );
1051 test("live with submit", function() {
1052 var count1 = 0, count2 = 0;
1054 jQuery("#testForm").live("submit", function(ev) {
1056 ev.preventDefault();
1059 jQuery("body").live("submit", function(ev) {
1061 ev.preventDefault();
1064 if ( jQuery.support.submitBubbles ) {
1065 jQuery("#testForm input[name=sub1]")[0].click();
1069 jQuery("#testForm input[name=sub1]")[0].click();
1070 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1075 jQuery("#testForm").die("submit");
1076 jQuery("body").die("submit");
1079 test("live with focus/blur", function(){
1083 jQuery("<input type='text' id='livefb' />").appendTo("body");
1085 var $child = jQuery("#livefb"),
1090 pass[e.type] = true;
1093 $child.live("focus", worked);
1094 $child.live("blur", worked);
1099 ok(true, "Test live() with focus event");
1101 ok(true, "Cannot test focus because the window isn't focused");
1105 ok( true, "Test live() with blur event");
1107 ok(true, "Cannot test blur because the window isn't focused");
1110 $child.die("focus", worked);
1111 $child.die("blur", worked);
1113 window.scrollTo(0,0);
1116 test("Non DOM element events", function() {
1120 .bind('nonelementglobal', function(e) {
1121 ok( true, "Global event on non-DOM annonymos object triggered" );
1127 .bind('nonelementobj', function(e) {
1128 ok( true, "Event on non-DOM object triggered" );
1129 }).bind('nonelementglobal', function() {
1130 ok( true, "Global event on non-DOM object triggered" );
1133 jQuery(o).trigger('nonelementobj');
1134 jQuery.event.trigger('nonelementglobal');
1138 test("jQuery(function($) {})", function() {
1140 jQuery(function($) {
1141 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");
1146 test("event properties", function() {
1148 jQuery("#simon1").click(function(event) {
1149 ok( event.timeStamp, "assert event.timeStamp is present" );