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("trigger() shortcuts", function() {
238 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
239 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
240 equals( close.length, 0, "Context element does not exist, length must be zero" );
241 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
245 jQuery("#check1").click(function() {
246 ok( true, "click event handler for checkbox gets fired twice, see #815" );
250 jQuery('#firstp')[0].onclick = function(event) {
253 jQuery('#firstp').click();
254 equals( counter, 1, "Check that click, triggers onclick event handler also" );
256 var clickCounter = 0;
257 jQuery('#simon1')[0].onclick = function(event) {
260 jQuery('#simon1').click();
261 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
263 jQuery('<img />').load(function(){
264 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
268 test("trigger() bubbling", function() {
271 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
273 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
274 jQuery("html").bind("click", function(e){ html++; });
275 jQuery("body").bind("click", function(e){ body++; });
276 jQuery("#main").bind("click", function(e){ main++; });
277 jQuery("#ap").bind("click", function(){ ap++; return false; });
279 jQuery("html").trigger("click");
280 equals( doc, 1, "HTML bubble" );
281 equals( html, 1, "HTML bubble" );
283 jQuery("body").trigger("click");
284 equals( doc, 2, "Body bubble" );
285 equals( html, 2, "Body bubble" );
286 equals( body, 1, "Body bubble" );
288 jQuery("#main").trigger("click");
289 equals( doc, 3, "Main bubble" );
290 equals( html, 3, "Main bubble" );
291 equals( body, 2, "Main bubble" );
292 equals( main, 1, "Main bubble" );
294 jQuery("#ap").trigger("click");
295 equals( doc, 3, "ap bubble" );
296 equals( html, 3, "ap bubble" );
297 equals( body, 2, "ap bubble" );
298 equals( main, 1, "ap bubble" );
299 equals( ap, 1, "ap bubble" );
302 test("trigger(type, [data], [fn])", function() {
305 var handler = function(event, a, b, c) {
306 equals( event.type, "click", "check passed data" );
307 equals( a, 1, "check passed data" );
308 equals( b, "2", "check passed data" );
309 equals( c, "abc", "check passed data" );
313 var $elem = jQuery("#firstp");
315 // Simulate a "native" click
316 $elem[0].click = function(){
317 ok( true, "Native call was triggered" );
320 // Triggers handlrs and native
322 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
324 // Simulate a "native" click
325 $elem[0].click = function(){
326 ok( false, "Native call was triggered" );
329 // Trigger only the handlers (no native)
331 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
335 jQuery('#form input:first').hide().trigger('focus');
339 ok( pass, "Trigger focus on hidden element" );
342 test("trigger(eventObject, [data], [fn])", function() {
345 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
346 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
348 var event = jQuery.Event("noNew");
349 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
350 equals( event.type, "noNew", "Verify its type" );
352 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
353 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
354 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
356 event.preventDefault();
357 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
358 event.stopPropagation();
359 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
361 event.isPropagationStopped = function(){ return false };
362 event.stopImmediatePropagation();
363 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
364 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
366 $parent.bind('foo',function(e){
368 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
369 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
370 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
371 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
374 // test with an event object
375 event = new jQuery.Event("foo");
376 event.secret = 'boo!';
377 $child.trigger(event);
379 // test with a literal object
380 $child.trigger({type:'foo', secret:'boo!'});
385 ok( false, "This assertion shouldn't be reached");
388 $parent.bind('foo', error );
390 $child.bind('foo',function(e, a, b, c ){
391 equals( arguments.length, 4, "Check arguments length");
392 equals( a, 1, "Check first custom argument");
393 equals( b, 2, "Check second custom argument");
394 equals( c, 3, "Check third custom argument");
396 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
397 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
398 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
401 e.stopImmediatePropagation();
406 // We should add this back in when we want to test the order
407 // in which event handlers are iterated.
408 //$child.bind('foo', error );
410 event = new jQuery.Event("foo");
411 $child.trigger( event, [1,2,3] ).unbind();
412 equals( event.result, "result", "Check event.result attribute");
414 // Will error if it bubbles
415 $child.triggerHandler('foo');
418 $parent.unbind().remove();
421 test("jQuery.Event.currentTarget", function(){
425 $elem = jQuery('<button>a</button>').click(function(e){
426 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
430 $elem.trigger('click');
436 test("toggle(Function, Function, ...)", function() {
440 fn1 = function(e) { count++; },
441 fn2 = function(e) { count--; },
442 preventDefault = function(e) { e.preventDefault() },
443 link = jQuery('#mark');
444 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
445 equals( count, 1, "Check for toggle(fn, fn)" );
447 jQuery("#firstp").toggle(function () {
448 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
449 }, function() {}).trigger("click", [ 1, 2, 3 ]);
452 jQuery("#simon1").one("click", function() {
453 ok( true, "Execute event only once" );
454 jQuery(this).toggle(function() {
455 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
457 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
460 }).click().click().click();
475 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
477 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
479 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
481 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
483 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
485 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
487 $div.unbind('click',fns[0]);
488 var data = jQuery.data( $div[0], 'events' );
489 ok( !data, "Unbinding one function from toggle unbinds them all");
492 test(".live()/.die()", function() {
495 var submit = 0, div = 0, livea = 0, liveb = 0;
497 jQuery("div").live("submit", function(){ submit++; return false; });
498 jQuery("div").live("click", function(){ div++; });
499 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
500 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
502 // Nothing should trigger on the body
503 jQuery("body").trigger("click");
504 equals( submit, 0, "Click on body" );
505 equals( div, 0, "Click on body" );
506 equals( livea, 0, "Click on body" );
507 equals( liveb, 0, "Click on body" );
509 // This should trigger two events
510 jQuery("div#nothiddendiv").trigger("click");
511 equals( submit, 0, "Click on div" );
512 equals( div, 1, "Click on div" );
513 equals( livea, 1, "Click on div" );
514 equals( liveb, 0, "Click on div" );
516 // This should trigger three events (w/ bubbling)
517 jQuery("div#nothiddendivchild").trigger("click");
518 equals( submit, 0, "Click on inner div" );
519 equals( div, 2, "Click on inner div" );
520 equals( livea, 2, "Click on inner div" );
521 equals( liveb, 1, "Click on inner div" );
523 // This should trigger one submit
524 jQuery("div#nothiddendivchild").trigger("submit");
525 equals( submit, 1, "Submit on div" );
526 equals( div, 2, "Submit on div" );
527 equals( livea, 2, "Submit on div" );
528 equals( liveb, 1, "Submit on div" );
530 // Make sure no other events were removed in the process
531 jQuery("div#nothiddendivchild").trigger("click");
532 equals( submit, 1, "die Click on inner div" );
533 equals( div, 3, "die Click on inner div" );
534 equals( livea, 3, "die Click on inner div" );
535 equals( liveb, 2, "die Click on inner div" );
537 // Now make sure that the removal works
538 jQuery("div#nothiddendivchild").die("click");
539 jQuery("div#nothiddendivchild").trigger("click");
540 equals( submit, 1, "die Click on inner div" );
541 equals( div, 4, "die Click on inner div" );
542 equals( livea, 4, "die Click on inner div" );
543 equals( liveb, 2, "die Click on inner div" );
545 // Make sure that the click wasn't removed too early
546 jQuery("div#nothiddendiv").trigger("click");
547 equals( submit, 1, "die Click on inner div" );
548 equals( div, 5, "die Click on inner div" );
549 equals( livea, 5, "die Click on inner div" );
550 equals( liveb, 2, "die Click on inner div" );
552 // Make sure that stopPropgation doesn't stop live events
553 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
554 jQuery("div#nothiddendivchild").trigger("click");
555 equals( submit, 1, "stopPropagation Click on inner div" );
556 equals( div, 6, "stopPropagation Click on inner div" );
557 equals( livea, 6, "stopPropagation Click on inner div" );
558 equals( liveb, 3, "stopPropagation Click on inner div" );
560 jQuery("div#nothiddendivchild").die("click");
561 jQuery("div#nothiddendiv").die("click");
562 jQuery("div").die("click");
563 jQuery("div").die("submit");
565 // Test binding with a different context
566 var clicked = 0, container = jQuery('#main')[0];
567 jQuery("#foo", container).live("click", function(e){ clicked++; });
568 jQuery("div").trigger('click');
569 jQuery("#foo").trigger('click');
570 jQuery("#main").trigger('click');
571 jQuery("body").trigger('click');
572 equals( clicked, 2, "live with a context" );
574 // Make sure the event is actually stored on the context
575 ok( jQuery.data(container, "events").live, "live with a context" );
577 // Test unbinding with a different context
578 jQuery("#foo", container).die("click");
579 jQuery("#foo").trigger('click');
580 equals( clicked, 2, "die with a context");
582 // Test binding with event data
583 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
584 jQuery("#foo").trigger("click").die("click");
586 // Test binding with trigger data
587 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
588 jQuery("#foo").trigger("click", true).die("click");
590 // Verify that return false prevents default action
591 jQuery("#anchor2").live("click", function(){ return false; });
592 var hash = window.location.hash;
593 jQuery("#anchor2").trigger("click");
594 equals( window.location.hash, hash, "return false worked" );
595 jQuery("#anchor2").die("click");
597 // Verify that .preventDefault() prevents default action
598 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
599 var hash = window.location.hash;
600 jQuery("#anchor2").trigger("click");
601 equals( window.location.hash, hash, "e.preventDefault() worked" );
602 jQuery("#anchor2").die("click");
604 // Test binding the same handler to multiple points
606 function callback(){ called++; return false; }
608 jQuery("#nothiddendiv").live("click", callback);
609 jQuery("#anchor2").live("click", callback);
611 jQuery("#nothiddendiv").trigger("click");
612 equals( called, 1, "Verify that only one click occurred." );
614 jQuery("#anchor2").trigger("click");
615 equals( called, 2, "Verify that only one click occurred." );
617 // Make sure that only one callback is removed
618 jQuery("#anchor2").die("click", callback);
620 jQuery("#nothiddendiv").trigger("click");
621 equals( called, 3, "Verify that only one click occurred." );
623 jQuery("#anchor2").trigger("click");
624 equals( called, 3, "Verify that no click occurred." );
626 // Make sure that it still works if the selector is the same,
627 // but the event type is different
628 jQuery("#nothiddendiv").live("foo", callback);
631 jQuery("#nothiddendiv").die("click", callback);
633 jQuery("#nothiddendiv").trigger("click");
634 equals( called, 3, "Verify that no click occurred." );
636 jQuery("#nothiddendiv").trigger("foo");
637 equals( called, 4, "Verify that one foo occurred." );
640 jQuery("#nothiddendiv").die("foo", callback);
642 // Make sure we don't loose the target by DOM modifications
643 // after the bubble already reached the liveHandler
644 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
646 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
647 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
649 jQuery("#nothiddendiv span").click();
650 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
651 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
654 jQuery("#nothiddendivchild").die("click");
656 // Verify that .live() ocurs and cancel buble in the same order as
657 // we would expect .bind() and .click() without delegation
658 var lived = 0, livee = 0;
660 // bind one pair in one order
661 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
662 jQuery('span#liveSpan1').live('click', function(){ livee++; });
664 jQuery('span#liveSpan1 a').click();
665 equals( lived, 1, "Verify that only one first handler occurred." );
666 equals( livee, 0, "Verify that second handler don't." );
668 // and one pair in inverse
669 jQuery('#liveHandlerOrder span#liveSpan2').live('click', function(){ livee++; });
670 jQuery('#liveHandlerOrder span#liveSpan2 a').live('click', function(){ lived++; return false; });
672 jQuery('span#liveSpan2 a').click();
673 equals( lived, 2, "Verify that only one first handler occurred." );
674 equals( livee, 0, "Verify that second handler don't." );
677 jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
679 // Test this, target and currentTarget are correct
680 jQuery('span#liveSpan1').live('click', function(e){
681 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
682 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
683 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
686 jQuery('span#liveSpan1 a').click();
688 jQuery('span#liveSpan1').die('click');
692 test("jQuery(function($) {})", function() {
695 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");
700 test("event properties", function() {
702 jQuery("#simon1").click(function(event) {
703 ok( event.timeStamp, "assert event.timeStamp is present" );