3 test("bind(), with data", function() {
5 var handler = function(event) {
6 ok( event.data, "bind() with data, check passed data exists" );
7 ok( 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 ok( event.data.foo == "bar", "Check value of passed data" );
19 ok( data, "Check trigger data" );
20 ok( 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 ok( clickCounter == 1, "bind() with multiple events at once" );
37 ok( 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("click",function(e){
76 ok(true, "Normal click triggered");
79 $("#firstp").bind("click.test",function(e){
80 ok(true, "Namespaced click triggered");
83 // Trigger both bound fn (2)
84 $("#firstp").trigger("click");
86 // Trigger one bound fn (1)
87 $("#firstp").trigger("click.test");
89 // Remove only the one fn
90 $("#firstp").unbind("click.test");
92 // Trigger the remaining fn (1)
93 $("#firstp").trigger("click");
95 // using contents will get comments regular, text, and comment nodes
96 $("#nonnodes").contents().bind("tester", function () {
97 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
100 // Make sure events stick with appendTo'd elements (which are cloned) #2027
101 $("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
102 ok( $("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
105 test("click()", function() {
107 $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
108 var close = $('spanx', this); // same with $(this).find('span');
109 ok( close.length == 0, "Context element does not exist, length must be zero" );
110 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
114 $("#check1").click(function() {
115 ok( true, "click event handler for checkbox gets fired twice, see #815" );
119 $('#firstp')[0].onclick = function(event) {
122 $('#firstp').click();
123 ok( counter == 1, "Check that click, triggers onclick event handler also" );
126 test("unbind(event)", function() {
128 var el = $("#firstp");
129 el.click(function() {
130 ok( true, "Fake normal bind" );
132 el.click(function(event) {
134 ok( true, "Fake onebind" );
138 el.click(function() { return; });
140 ok( !el[0].onclick, "Handler is removed" ); // Bug #964
142 el.click(function() { return; });
143 el.unbind('change',function(){ return; });
144 for (var ret in jQuery.data(el[0], "events")['click']) break;
145 ok( ret, "Extra handlers weren't accidentally removed." );
148 ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
151 var clickCounter = mouseoverCounter = 0;
152 var handler = function(event) {
153 if (event.type == "click")
155 else if (event.type == "mouseover")
156 mouseoverCounter += 1;
158 $("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
159 ok( clickCounter == 0, "unbind() with multiple events at once" );
160 ok( mouseoverCounter == 0, "unbind() with multiple events at once" );
163 test("trigger(event, [data], [fn])", function() {
166 var handler = function(event, a, b, c) {
167 equals( event.type, "click", "check passed data" );
168 equals( a, 1, "check passed data" );
169 equals( b, "2", "check passed data" );
170 equals( c, "abc", "check passed data" );
174 var handler2 = function(a, b, c) {
175 equals( a, 1, "check passed data" );
176 equals( b, "2", "check passed data" );
177 equals( c, "abc", "check passed data" );
181 var handler3 = function(a, b, c, v) {
182 equals( a, 1, "check passed data" );
183 equals( b, "2", "check passed data" );
184 equals( c, "abc", "check passed data" );
185 equals( v, "test", "check current value" );
189 var handler4 = function(a, b, c, v) {
190 equals( a, 1, "check passed data" );
191 equals( b, "2", "check passed data" );
192 equals( c, "abc", "check passed data" );
193 equals( v, "test", "check current value" );
196 // Simulate a "native" click
197 $("#firstp")[0].click = function(){
198 ok( true, "Native call was triggered" );
201 // Triggers handlrs and native
203 $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
205 // Triggers handlers, native, and extra fn
207 $("#firstp").trigger("click", [1, "2", "abc"], handler4);
209 // Simulate a "native" click
210 $("#firstp")[0].click = function(){
211 ok( false, "Native call was triggered" );
214 // Triggers handlers, native, and extra fn
216 $("#firstp").trigger("click", [1, "2", "abc"], handler2);
218 // Trigger only the handlers (no native)
220 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
222 // Trigger only the handlers (no native) and extra fn
224 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
226 // Build fake click event to pass in
227 var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
229 // Trigger only the handlers (no native), with external event obj
231 equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
233 // Trigger only the handlers (no native) and extra fn, with external event obj
235 eventObj = jQuery.event.fix({ type: "foo", target: document.body });
236 equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
246 ok( pass, "Trigger focus on hidden element" );
248 // have the extra handler override the return
250 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
252 // have the extra handler leave the return value alone
254 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
257 test("toggle(Function, Function)", function() {
260 fn1 = function(e) { count++; },
261 fn2 = function(e) { count--; },
262 preventDefault = function(e) { e.preventDefault() },
264 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
265 ok( count == 1, "Check for toggle(fn, fn)" );
267 $("#firstp").toggle(function () {
268 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
269 }, function() {}).trigger("click", [ 1, 2, 3 ]);
272 $("#simon1").one("click", function() {
273 ok( true, "Execute event only once" );
274 $(this).toggle(function() {
275 ok( first++ == 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
277 ok( first == 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
280 }).click().click().click();
283 test("jQuery(function($) {})", function() {
286 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");