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("unbind(type)", function() {
168 var $elem = jQuery("#firstp"),
172 ok( false, message );
175 message = "unbind passing function";
176 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
178 message = "unbind all from event";
179 $elem.bind('error', error).unbind('error').triggerHandler('error');
181 message = "unbind all";
182 $elem.bind('error', error).unbind().triggerHandler('error');
184 message = "unbind many with function";
185 $elem.bind('error error2',error)
186 .unbind('error error2', error )
187 .trigger('error').triggerHandler('error2');
189 message = "unbind many"; // #3538
190 $elem.bind('error error2',error)
191 .unbind('error error2')
192 .trigger('error').triggerHandler('error2');
194 message = "unbind without a type or handler";
195 $elem.bind("error error2.test",error)
197 .trigger("error").triggerHandler("error2");
200 test("unbind(eventObject)", function() {
203 var $elem = jQuery("#firstp"),
206 function assert( expected ){
208 $elem.trigger('foo').triggerHandler('bar');
209 equals( num, expected, "Check the right handlers are triggered" );
213 // This handler shouldn't be unbound
214 .bind('foo', function(){
217 .bind('foo', function(e){
222 .bind('bar', function(){
236 test("hover()", function() {
238 handler1 = function( event ) { ++times; },
239 handler2 = function( event ) { ++times; };
242 .hover(handler1, handler2)
243 .mouseenter().mouseleave()
244 .unbind("mouseenter", handler1)
245 .unbind("mouseleave", handler2)
247 .mouseenter().mouseleave()
248 .unbind("mouseenter mouseleave", handler1)
249 .mouseenter().mouseleave();
251 equals( times, 4, "hover handlers fired" );
254 test("trigger() shortcuts", function() {
256 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
257 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
258 equals( close.length, 0, "Context element does not exist, length must be zero" );
259 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
263 jQuery("#check1").click(function() {
264 ok( true, "click event handler for checkbox gets fired twice, see #815" );
268 jQuery('#firstp')[0].onclick = function(event) {
271 jQuery('#firstp').click();
272 equals( counter, 1, "Check that click, triggers onclick event handler also" );
274 var clickCounter = 0;
275 jQuery('#simon1')[0].onclick = function(event) {
278 jQuery('#simon1').click();
279 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
281 jQuery('<img />').load(function(){
282 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
286 test("trigger() bubbling", function() {
289 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
291 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
292 jQuery("html").bind("click", function(e){ html++; });
293 jQuery("body").bind("click", function(e){ body++; });
294 jQuery("#main").bind("click", function(e){ main++; });
295 jQuery("#ap").bind("click", function(){ ap++; return false; });
297 jQuery("html").trigger("click");
298 equals( doc, 1, "HTML bubble" );
299 equals( html, 1, "HTML bubble" );
301 jQuery("body").trigger("click");
302 equals( doc, 2, "Body bubble" );
303 equals( html, 2, "Body bubble" );
304 equals( body, 1, "Body bubble" );
306 jQuery("#main").trigger("click");
307 equals( doc, 3, "Main bubble" );
308 equals( html, 3, "Main bubble" );
309 equals( body, 2, "Main bubble" );
310 equals( main, 1, "Main bubble" );
312 jQuery("#ap").trigger("click");
313 equals( doc, 3, "ap bubble" );
314 equals( html, 3, "ap bubble" );
315 equals( body, 2, "ap bubble" );
316 equals( main, 1, "ap bubble" );
317 equals( ap, 1, "ap bubble" );
320 test("trigger(type, [data], [fn])", function() {
323 var handler = function(event, a, b, c) {
324 equals( event.type, "click", "check passed data" );
325 equals( a, 1, "check passed data" );
326 equals( b, "2", "check passed data" );
327 equals( c, "abc", "check passed data" );
331 var $elem = jQuery("#firstp");
333 // Simulate a "native" click
334 $elem[0].click = function(){
335 ok( true, "Native call was triggered" );
338 // Triggers handlrs and native
340 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
342 // Simulate a "native" click
343 $elem[0].click = function(){
344 ok( false, "Native call was triggered" );
347 // Trigger only the handlers (no native)
349 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
353 jQuery('#form input:first').hide().trigger('focus');
357 ok( pass, "Trigger focus on hidden element" );
360 test("trigger(eventObject, [data], [fn])", function() {
363 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
364 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
366 var event = jQuery.Event("noNew");
367 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
368 equals( event.type, "noNew", "Verify its type" );
370 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
371 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
372 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
374 event.preventDefault();
375 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
376 event.stopPropagation();
377 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
379 event.isPropagationStopped = function(){ return false };
380 event.stopImmediatePropagation();
381 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
382 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
384 $parent.bind('foo',function(e){
386 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
387 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
388 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
389 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
392 // test with an event object
393 event = new jQuery.Event("foo");
394 event.secret = 'boo!';
395 $child.trigger(event);
397 // test with a literal object
398 $child.trigger({type:'foo', secret:'boo!'});
403 ok( false, "This assertion shouldn't be reached");
406 $parent.bind('foo', error );
408 $child.bind('foo',function(e, a, b, c ){
409 equals( arguments.length, 4, "Check arguments length");
410 equals( a, 1, "Check first custom argument");
411 equals( b, 2, "Check second custom argument");
412 equals( c, 3, "Check third custom argument");
414 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
415 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
416 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
419 e.stopImmediatePropagation();
424 // We should add this back in when we want to test the order
425 // in which event handlers are iterated.
426 //$child.bind('foo', error );
428 event = new jQuery.Event("foo");
429 $child.trigger( event, [1,2,3] ).unbind();
430 equals( event.result, "result", "Check event.result attribute");
432 // Will error if it bubbles
433 $child.triggerHandler('foo');
436 $parent.unbind().remove();
439 test("jQuery.Event.currentTarget", function(){
443 $elem = jQuery('<button>a</button>').click(function(e){
444 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
448 $elem.trigger('click');
454 test("toggle(Function, Function, ...)", function() {
458 fn1 = function(e) { count++; },
459 fn2 = function(e) { count--; },
460 preventDefault = function(e) { e.preventDefault() },
461 link = jQuery('#mark');
462 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
463 equals( count, 1, "Check for toggle(fn, fn)" );
465 jQuery("#firstp").toggle(function () {
466 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
467 }, function() {}).trigger("click", [ 1, 2, 3 ]);
470 jQuery("#simon1").one("click", function() {
471 ok( true, "Execute event only once" );
472 jQuery(this).toggle(function() {
473 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
475 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
478 }).click().click().click();
493 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
495 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
497 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
499 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
501 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
503 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
505 $div.unbind('click',fns[0]);
506 var data = jQuery.data( $div[0], 'events' );
507 ok( !data, "Unbinding one function from toggle unbinds them all");
510 test(".live()/.die()", function() {
513 var submit = 0, div = 0, livea = 0, liveb = 0;
515 jQuery("div").live("submit", function(){ submit++; return false; });
516 jQuery("div").live("click", function(){ div++; });
517 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
518 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
520 // Nothing should trigger on the body
521 jQuery("body").trigger("click");
522 equals( submit, 0, "Click on body" );
523 equals( div, 0, "Click on body" );
524 equals( livea, 0, "Click on body" );
525 equals( liveb, 0, "Click on body" );
527 // This should trigger two events
528 jQuery("div#nothiddendiv").trigger("click");
529 equals( submit, 0, "Click on div" );
530 equals( div, 1, "Click on div" );
531 equals( livea, 1, "Click on div" );
532 equals( liveb, 0, "Click on div" );
534 // This should trigger three events (w/ bubbling)
535 jQuery("div#nothiddendivchild").trigger("click");
536 equals( submit, 0, "Click on inner div" );
537 equals( div, 2, "Click on inner div" );
538 equals( livea, 2, "Click on inner div" );
539 equals( liveb, 1, "Click on inner div" );
541 // This should trigger one submit
542 jQuery("div#nothiddendivchild").trigger("submit");
543 equals( submit, 1, "Submit on div" );
544 equals( div, 2, "Submit on div" );
545 equals( livea, 2, "Submit on div" );
546 equals( liveb, 1, "Submit on div" );
548 // Make sure no other events were removed in the process
549 jQuery("div#nothiddendivchild").trigger("click");
550 equals( submit, 1, "die Click on inner div" );
551 equals( div, 3, "die Click on inner div" );
552 equals( livea, 3, "die Click on inner div" );
553 equals( liveb, 2, "die Click on inner div" );
555 // Now make sure that the removal works
556 jQuery("div#nothiddendivchild").die("click");
557 jQuery("div#nothiddendivchild").trigger("click");
558 equals( submit, 1, "die Click on inner div" );
559 equals( div, 4, "die Click on inner div" );
560 equals( livea, 4, "die Click on inner div" );
561 equals( liveb, 2, "die Click on inner div" );
563 // Make sure that the click wasn't removed too early
564 jQuery("div#nothiddendiv").trigger("click");
565 equals( submit, 1, "die Click on inner div" );
566 equals( div, 5, "die Click on inner div" );
567 equals( livea, 5, "die Click on inner div" );
568 equals( liveb, 2, "die Click on inner div" );
570 // Make sure that stopPropgation doesn't stop live events
571 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
572 jQuery("div#nothiddendivchild").trigger("click");
573 equals( submit, 1, "stopPropagation Click on inner div" );
574 equals( div, 6, "stopPropagation Click on inner div" );
575 equals( livea, 6, "stopPropagation Click on inner div" );
576 equals( liveb, 3, "stopPropagation Click on inner div" );
578 jQuery("div#nothiddendivchild").die("click");
579 jQuery("div#nothiddendiv").die("click");
580 jQuery("div").die("click");
581 jQuery("div").die("submit");
583 // Test binding with a different context
584 var clicked = 0, container = jQuery('#main')[0];
585 jQuery("#foo", container).live("click", function(e){ clicked++; });
586 jQuery("div").trigger('click');
587 jQuery("#foo").trigger('click');
588 jQuery("#main").trigger('click');
589 jQuery("body").trigger('click');
590 equals( clicked, 2, "live with a context" );
592 // Make sure the event is actually stored on the context
593 ok( jQuery.data(container, "events").live, "live with a context" );
595 // Test unbinding with a different context
596 jQuery("#foo", container).die("click");
597 jQuery("#foo").trigger('click');
598 equals( clicked, 2, "die with a context");
600 // Test binding with event data
601 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
602 jQuery("#foo").trigger("click").die("click");
604 // Test binding with trigger data
605 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
606 jQuery("#foo").trigger("click", true).die("click");
608 // Verify that return false prevents default action
609 jQuery("#anchor2").live("click", function(){ return false; });
610 var hash = window.location.hash;
611 jQuery("#anchor2").trigger("click");
612 equals( window.location.hash, hash, "return false worked" );
613 jQuery("#anchor2").die("click");
615 // Verify that .preventDefault() prevents default action
616 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
617 var hash = window.location.hash;
618 jQuery("#anchor2").trigger("click");
619 equals( window.location.hash, hash, "e.preventDefault() worked" );
620 jQuery("#anchor2").die("click");
622 // Test binding the same handler to multiple points
624 function callback(){ called++; return false; }
626 jQuery("#nothiddendiv").live("click", callback);
627 jQuery("#anchor2").live("click", callback);
629 jQuery("#nothiddendiv").trigger("click");
630 equals( called, 1, "Verify that only one click occurred." );
632 jQuery("#anchor2").trigger("click");
633 equals( called, 2, "Verify that only one click occurred." );
635 // Make sure that only one callback is removed
636 jQuery("#anchor2").die("click", callback);
638 jQuery("#nothiddendiv").trigger("click");
639 equals( called, 3, "Verify that only one click occurred." );
641 jQuery("#anchor2").trigger("click");
642 equals( called, 3, "Verify that no click occurred." );
644 // Make sure that it still works if the selector is the same,
645 // but the event type is different
646 jQuery("#nothiddendiv").live("foo", callback);
649 jQuery("#nothiddendiv").die("click", callback);
651 jQuery("#nothiddendiv").trigger("click");
652 equals( called, 3, "Verify that no click occurred." );
654 jQuery("#nothiddendiv").trigger("foo");
655 equals( called, 4, "Verify that one foo occurred." );
658 jQuery("#nothiddendiv").die("foo", callback);
660 // Make sure we don't loose the target by DOM modifications
661 // after the bubble already reached the liveHandler
662 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
664 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
665 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
667 jQuery("#nothiddendiv span").click();
668 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
669 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
672 jQuery("#nothiddendivchild").die("click");
674 // Verify that .live() ocurs and cancel buble in the same order as
675 // we would expect .bind() and .click() without delegation
676 var lived = 0, livee = 0;
678 // bind one pair in one order
679 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
680 jQuery('span#liveSpan1').live('click', function(){ livee++; });
682 jQuery('span#liveSpan1 a').click();
683 equals( lived, 1, "Verify that only one first handler occurred." );
684 equals( livee, 0, "Verify that second handler don't." );
686 // and one pair in inverse
687 jQuery('#liveHandlerOrder span#liveSpan2').live('click', function(){ livee++; });
688 jQuery('#liveHandlerOrder span#liveSpan2 a').live('click', function(){ lived++; return false; });
690 jQuery('span#liveSpan2 a').click();
691 equals( lived, 2, "Verify that only one first handler occurred." );
692 equals( livee, 0, "Verify that second handler don't." );
695 jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
697 // Test this, target and currentTarget are correct
698 jQuery('span#liveSpan1').live('click', function(e){
699 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
700 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
701 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
704 jQuery('span#liveSpan1 a').click();
706 jQuery('span#liveSpan1').die('click');
709 test("Non DOM element events", function() {
713 .bind('nonelementglobal', function(e) {
714 ok( true, "Global event on non-DOM annonymos object triggered" );
720 .bind('nonelementobj', function(e) {
721 ok( true, "Event on non-DOM object triggered" );
722 }).bind('nonelementglobal', function() {
723 ok( true, "Global event on non-DOM object triggered" );
726 jQuery(o).trigger('nonelementobj');
727 jQuery.event.trigger('nonelementglobal');
731 test("jQuery(function($) {})", function() {
734 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");
739 test("event properties", function() {
741 jQuery("#simon1").click(function(event) {
742 ok( event.timeStamp, "assert event.timeStamp is present" );