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" );
379 test("trigger(eventObject, [data], [fn])", function() {
382 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
383 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
385 var event = jQuery.Event("noNew");
386 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
387 equals( event.type, "noNew", "Verify its type" );
389 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
390 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
391 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
393 event.preventDefault();
394 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
395 event.stopPropagation();
396 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
398 event.isPropagationStopped = function(){ return false };
399 event.stopImmediatePropagation();
400 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
401 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
403 $parent.bind('foo',function(e){
405 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
406 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
407 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
408 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
411 // test with an event object
412 event = new jQuery.Event("foo");
413 event.secret = 'boo!';
414 $child.trigger(event);
416 // test with a literal object
417 $child.trigger({type:'foo', secret:'boo!'});
422 ok( false, "This assertion shouldn't be reached");
425 $parent.bind('foo', error );
427 $child.bind('foo',function(e, a, b, c ){
428 equals( arguments.length, 4, "Check arguments length");
429 equals( a, 1, "Check first custom argument");
430 equals( b, 2, "Check second custom argument");
431 equals( c, 3, "Check third custom argument");
433 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
434 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
435 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
438 e.stopImmediatePropagation();
443 // We should add this back in when we want to test the order
444 // in which event handlers are iterated.
445 //$child.bind('foo', error );
447 event = new jQuery.Event("foo");
448 $child.trigger( event, [1,2,3] ).unbind();
449 equals( event.result, "result", "Check event.result attribute");
451 // Will error if it bubbles
452 $child.triggerHandler('foo');
455 $parent.unbind().remove();
458 test("jQuery.Event.currentTarget", function(){
462 $elem = jQuery('<button>a</button>').click(function(e){
463 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
467 $elem.trigger('click');
473 test("toggle(Function, Function, ...)", function() {
477 fn1 = function(e) { count++; },
478 fn2 = function(e) { count--; },
479 preventDefault = function(e) { e.preventDefault() },
480 link = jQuery('#mark');
481 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
482 equals( count, 1, "Check for toggle(fn, fn)" );
484 jQuery("#firstp").toggle(function () {
485 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
486 }, function() {}).trigger("click", [ 1, 2, 3 ]);
489 jQuery("#simon1").one("click", function() {
490 ok( true, "Execute event only once" );
491 jQuery(this).toggle(function() {
492 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
494 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
497 }).click().click().click();
512 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
514 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
516 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
518 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
520 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
522 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
524 $div.unbind('click',fns[0]);
525 var data = jQuery.data( $div[0], 'events' );
526 ok( !data, "Unbinding one function from toggle unbinds them all");
529 test(".live()/.die()", function() {
532 var submit = 0, div = 0, livea = 0, liveb = 0;
534 jQuery("div").live("submit", function(){ submit++; return false; });
535 jQuery("div").live("click", function(){ div++; });
536 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
537 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
539 // Nothing should trigger on the body
540 jQuery("body").trigger("click");
541 equals( submit, 0, "Click on body" );
542 equals( div, 0, "Click on body" );
543 equals( livea, 0, "Click on body" );
544 equals( liveb, 0, "Click on body" );
546 // This should trigger two events
547 jQuery("div#nothiddendiv").trigger("click");
548 equals( submit, 0, "Click on div" );
549 equals( div, 1, "Click on div" );
550 equals( livea, 1, "Click on div" );
551 equals( liveb, 0, "Click on div" );
553 // This should trigger three events (w/ bubbling)
554 jQuery("div#nothiddendivchild").trigger("click");
555 equals( submit, 0, "Click on inner div" );
556 equals( div, 2, "Click on inner div" );
557 equals( livea, 2, "Click on inner div" );
558 equals( liveb, 1, "Click on inner div" );
560 // This should trigger one submit
561 jQuery("div#nothiddendivchild").trigger("submit");
562 equals( submit, 1, "Submit on div" );
563 equals( div, 2, "Submit on div" );
564 equals( livea, 2, "Submit on div" );
565 equals( liveb, 1, "Submit on div" );
567 // Make sure no other events were removed in the process
568 jQuery("div#nothiddendivchild").trigger("click");
569 equals( submit, 1, "die Click on inner div" );
570 equals( div, 3, "die Click on inner div" );
571 equals( livea, 3, "die Click on inner div" );
572 equals( liveb, 2, "die Click on inner div" );
574 // Now make sure that the removal works
575 jQuery("div#nothiddendivchild").die("click");
576 jQuery("div#nothiddendivchild").trigger("click");
577 equals( submit, 1, "die Click on inner div" );
578 equals( div, 4, "die Click on inner div" );
579 equals( livea, 4, "die Click on inner div" );
580 equals( liveb, 2, "die Click on inner div" );
582 // Make sure that the click wasn't removed too early
583 jQuery("div#nothiddendiv").trigger("click");
584 equals( submit, 1, "die Click on inner div" );
585 equals( div, 5, "die Click on inner div" );
586 equals( livea, 5, "die Click on inner div" );
587 equals( liveb, 2, "die Click on inner div" );
589 // Make sure that stopPropgation doesn't stop live events
590 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
591 jQuery("div#nothiddendivchild").trigger("click");
592 equals( submit, 1, "stopPropagation Click on inner div" );
593 equals( div, 6, "stopPropagation Click on inner div" );
594 equals( livea, 6, "stopPropagation Click on inner div" );
595 equals( liveb, 3, "stopPropagation Click on inner div" );
597 jQuery("div#nothiddendivchild").die("click");
598 jQuery("div#nothiddendiv").die("click");
599 jQuery("div").die("click");
600 jQuery("div").die("submit");
602 // Test binding with a different context
603 var clicked = 0, container = jQuery('#main')[0];
604 jQuery("#foo", container).live("click", function(e){ clicked++; });
605 jQuery("div").trigger('click');
606 jQuery("#foo").trigger('click');
607 jQuery("#main").trigger('click');
608 jQuery("body").trigger('click');
609 equals( clicked, 2, "live with a context" );
611 // Make sure the event is actually stored on the context
612 ok( jQuery.data(container, "events").live, "live with a context" );
614 // Test unbinding with a different context
615 jQuery("#foo", container).die("click");
616 jQuery("#foo").trigger('click');
617 equals( clicked, 2, "die with a context");
619 // Test binding with event data
620 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
621 jQuery("#foo").trigger("click").die("click");
623 // Test binding with trigger data
624 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
625 jQuery("#foo").trigger("click", true).die("click");
627 // Test binding with different this object
628 jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
629 jQuery("#foo").trigger("click").die("click");
631 // Test binding with different this object, event data, and trigger data
632 jQuery("#foo").live("click", true, function(e, data){
633 equals( e.data, true, "live with with different this object, event data, and trigger data" );
634 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
635 equals( data, true, "live with with different this object, event data, and trigger data")
637 jQuery("#foo").trigger("click", true).die("click");
639 // Verify that return false prevents default action
640 jQuery("#anchor2").live("click", function(){ return false; });
641 var hash = window.location.hash;
642 jQuery("#anchor2").trigger("click");
643 equals( window.location.hash, hash, "return false worked" );
644 jQuery("#anchor2").die("click");
646 // Verify that .preventDefault() prevents default action
647 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
648 var hash = window.location.hash;
649 jQuery("#anchor2").trigger("click");
650 equals( window.location.hash, hash, "e.preventDefault() worked" );
651 jQuery("#anchor2").die("click");
653 // Test binding the same handler to multiple points
655 function callback(){ called++; return false; }
657 jQuery("#nothiddendiv").live("click", callback);
658 jQuery("#anchor2").live("click", callback);
660 jQuery("#nothiddendiv").trigger("click");
661 equals( called, 1, "Verify that only one click occurred." );
663 jQuery("#anchor2").trigger("click");
664 equals( called, 2, "Verify that only one click occurred." );
666 // Make sure that only one callback is removed
667 jQuery("#anchor2").die("click", callback);
669 jQuery("#nothiddendiv").trigger("click");
670 equals( called, 3, "Verify that only one click occurred." );
672 jQuery("#anchor2").trigger("click");
673 equals( called, 3, "Verify that no click occurred." );
675 // Make sure that it still works if the selector is the same,
676 // but the event type is different
677 jQuery("#nothiddendiv").live("foo", callback);
680 jQuery("#nothiddendiv").die("click", callback);
682 jQuery("#nothiddendiv").trigger("click");
683 equals( called, 3, "Verify that no click occurred." );
685 jQuery("#nothiddendiv").trigger("foo");
686 equals( called, 4, "Verify that one foo occurred." );
689 jQuery("#nothiddendiv").die("foo", callback);
691 // Make sure we don't loose the target by DOM modifications
692 // after the bubble already reached the liveHandler
693 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
695 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
696 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
698 jQuery("#nothiddendiv span").click();
699 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
700 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
703 jQuery("#nothiddendivchild").die("click");
705 // Verify that .live() ocurs and cancel buble in the same order as
706 // we would expect .bind() and .click() without delegation
707 var lived = 0, livee = 0;
709 // bind one pair in one order
710 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
711 jQuery('span#liveSpan1').live('click', function(){ livee++; });
713 jQuery('span#liveSpan1 a').click();
714 equals( lived, 1, "Verify that only one first handler occurred." );
715 equals( livee, 0, "Verify that second handler don't." );
717 // and one pair in inverse
718 jQuery('#liveHandlerOrder span#liveSpan2').live('click', function(){ livee++; });
719 jQuery('#liveHandlerOrder span#liveSpan2 a').live('click', function(){ lived++; return false; });
721 jQuery('span#liveSpan2 a').click();
722 equals( lived, 2, "Verify that only one first handler occurred." );
723 equals( livee, 0, "Verify that second handler don't." );
726 jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
728 // Test this, target and currentTarget are correct
729 jQuery('span#liveSpan1').live('click', function(e){
730 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
731 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
732 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
735 jQuery('span#liveSpan1 a').click();
737 jQuery('span#liveSpan1').die('click');
740 test("Non DOM element events", function() {
744 .bind('nonelementglobal', function(e) {
745 ok( true, "Global event on non-DOM annonymos object triggered" );
751 .bind('nonelementobj', function(e) {
752 ok( true, "Event on non-DOM object triggered" );
753 }).bind('nonelementglobal', function() {
754 ok( true, "Global event on non-DOM object triggered" );
757 jQuery(o).trigger('nonelementobj');
758 jQuery.event.trigger('nonelementglobal');
762 test("jQuery(function($) {})", function() {
765 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");
770 test("event properties", function() {
772 jQuery("#simon1").click(function(event) {
773 ok( event.timeStamp, "assert event.timeStamp is present" );