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 = document.getElementById("iframe").contentDocument;
52 // doc.body.innerHTML = "<input type='text'/>";
54 // var input = doc.getElementsByTagName("input")[0];
56 // jQuery(input).bind("click",function() {
57 // ok( true, "Binding to element inside iframe" );
61 test("bind(), trigger change on select", function() {
64 function selectOnChange(event) {
65 equals( event.data, counter++, "Event.data is not a global event object" );
67 jQuery("#form select").each(function(i){
68 jQuery(this).bind('change', i, selectOnChange);
72 test("bind(), namespaced events, cloned events", function() {
75 jQuery("#firstp").bind("custom.test",function(e){
76 ok(true, "Custom event triggered");
79 jQuery("#firstp").bind("click",function(e){
80 ok(true, "Normal click triggered");
83 jQuery("#firstp").bind("click.test",function(e){
84 ok(true, "Namespaced click triggered");
87 // Trigger both bound fn (2)
88 jQuery("#firstp").trigger("click");
90 // Trigger one bound fn (1)
91 jQuery("#firstp").trigger("click.test");
93 // Remove only the one fn
94 jQuery("#firstp").unbind("click.test");
96 // Trigger the remaining fn (1)
97 jQuery("#firstp").trigger("click");
99 // Remove the remaining fn
100 jQuery("#firstp").unbind(".test");
102 // Trigger the remaining fn (0)
103 jQuery("#firstp").trigger("custom");
105 // using contents will get comments regular, text, and comment nodes
106 jQuery("#nonnodes").contents().bind("tester", function () {
107 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
108 }).trigger("tester");
110 // Make sure events stick with appendTo'd elements (which are cloned) #2027
111 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
112 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
115 test("bind(), multi-namespaced events", function() {
127 function check(name, msg){
128 same(name, order.shift(), msg);
131 jQuery("#firstp").bind("custom.test",function(e){
132 check("custom.test", "Custom event triggered");
135 jQuery("#firstp").bind("custom.test2",function(e){
136 check("custom.test2", "Custom event triggered");
139 jQuery("#firstp").bind("click.test",function(e){
140 check("click.test", "Normal click triggered");
143 jQuery("#firstp").bind("click.test.abc",function(e){
144 check("click.test.abc", "Namespaced click triggered");
147 // Trigger both bound fn (1)
148 jQuery("#firstp").trigger("click.test.abc");
150 // Trigger one bound fn (1)
151 jQuery("#firstp").trigger("click.abc");
153 // Trigger two bound fn (2)
154 jQuery("#firstp").trigger("click.test");
156 // Remove only the one fn
157 jQuery("#firstp").unbind("click.abc");
159 // Trigger the remaining fn (1)
160 jQuery("#firstp").trigger("click");
162 // Remove the remaining fn
163 jQuery("#firstp").unbind(".test");
165 // Trigger the remaining fn (1)
166 jQuery("#firstp").trigger("custom");
169 test("unbind(type)", function() {
172 var $elem = jQuery("#firstp"),
176 ok( false, message );
179 message = "unbind passing function";
180 $elem.bind('error', error).unbind('error',error).triggerHandler('error');
182 message = "unbind all from event";
183 $elem.bind('error', error).unbind('error').triggerHandler('error');
185 message = "unbind all";
186 $elem.bind('error', error).unbind().triggerHandler('error');
188 message = "unbind many with function";
189 $elem.bind('error error2',error)
190 .unbind('error error2', error )
191 .trigger('error').triggerHandler('error2');
193 message = "unbind many"; // #3538
194 $elem.bind('error error2',error)
195 .unbind('error error2')
196 .trigger('error').triggerHandler('error2');
199 test("unbind(eventObject)", function() {
202 var $elem = jQuery("#firstp"),
205 function assert( expected ){
207 $elem.trigger('foo').triggerHandler('bar');
208 equals( num, expected, "Check the right handlers are triggered" );
212 // This handler shouldn't be unbound
213 .bind('foo', function(){
216 .bind('foo', function(e){
221 .bind('bar', function(){
235 test("trigger() shortcuts", function() {
237 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
238 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
239 equals( close.length, 0, "Context element does not exist, length must be zero" );
240 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
244 jQuery("#check1").click(function() {
245 ok( true, "click event handler for checkbox gets fired twice, see #815" );
249 jQuery('#firstp')[0].onclick = function(event) {
252 jQuery('#firstp').click();
253 equals( counter, 1, "Check that click, triggers onclick event handler also" );
255 var clickCounter = 0;
256 jQuery('#simon1')[0].onclick = function(event) {
259 jQuery('#simon1').click();
260 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
262 jQuery('<img />').load(function(){
263 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
267 test("trigger() bubbling", function() {
270 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
272 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
273 jQuery("html").bind("click", function(e){ html++; });
274 jQuery("body").bind("click", function(e){ body++; });
275 jQuery("#main").bind("click", function(e){ main++; });
276 jQuery("#ap").bind("click", function(){ ap++; return false; });
278 jQuery("html").trigger("click");
279 equals( doc, 1, "HTML bubble" );
280 equals( html, 1, "HTML bubble" );
282 jQuery("body").trigger("click");
283 equals( doc, 2, "Body bubble" );
284 equals( html, 2, "Body bubble" );
285 equals( body, 1, "Body bubble" );
287 jQuery("#main").trigger("click");
288 equals( doc, 3, "Main bubble" );
289 equals( html, 3, "Main bubble" );
290 equals( body, 2, "Main bubble" );
291 equals( main, 1, "Main bubble" );
293 jQuery("#ap").trigger("click");
294 equals( doc, 3, "ap bubble" );
295 equals( html, 3, "ap bubble" );
296 equals( body, 2, "ap bubble" );
297 equals( main, 1, "ap bubble" );
298 equals( ap, 1, "ap bubble" );
301 test("trigger(type, [data], [fn])", function() {
304 var handler = function(event, a, b, c) {
305 equals( event.type, "click", "check passed data" );
306 equals( a, 1, "check passed data" );
307 equals( b, "2", "check passed data" );
308 equals( c, "abc", "check passed data" );
312 var $elem = jQuery("#firstp");
314 // Simulate a "native" click
315 $elem[0].click = function(){
316 ok( true, "Native call was triggered" );
319 // Triggers handlrs and native
321 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
323 // Simulate a "native" click
324 $elem[0].click = function(){
325 ok( false, "Native call was triggered" );
328 // Trigger only the handlers (no native)
330 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
334 jQuery('#form input:first').hide().trigger('focus');
338 ok( pass, "Trigger focus on hidden element" );
341 test("trigger(eventObject, [data], [fn])", function() {
344 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
345 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
347 var event = jQuery.Event("noNew");
348 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
349 equals( event.type, "noNew", "Verify its type" );
351 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
352 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
353 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
355 event.preventDefault();
356 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
357 event.stopPropagation();
358 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
360 event.isPropagationStopped = function(){ return false };
361 event.stopImmediatePropagation();
362 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
363 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
365 $parent.bind('foo',function(e){
367 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
368 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
369 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
370 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
373 // test with an event object
374 event = new jQuery.Event("foo");
375 event.secret = 'boo!';
376 $child.trigger(event);
378 // test with a literal object
379 $child.trigger({type:'foo', secret:'boo!'});
384 ok( false, "This assertion shouldn't be reached");
387 $parent.bind('foo', error );
389 $child.bind('foo',function(e, a, b, c ){
390 equals( arguments.length, 4, "Check arguments length");
391 equals( a, 1, "Check first custom argument");
392 equals( b, 2, "Check second custom argument");
393 equals( c, 3, "Check third custom argument");
395 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
396 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
397 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
400 e.stopImmediatePropagation();
405 // We should add this back in when we want to test the order
406 // in which event handlers are iterated.
407 //$child.bind('foo', error );
409 event = new jQuery.Event("foo");
410 $child.trigger( event, [1,2,3] ).unbind();
411 equals( event.result, "result", "Check event.result attribute");
413 // Will error if it bubbles
414 $child.triggerHandler('foo');
417 $parent.unbind().remove();
420 test("toggle(Function, Function, ...)", function() {
424 fn1 = function(e) { count++; },
425 fn2 = function(e) { count--; },
426 preventDefault = function(e) { e.preventDefault() },
427 link = jQuery('#mark');
428 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
429 equals( count, 1, "Check for toggle(fn, fn)" );
431 jQuery("#firstp").toggle(function () {
432 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
433 }, function() {}).trigger("click", [ 1, 2, 3 ]);
436 jQuery("#simon1").one("click", function() {
437 ok( true, "Execute event only once" );
438 jQuery(this).toggle(function() {
439 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
441 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
444 }).click().click().click();
459 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
461 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
463 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
465 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
467 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
469 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
471 $div.unbind('click',fns[0]);
472 var data = jQuery.data( $div[0], 'events' );
473 ok( !data, "Unbinding one function from toggle unbinds them all");
476 test(".live()/.die()", function() {
479 var submit = 0, div = 0, livea = 0, liveb = 0;
481 jQuery("div").live("submit", function(){ submit++; return false; });
482 jQuery("div").live("click", function(){ div++; });
483 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
484 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
486 // Nothing should trigger on the body
487 jQuery("body").trigger("click");
488 equals( submit, 0, "Click on body" );
489 equals( div, 0, "Click on body" );
490 equals( livea, 0, "Click on body" );
491 equals( liveb, 0, "Click on body" );
493 // This should trigger two events
494 jQuery("div#nothiddendiv").trigger("click");
495 equals( submit, 0, "Click on div" );
496 equals( div, 1, "Click on div" );
497 equals( livea, 1, "Click on div" );
498 equals( liveb, 0, "Click on div" );
500 // This should trigger three events (w/ bubbling)
501 jQuery("div#nothiddendivchild").trigger("click");
502 equals( submit, 0, "Click on inner div" );
503 equals( div, 2, "Click on inner div" );
504 equals( livea, 2, "Click on inner div" );
505 equals( liveb, 1, "Click on inner div" );
507 // This should trigger one submit
508 jQuery("div#nothiddendivchild").trigger("submit");
509 equals( submit, 1, "Submit on div" );
510 equals( div, 2, "Submit on div" );
511 equals( livea, 2, "Submit on div" );
512 equals( liveb, 1, "Submit on div" );
514 // Make sure no other events were removed in the process
515 jQuery("div#nothiddendivchild").trigger("click");
516 equals( submit, 1, "die Click on inner div" );
517 equals( div, 3, "die Click on inner div" );
518 equals( livea, 3, "die Click on inner div" );
519 equals( liveb, 2, "die Click on inner div" );
521 // Now make sure that the removal works
522 jQuery("div#nothiddendivchild").die("click");
523 jQuery("div#nothiddendivchild").trigger("click");
524 equals( submit, 1, "die Click on inner div" );
525 equals( div, 4, "die Click on inner div" );
526 equals( livea, 4, "die Click on inner div" );
527 equals( liveb, 2, "die Click on inner div" );
529 // Make sure that the click wasn't removed too early
530 jQuery("div#nothiddendiv").trigger("click");
531 equals( submit, 1, "die Click on inner div" );
532 equals( div, 5, "die Click on inner div" );
533 equals( livea, 5, "die Click on inner div" );
534 equals( liveb, 2, "die Click on inner div" );
536 jQuery("div#nothiddendiv").die("click");
537 jQuery("div").die("click");
538 jQuery("div").die("submit");
540 // Verify that return false prevents default action
541 jQuery("#anchor2").live("click", function(){ return false; });
542 var hash = window.location.hash;
543 jQuery("#anchor2").trigger("click");
544 equals( window.location.hash, hash, "return false worked" );
545 jQuery("#anchor2").die("click");
547 // Verify that .preventDefault() prevents default action
548 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
549 var hash = window.location.hash;
550 jQuery("#anchor2").trigger("click");
551 equals( window.location.hash, hash, "e.preventDefault() worked" );
552 jQuery("#anchor2").die("click");
554 // Test binding the same handler to multiple points
556 function callback(){ called++; return false; }
558 jQuery("#nothiddendiv").live("click", callback);
559 jQuery("#anchor2").live("click", callback);
561 jQuery("#nothiddendiv").trigger("click");
562 equals( called, 1, "Verify that only one click occurred." );
564 jQuery("#anchor2").trigger("click");
565 equals( called, 2, "Verify that only one click occurred." );
567 // Make sure that only one callback is removed
568 jQuery("#anchor2").die("click", callback);
570 jQuery("#nothiddendiv").trigger("click");
571 equals( called, 3, "Verify that only one click occurred." );
573 jQuery("#anchor2").trigger("click");
574 equals( called, 3, "Verify that no click occurred." );
576 // Make sure that it still works if the selector is the same,
577 // but the event type is different
578 jQuery("#nothiddendiv").live("foo", callback);
581 jQuery("#nothiddendiv").die("click", callback);
583 jQuery("#nothiddendiv").trigger("click");
584 equals( called, 3, "Verify that no click occurred." );
586 jQuery("#nothiddendiv").trigger("foo");
587 equals( called, 4, "Verify that one foo occurred." );
590 jQuery("#nothiddendiv").die("foo", callback);
592 // Make sure we don't loose the target by DOM modifications
593 // after the bubble already reached the liveHandler
594 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
596 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
597 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
599 jQuery("#nothiddendiv span").click();
600 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
601 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
604 jQuery("#nothiddendivchild").die("click");
608 test("jQuery(function($) {})", function() {
611 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");
616 test("event properties", function() {
618 jQuery("#simon1").click(function(event) {
619 ok( event.timeStamp, "assert event.timeStamp is present" );