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 $("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
11 ok( !jQuery.data($("#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 $("#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 $("#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 $("#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 // $(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 $("#form select").each(function(i){
68 $(this).bind('change', i, selectOnChange);
72 test("bind(), namespaced events, cloned events", function() {
75 $("#firstp").bind("custom.test",function(e){
76 ok(true, "Custom event triggered");
79 $("#firstp").bind("click",function(e){
80 ok(true, "Normal click triggered");
83 $("#firstp").bind("click.test",function(e){
84 ok(true, "Namespaced click triggered");
87 // Trigger both bound fn (2)
88 $("#firstp").trigger("click");
90 // Trigger one bound fn (1)
91 $("#firstp").trigger("click.test");
93 // Remove only the one fn
94 $("#firstp").unbind("click.test");
96 // Trigger the remaining fn (1)
97 $("#firstp").trigger("click");
99 // Remove the remaining fn
100 $("#firstp").unbind(".test");
102 // Trigger the remaining fn (0)
103 $("#firstp").trigger("custom");
105 // using contents will get comments regular, text, and comment nodes
106 $("#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 $("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
112 ok( $("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
115 test("trigger() shortcuts", function() {
117 $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
118 var close = $('spanx', this); // same with $(this).find('span');
119 equals( close.length, 0, "Context element does not exist, length must be zero" );
120 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
124 $("#check1").click(function() {
125 ok( true, "click event handler for checkbox gets fired twice, see #815" );
129 $('#firstp')[0].onclick = function(event) {
132 $('#firstp').click();
133 equals( counter, 1, "Check that click, triggers onclick event handler also" );
135 var clickCounter = 0;
136 $('#simon1')[0].onclick = function(event) {
139 $('#simon1').click();
140 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
142 $('<img />').load(function(){
143 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
147 test("unbind(event)", function() {
149 var el = $("#firstp");
150 el.click(function() {
151 ok( true, "Fake normal bind" );
153 el.click(function(event) {
155 ok( true, "Fake onebind" );
159 el.click(function() { return; });
161 ok( !el[0].onclick, "Handler is removed" ); // Bug #964
163 el.click(function() { return; });
164 el.unbind('change',function(){ return; });
165 for (var ret in jQuery.data(el[0], "events")['click']) break;
166 ok( ret, "Extra handlers weren't accidentally removed." );
169 ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
172 var clickCounter = (mouseoverCounter = 0);
173 var handler = function(event) {
174 if (event.type == "click")
176 else if (event.type == "mouseover")
177 mouseoverCounter += 1;
179 $("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
180 equals( clickCounter, 0, "unbind() with multiple events at once" );
181 equals( mouseoverCounter, 0, "unbind() with multiple events at once" );
184 test("trigger(event, [data], [fn])", function() {
187 var handler = function(event, a, b, c) {
188 equals( event.type, "click", "check passed data" );
189 equals( a, 1, "check passed data" );
190 equals( b, "2", "check passed data" );
191 equals( c, "abc", "check passed data" );
195 var handler2 = function(a, b, c) {
196 equals( a, 1, "check passed data" );
197 equals( b, "2", "check passed data" );
198 equals( c, "abc", "check passed data" );
202 var handler3 = function(a, b, c, v) {
203 equals( a, 1, "check passed data" );
204 equals( b, "2", "check passed data" );
205 equals( c, "abc", "check passed data" );
206 equals( v, "test", "check current value" );
210 var handler4 = function(a, b, c, v) {
211 equals( a, 1, "check passed data" );
212 equals( b, "2", "check passed data" );
213 equals( c, "abc", "check passed data" );
214 equals( v, "test", "check current value" );
217 // Simulate a "native" click
218 $("#firstp")[0].click = function(){
219 ok( true, "Native call was triggered" );
222 // Triggers handlrs and native
224 $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
226 // Triggers handlers, native, and extra fn
228 $("#firstp").trigger("click", [1, "2", "abc"], handler4);
230 // Simulate a "native" click
231 $("#firstp")[0].click = function(){
232 ok( false, "Native call was triggered" );
235 // Triggers handlers, native, and extra fn
237 $("#firstp").trigger("click", [1, "2", "abc"], handler2);
239 // Trigger only the handlers (no native)
241 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
243 // Trigger only the handlers (no native) and extra fn
245 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
247 // Build fake click event to pass in
248 var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
250 // Trigger only the handlers (no native), with external event obj
252 equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
254 // Trigger only the handlers (no native) and extra fn, with external event obj
256 eventObj = jQuery.event.fix({ type: "foo", target: document.body });
257 equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
267 ok( pass, "Trigger focus on hidden element" );
269 // have the extra handler override the return
271 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
273 // have the extra handler leave the return value alone
275 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
278 test("toggle(Function, Function, ...)", function() {
282 fn1 = function(e) { count++; },
283 fn2 = function(e) { count--; },
284 preventDefault = function(e) { e.preventDefault() },
286 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
287 equals( count, 1, "Check for toggle(fn, fn)" );
289 $("#firstp").toggle(function () {
290 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
291 }, function() {}).trigger("click", [ 1, 2, 3 ]);
294 $("#simon1").one("click", function() {
295 ok( true, "Execute event only once" );
296 $(this).toggle(function() {
297 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
299 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
302 }).click().click().click();
317 var $div = $("<div> </div>").toggle( fns[0], fns[1], fns[2] );
319 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
321 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
323 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
325 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
327 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
329 $div.unbind('click',fns[0]);
330 var data = $.data( $div[0], 'events' );
331 ok( !data, "Unbinding one function from toggle unbinds them all");
334 test("jQuery(function($) {})", function() {
337 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");
342 test("event properties", function() {
344 $("#simon1").click(function(event) {
345 ok( event.timeStamp, "assert event.timeStamp is present" );