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("trigger() shortcuts", function() {
171 jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
172 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
173 equals( close.length, 0, "Context element does not exist, length must be zero" );
174 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
178 jQuery("#check1").click(function() {
179 ok( true, "click event handler for checkbox gets fired twice, see #815" );
183 jQuery('#firstp')[0].onclick = function(event) {
186 jQuery('#firstp').click();
187 equals( counter, 1, "Check that click, triggers onclick event handler also" );
189 var clickCounter = 0;
190 jQuery('#simon1')[0].onclick = function(event) {
193 jQuery('#simon1').click();
194 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
196 jQuery('<img />').load(function(){
197 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
201 test("trigger() bubbling", function() {
204 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
206 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
207 jQuery("html").bind("click", function(e){ html++; });
208 jQuery("body").bind("click", function(e){ body++; });
209 jQuery("#main").bind("click", function(e){ main++; });
210 jQuery("#ap").bind("click", function(){ ap++; return false; });
212 jQuery("html").trigger("click");
213 equals( doc, 1, "HTML bubble" );
214 equals( html, 1, "HTML bubble" );
216 jQuery("body").trigger("click");
217 equals( doc, 2, "Body bubble" );
218 equals( html, 2, "Body bubble" );
219 equals( body, 1, "Body bubble" );
221 jQuery("#main").trigger("click");
222 equals( doc, 3, "Main bubble" );
223 equals( html, 3, "Main bubble" );
224 equals( body, 2, "Main bubble" );
225 equals( main, 1, "Main bubble" );
227 jQuery("#ap").trigger("click");
228 equals( doc, 3, "ap bubble" );
229 equals( html, 3, "ap bubble" );
230 equals( body, 2, "ap bubble" );
231 equals( main, 1, "ap bubble" );
232 equals( ap, 1, "ap bubble" );
235 test("unbind(event)", function() {
237 var el = jQuery("#firstp");
238 el.click(function() {
239 ok( true, "Fake normal bind" );
241 el.click(function(event) {
243 ok( true, "Fake onebind" );
247 el.click(function() { return; });
249 ok( !el[0].onclick, "Handler is removed" ); // Bug #964
251 el.click(function() { return; });
252 el.unbind('change',function(){ return; });
253 for (var ret in jQuery.data(el[0], "events")['click']) break;
254 ok( ret, "Extra handlers weren't accidentally removed." );
257 ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
260 var clickCounter = (mouseoverCounter = 0);
261 var handler = function(event) {
262 if (event.type == "click")
264 else if (event.type == "mouseover")
265 mouseoverCounter += 1;
267 jQuery("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
268 equals( clickCounter, 0, "unbind() with multiple events at once" );
269 equals( mouseoverCounter, 0, "unbind() with multiple events at once" );
272 test("trigger(type, [data], [fn])", function() {
275 var handler = function(event, a, b, c) {
276 equals( event.type, "click", "check passed data" );
277 equals( a, 1, "check passed data" );
278 equals( b, "2", "check passed data" );
279 equals( c, "abc", "check passed data" );
283 var $elem = jQuery("#firstp");
285 // Simulate a "native" click
286 $elem[0].click = function(){
287 ok( true, "Native call was triggered" );
290 // Triggers handlrs and native
292 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
294 // Simulate a "native" click
295 $elem[0].click = function(){
296 ok( false, "Native call was triggered" );
299 // Trigger only the handlers (no native)
301 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
305 jQuery('#form input:first').hide().trigger('focus');
309 ok( pass, "Trigger focus on hidden element" );
312 test("trigger(eventObject, [data], [fn])", function() {
315 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
316 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
318 var event = jQuery.Event("noNew");
319 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
320 equals( event.type, "noNew", "Verify its type" );
322 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
323 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
324 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
326 event.preventDefault();
327 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
328 event.stopPropagation();
329 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
331 event.isPropagationStopped = function(){ return false };
332 event.stopImmediatePropagation();
333 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
334 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
336 $parent.bind('foo',function(e){
338 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
339 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
340 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
341 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
344 // test with an event object
345 event = new jQuery.Event("foo");
346 event.secret = 'boo!';
347 $child.trigger(event);
349 // test with a literal object
350 $child.trigger({type:'foo', secret:'boo!'});
355 ok( false, "This assertion shouldn't be reached");
358 $parent.bind('foo', error );
360 $child.bind('foo',function(e, a, b, c ){
361 equals( arguments.length, 4, "Check arguments length");
362 equals( a, 1, "Check first custom argument");
363 equals( b, 2, "Check second custom argument");
364 equals( c, 3, "Check third custom argument");
366 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
367 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
368 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
371 e.stopImmediatePropagation();
376 $child.bind('foo', error );
378 event = new jQuery.Event("foo");
379 $child.trigger( event, [1,2,3] ).unbind();
380 equals( event.result, "result", "Check event.result attribute");
382 // Will error if it bubbles
383 $child.triggerHandler('foo');
386 $parent.unbind().remove();
389 test("toggle(Function, Function, ...)", function() {
393 fn1 = function(e) { count++; },
394 fn2 = function(e) { count--; },
395 preventDefault = function(e) { e.preventDefault() },
396 link = jQuery('#mark');
397 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
398 equals( count, 1, "Check for toggle(fn, fn)" );
400 jQuery("#firstp").toggle(function () {
401 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
402 }, function() {}).trigger("click", [ 1, 2, 3 ]);
405 jQuery("#simon1").one("click", function() {
406 ok( true, "Execute event only once" );
407 jQuery(this).toggle(function() {
408 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
410 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
413 }).click().click().click();
428 var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] );
430 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
432 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
434 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
436 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
438 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
440 $div.unbind('click',fns[0]);
441 var data = jQuery.data( $div[0], 'events' );
442 ok( !data, "Unbinding one function from toggle unbinds them all");
445 test(".live()/.die()", function() {
448 var submit = 0, div = 0, livea = 0, liveb = 0;
450 jQuery("div").live("submit", function(){ submit++; return false; });
451 jQuery("div").live("click", function(){ div++; });
452 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
453 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
455 // Nothing should trigger on the body
456 jQuery("body").trigger("click");
457 equals( submit, 0, "Click on body" );
458 equals( div, 0, "Click on body" );
459 equals( livea, 0, "Click on body" );
460 equals( liveb, 0, "Click on body" );
462 // This should trigger two events
463 jQuery("div#nothiddendiv").trigger("click");
464 equals( submit, 0, "Click on div" );
465 equals( div, 1, "Click on div" );
466 equals( livea, 1, "Click on div" );
467 equals( liveb, 0, "Click on div" );
469 // This should trigger three events (w/ bubbling)
470 jQuery("div#nothiddendivchild").trigger("click");
471 equals( submit, 0, "Click on inner div" );
472 equals( div, 2, "Click on inner div" );
473 equals( livea, 2, "Click on inner div" );
474 equals( liveb, 1, "Click on inner div" );
476 // This should trigger one submit
477 jQuery("div#nothiddendivchild").trigger("submit");
478 equals( submit, 1, "Submit on div" );
479 equals( div, 2, "Submit on div" );
480 equals( livea, 2, "Submit on div" );
481 equals( liveb, 1, "Submit on div" );
483 // Make sure no other events were removed in the process
484 jQuery("div#nothiddendivchild").trigger("click");
485 equals( submit, 1, "die Click on inner div" );
486 equals( div, 3, "die Click on inner div" );
487 equals( livea, 3, "die Click on inner div" );
488 equals( liveb, 2, "die Click on inner div" );
490 // Now make sure that the removal works
491 jQuery("div#nothiddendivchild").die("click");
492 jQuery("div#nothiddendivchild").trigger("click");
493 equals( submit, 1, "die Click on inner div" );
494 equals( div, 4, "die Click on inner div" );
495 equals( livea, 4, "die Click on inner div" );
496 equals( liveb, 2, "die Click on inner div" );
498 // Make sure that the click wasn't removed too early
499 jQuery("div#nothiddendiv").trigger("click");
500 equals( submit, 1, "die Click on inner div" );
501 equals( div, 5, "die Click on inner div" );
502 equals( livea, 5, "die Click on inner div" );
503 equals( liveb, 2, "die Click on inner div" );
505 jQuery("div#nothiddendiv").die("click");
506 jQuery("div").die("click");
507 jQuery("div").die("submit");
509 // Verify that return false prevents default action
510 jQuery("#anchor2").live("click", function(){ return false; });
511 var hash = window.location.hash;
512 jQuery("#anchor2").trigger("click");
513 equals( window.location.hash, hash, "return false worked" );
514 jQuery("#anchor2").die("click");
516 // Verify that .preventDefault() prevents default action
517 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
518 var hash = window.location.hash;
519 jQuery("#anchor2").trigger("click");
520 equals( window.location.hash, hash, "e.preventDefault() worked" );
521 jQuery("#anchor2").die("click");
525 test("jQuery(function($) {})", function() {
528 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");
533 test("event properties", function() {
535 jQuery("#simon1").click(function(event) {
536 ok( event.timeStamp, "assert event.timeStamp is present" );