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(), iframes", function() {
49 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
50 var doc = jQuery("#loadediframe").contents();
52 jQuery("div", doc).bind("click", function() {
53 ok( true, "Binding to element inside iframe" );
54 }).click().unbind('click');
57 test("bind(), trigger change on select", function() {
60 function selectOnChange(event) {
61 equals( event.data, counter++, "Event.data is not a global event object" );
63 jQuery("#form select").each(function(i){
64 jQuery(this).bind('change', i, selectOnChange);
68 test("bind(), namespaced events, cloned events", function() {
71 jQuery("#firstp").bind("custom.test",function(e){
72 ok(true, "Custom event triggered");
75 jQuery("#firstp").bind("click",function(e){
76 ok(true, "Normal click triggered");
79 jQuery("#firstp").bind("click.test",function(e){
80 ok(true, "Namespaced click triggered");
83 // Trigger both bound fn (2)
84 jQuery("#firstp").trigger("click");
86 // Trigger one bound fn (1)
87 jQuery("#firstp").trigger("click.test");
89 // Remove only the one fn
90 jQuery("#firstp").unbind("click.test");
92 // Trigger the remaining fn (1)
93 jQuery("#firstp").trigger("click");
95 // Remove the remaining fn
96 jQuery("#firstp").unbind(".test");
98 // Trigger the remaining fn (0)
99 jQuery("#firstp").trigger("custom");
101 // using contents will get comments regular, text, and comment nodes
102 jQuery("#nonnodes").contents().bind("tester", function () {
103 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
104 }).trigger("tester");
106 // Make sure events stick with appendTo'd elements (which are cloned) #2027
107 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
108 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
111 test("bind(), multi-namespaced events", function() {
123 function check(name, msg){
124 same(name, order.shift(), msg);
127 jQuery("#firstp").bind("custom.test",function(e){
128 check("custom.test", "Custom event triggered");
131 jQuery("#firstp").bind("custom.test2",function(e){
132 check("custom.test2", "Custom event triggered");
135 jQuery("#firstp").bind("click.test",function(e){
136 check("click.test", "Normal click triggered");
139 jQuery("#firstp").bind("click.test.abc",function(e){
140 check("click.test.abc", "Namespaced click triggered");
143 // Trigger both bound fn (1)
144 jQuery("#firstp").trigger("click.test.abc");
146 // Trigger one bound fn (1)
147 jQuery("#firstp").trigger("click.abc");
149 // Trigger two bound fn (2)
150 jQuery("#firstp").trigger("click.test");
152 // Remove only the one fn
153 jQuery("#firstp").unbind("click.abc");
155 // Trigger the remaining fn (1)
156 jQuery("#firstp").trigger("click");
158 // Remove the remaining fn
159 jQuery("#firstp").unbind(".test");
161 // Trigger the remaining fn (1)
162 jQuery("#firstp").trigger("custom");
165 test("bind(), with different this object", function() {
167 var thisObject = { myThis: true },
168 data = { myData: true },
169 handler1 = function( event ) {
170 equals( this, thisObject, "bind() with different this object" );
172 handler2 = function( event ) {
173 equals( this, thisObject, "bind() with different this object and data" );
174 equals( event.data, data, "bind() with different this object and data" );
178 .bind("click", handler1, thisObject).click().unbind("click", handler1)
179 .bind("click", data, handler2, thisObject).click().unbind("click", handler2);
181 ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
184 test("unbind(type)", function() {
187 var $elem = jQuery("#firstp"),
191 ok( false, message );
194 message = "unbind passing function";
195 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
197 message = "unbind all from event";
198 $elem.bind('error', error).unbind('error').triggerHandler('error');
200 message = "unbind all";
201 $elem.bind('error', error).unbind().triggerHandler('error');
203 message = "unbind many with function";
204 $elem.bind('error error2',error)
205 .unbind('error error2', error )
206 .trigger('error').triggerHandler('error2');
208 message = "unbind many"; // #3538
209 $elem.bind('error error2',error)
210 .unbind('error error2')
211 .trigger('error').triggerHandler('error2');
213 message = "unbind without a type or handler";
214 $elem.bind("error error2.test",error)
216 .trigger("error").triggerHandler("error2");
219 test("unbind(eventObject)", function() {
222 var $elem = jQuery("#firstp"),
225 function assert( expected ){
227 $elem.trigger('foo').triggerHandler('bar');
228 equals( num, expected, "Check the right handlers are triggered" );
232 // This handler shouldn't be unbound
233 .bind('foo', function(){
236 .bind('foo', function(e){
241 .bind('bar', function(){
255 test("hover()", function() {
257 handler1 = function( event ) { ++times; },
258 handler2 = function( event ) { ++times; };
261 .hover(handler1, handler2)
262 .mouseenter().mouseleave()
263 .unbind("mouseenter", handler1)
264 .unbind("mouseleave", handler2)
266 .mouseenter().mouseleave()
267 .unbind("mouseenter mouseleave", handler1)
268 .mouseenter().mouseleave();
270 equals( times, 4, "hover handlers fired" );
273 test("trigger() shortcuts", function() {
275 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
276 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
277 equals( close.length, 0, "Context element does not exist, length must be zero" );
278 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
282 jQuery("#check1").click(function() {
283 ok( true, "click event handler for checkbox gets fired twice, see #815" );
287 jQuery('#firstp')[0].onclick = function(event) {
290 jQuery('#firstp').click();
291 equals( counter, 1, "Check that click, triggers onclick event handler also" );
293 var clickCounter = 0;
294 jQuery('#simon1')[0].onclick = function(event) {
297 jQuery('#simon1').click();
298 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
300 jQuery('<img />').load(function(){
301 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
305 test("trigger() bubbling", function() {
308 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
310 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
311 jQuery("html").bind("click", function(e){ html++; });
312 jQuery("body").bind("click", function(e){ body++; });
313 jQuery("#main").bind("click", function(e){ main++; });
314 jQuery("#ap").bind("click", function(){ ap++; return false; });
316 jQuery("html").trigger("click");
317 equals( doc, 1, "HTML bubble" );
318 equals( html, 1, "HTML bubble" );
320 jQuery("body").trigger("click");
321 equals( doc, 2, "Body bubble" );
322 equals( html, 2, "Body bubble" );
323 equals( body, 1, "Body bubble" );
325 jQuery("#main").trigger("click");
326 equals( doc, 3, "Main bubble" );
327 equals( html, 3, "Main bubble" );
328 equals( body, 2, "Main bubble" );
329 equals( main, 1, "Main bubble" );
331 jQuery("#ap").trigger("click");
332 equals( doc, 3, "ap bubble" );
333 equals( html, 3, "ap bubble" );
334 equals( body, 2, "ap bubble" );
335 equals( main, 1, "ap bubble" );
336 equals( ap, 1, "ap bubble" );
339 test("trigger(type, [data], [fn])", function() {
342 var handler = function(event, a, b, c) {
343 equals( event.type, "click", "check passed data" );
344 equals( a, 1, "check passed data" );
345 equals( b, "2", "check passed data" );
346 equals( c, "abc", "check passed data" );
350 var $elem = jQuery("#firstp");
352 // Simulate a "native" click
353 $elem[0].click = function(){
354 ok( true, "Native call was triggered" );
357 // Triggers handlrs and native
359 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
361 // Simulate a "native" click
362 $elem[0].click = function(){
363 ok( false, "Native call was triggered" );
366 // Trigger only the handlers (no native)
368 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
372 jQuery('#form input:first').hide().trigger('focus');
376 ok( pass, "Trigger focus on hidden element" );
380 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
384 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
387 test("trigger(eventObject, [data], [fn])", function() {
390 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
391 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
393 var event = jQuery.Event("noNew");
394 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
395 equals( event.type, "noNew", "Verify its type" );
397 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
398 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
399 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
401 event.preventDefault();
402 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
403 event.stopPropagation();
404 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
406 event.isPropagationStopped = function(){ return false };
407 event.stopImmediatePropagation();
408 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
409 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
411 $parent.bind('foo',function(e){
413 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
414 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
415 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
416 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
419 // test with an event object
420 event = new jQuery.Event("foo");
421 event.secret = 'boo!';
422 $child.trigger(event);
424 // test with a literal object
425 $child.trigger({type:'foo', secret:'boo!'});
430 ok( false, "This assertion shouldn't be reached");
433 $parent.bind('foo', error );
435 $child.bind('foo',function(e, a, b, c ){
436 equals( arguments.length, 4, "Check arguments length");
437 equals( a, 1, "Check first custom argument");
438 equals( b, 2, "Check second custom argument");
439 equals( c, 3, "Check third custom argument");
441 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
442 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
443 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
446 e.stopImmediatePropagation();
451 // We should add this back in when we want to test the order
452 // in which event handlers are iterated.
453 //$child.bind('foo', error );
455 event = new jQuery.Event("foo");
456 $child.trigger( event, [1,2,3] ).unbind();
457 equals( event.result, "result", "Check event.result attribute");
459 // Will error if it bubbles
460 $child.triggerHandler('foo');
463 $parent.unbind().remove();
466 test("jQuery.Event.currentTarget", function(){
470 $elem = jQuery('<button>a</button>').click(function(e){
471 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
475 $elem.trigger('click');
481 test("toggle(Function, Function, ...)", function() {
485 fn1 = function(e) { count++; },
486 fn2 = function(e) { count--; },
487 preventDefault = function(e) { e.preventDefault() },
488 link = jQuery('#mark');
489 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
490 equals( count, 1, "Check for toggle(fn, fn)" );
492 jQuery("#firstp").toggle(function () {
493 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
494 }, function() {}).trigger("click", [ 1, 2, 3 ]);
497 jQuery("#simon1").one("click", function() {
498 ok( true, "Execute event only once" );
499 jQuery(this).toggle(function() {
500 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
502 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
505 }).click().click().click();
520 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
522 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
524 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
526 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
528 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
530 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
532 $div.unbind('click',fns[0]);
533 var data = jQuery.data( $div[0], 'events' );
534 ok( !data, "Unbinding one function from toggle unbinds them all");
537 test(".live()/.die()", function() {
540 var submit = 0, div = 0, livea = 0, liveb = 0;
542 jQuery("div").live("submit", function(){ submit++; return false; });
543 jQuery("div").live("click", function(){ div++; });
544 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
545 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
547 // Nothing should trigger on the body
548 jQuery("body").trigger("click");
549 equals( submit, 0, "Click on body" );
550 equals( div, 0, "Click on body" );
551 equals( livea, 0, "Click on body" );
552 equals( liveb, 0, "Click on body" );
554 // This should trigger two events
555 jQuery("div#nothiddendiv").trigger("click");
556 equals( submit, 0, "Click on div" );
557 equals( div, 1, "Click on div" );
558 equals( livea, 1, "Click on div" );
559 equals( liveb, 0, "Click on div" );
561 // This should trigger three events (w/ bubbling)
562 jQuery("div#nothiddendivchild").trigger("click");
563 equals( submit, 0, "Click on inner div" );
564 equals( div, 2, "Click on inner div" );
565 equals( livea, 2, "Click on inner div" );
566 equals( liveb, 1, "Click on inner div" );
568 // This should trigger one submit
569 jQuery("div#nothiddendivchild").trigger("submit");
570 equals( submit, 1, "Submit on div" );
571 equals( div, 2, "Submit on div" );
572 equals( livea, 2, "Submit on div" );
573 equals( liveb, 1, "Submit on div" );
575 // Make sure no other events were removed in the process
576 jQuery("div#nothiddendivchild").trigger("click");
577 equals( submit, 1, "die Click on inner div" );
578 equals( div, 3, "die Click on inner div" );
579 equals( livea, 3, "die Click on inner div" );
580 equals( liveb, 2, "die Click on inner div" );
582 // Now make sure that the removal works
583 jQuery("div#nothiddendivchild").die("click");
584 jQuery("div#nothiddendivchild").trigger("click");
585 equals( submit, 1, "die Click on inner div" );
586 equals( div, 4, "die Click on inner div" );
587 equals( livea, 4, "die Click on inner div" );
588 equals( liveb, 2, "die Click on inner div" );
590 // Make sure that the click wasn't removed too early
591 jQuery("div#nothiddendiv").trigger("click");
592 equals( submit, 1, "die Click on inner div" );
593 equals( div, 5, "die Click on inner div" );
594 equals( livea, 5, "die Click on inner div" );
595 equals( liveb, 2, "die Click on inner div" );
597 // Make sure that stopPropgation doesn't stop live events
598 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
599 jQuery("div#nothiddendivchild").trigger("click");
600 equals( submit, 1, "stopPropagation Click on inner div" );
601 equals( div, 6, "stopPropagation Click on inner div" );
602 equals( livea, 6, "stopPropagation Click on inner div" );
603 equals( liveb, 3, "stopPropagation Click on inner div" );
605 jQuery("div#nothiddendivchild").die("click");
606 jQuery("div#nothiddendiv").die("click");
607 jQuery("div").die("click");
608 jQuery("div").die("submit");
610 // Test binding with a different context
611 var clicked = 0, container = jQuery('#main')[0];
612 jQuery("#foo", container).live("click", function(e){ clicked++; });
613 jQuery("div").trigger('click');
614 jQuery("#foo").trigger('click');
615 jQuery("#main").trigger('click');
616 jQuery("body").trigger('click');
617 equals( clicked, 2, "live with a context" );
619 // Make sure the event is actually stored on the context
620 ok( jQuery.data(container, "events").live, "live with a context" );
622 // Test unbinding with a different context
623 jQuery("#foo", container).die("click");
624 jQuery("#foo").trigger('click');
625 equals( clicked, 2, "die with a context");
627 // Test binding with event data
628 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
629 jQuery("#foo").trigger("click").die("click");
631 // Test binding with trigger data
632 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
633 jQuery("#foo").trigger("click", true).die("click");
635 // Test binding with different this object
636 jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
637 jQuery("#foo").trigger("click").die("click");
639 // Test binding with different this object, event data, and trigger data
640 jQuery("#foo").live("click", true, function(e, data){
641 equals( e.data, true, "live with with different this object, event data, and trigger data" );
642 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
643 equals( data, true, "live with with different this object, event data, and trigger data")
645 jQuery("#foo").trigger("click", true).die("click");
647 // Verify that return false prevents default action
648 jQuery("#anchor2").live("click", function(){ return false; });
649 var hash = window.location.hash;
650 jQuery("#anchor2").trigger("click");
651 equals( window.location.hash, hash, "return false worked" );
652 jQuery("#anchor2").die("click");
654 // Verify that .preventDefault() prevents default action
655 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
656 var hash = window.location.hash;
657 jQuery("#anchor2").trigger("click");
658 equals( window.location.hash, hash, "e.preventDefault() worked" );
659 jQuery("#anchor2").die("click");
661 // Test binding the same handler to multiple points
663 function callback(){ called++; return false; }
665 jQuery("#nothiddendiv").live("click", callback);
666 jQuery("#anchor2").live("click", callback);
668 jQuery("#nothiddendiv").trigger("click");
669 equals( called, 1, "Verify that only one click occurred." );
671 jQuery("#anchor2").trigger("click");
672 equals( called, 2, "Verify that only one click occurred." );
674 // Make sure that only one callback is removed
675 jQuery("#anchor2").die("click", callback);
677 jQuery("#nothiddendiv").trigger("click");
678 equals( called, 3, "Verify that only one click occurred." );
680 jQuery("#anchor2").trigger("click");
681 equals( called, 3, "Verify that no click occurred." );
683 // Make sure that it still works if the selector is the same,
684 // but the event type is different
685 jQuery("#nothiddendiv").live("foo", callback);
688 jQuery("#nothiddendiv").die("click", callback);
690 jQuery("#nothiddendiv").trigger("click");
691 equals( called, 3, "Verify that no click occurred." );
693 jQuery("#nothiddendiv").trigger("foo");
694 equals( called, 4, "Verify that one foo occurred." );
697 jQuery("#nothiddendiv").die("foo", callback);
699 // Make sure we don't loose the target by DOM modifications
700 // after the bubble already reached the liveHandler
701 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
703 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
704 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
706 jQuery("#nothiddendiv span").click();
707 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
708 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
711 jQuery("#nothiddendivchild").die("click");
713 // Verify that .live() ocurs and cancel buble in the same order as
714 // we would expect .bind() and .click() without delegation
715 var lived = 0, livee = 0;
717 // bind one pair in one order
718 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
719 jQuery('span#liveSpan1').live('click', function(){ livee++; });
721 jQuery('span#liveSpan1 a').click();
722 equals( lived, 1, "Verify that only one first handler occurred." );
723 equals( livee, 0, "Verify that second handler don't." );
725 // and one pair in inverse
726 jQuery('#liveHandlerOrder span#liveSpan2').live('click', function(){ livee++; });
727 jQuery('#liveHandlerOrder span#liveSpan2 a').live('click', function(){ lived++; return false; });
729 jQuery('span#liveSpan2 a').click();
730 equals( lived, 2, "Verify that only one first handler occurred." );
731 equals( livee, 0, "Verify that second handler don't." );
734 jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
736 // Test this, target and currentTarget are correct
737 jQuery('span#liveSpan1').live('click', function(e){
738 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
739 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
740 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
743 jQuery('span#liveSpan1 a').click();
745 jQuery('span#liveSpan1').die('click');
748 test("Non DOM element events", function() {
752 .bind('nonelementglobal', function(e) {
753 ok( true, "Global event on non-DOM annonymos object triggered" );
759 .bind('nonelementobj', function(e) {
760 ok( true, "Event on non-DOM object triggered" );
761 }).bind('nonelementglobal', function() {
762 ok( true, "Global event on non-DOM object triggered" );
765 jQuery(o).trigger('nonelementobj');
766 jQuery.event.trigger('nonelementglobal');
770 test("jQuery(function($) {})", function() {
773 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");
778 test("event properties", function() {
780 jQuery("#simon1").click(function(event) {
781 ok( event.timeStamp, "assert event.timeStamp is present" );