3 test("bind()", function() {
6 var handler = function(event) {
7 ok( event.data, "bind() with data, check passed data exists" );
8 ok( event.data.foo == "bar", "bind() with data, Check value of passed data" );
10 $("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
12 ok( !jQuery.data($("#firstp")[0], "events"), "Event handler unbound when using data." );
15 var handler = function(event, data) {
16 ok( event.data, "check passed data exists" );
17 ok( event.data.foo == "bar", "Check value of passed data" );
18 ok( data, "Check trigger data" );
19 ok( data.bar == "foo", "Check value of trigger data" );
21 $("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
24 var clickCounter = mouseoverCounter = 0;
25 var handler = function(event) {
26 if (event.type == "click")
28 else if (event.type == "mouseover")
29 mouseoverCounter += 1;
31 $("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
32 ok( clickCounter == 1, "bind() with multiple events at once" );
33 ok( mouseoverCounter == 1, "bind() with multiple events at once" );
37 var handler = function(event) {
38 ok ( !event.data, "Check that no data is added to the event object" );
40 $("#firstp").bind("click", handler).trigger("click");
43 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
44 // var doc = document.getElementById("iframe").contentDocument;
46 // doc.body.innerHTML = "<input type='text'/>";
48 // var input = doc.getElementsByTagName("input")[0];
50 // $(input).bind("click",function() {
51 // ok( true, "Binding to element inside iframe" );
55 function selectOnChange(event) {
56 equals( event.data, counter++, "Event.data is not a global event object" );
58 $("#form select").each(function(i){
59 $(this).bind('change', i, selectOnChange);
64 $("#firstp").bind("click",function(e){
65 ok(true, "Normal click triggered");
68 $("#firstp").bind("click.test",function(e){
69 ok(true, "Namespaced click triggered");
72 // Trigger both bound fn (2)
73 $("#firstp").trigger("click");
75 // Trigger one bound fn (1)
76 $("#firstp").trigger("click.test");
78 // Remove only the one fn
79 $("#firstp").unbind("click.test");
81 // Trigger the remaining fn (1)
82 $("#firstp").trigger("click");
84 // using contents will get comments regular, text, and comment nodes
85 $("#nonnodes").contents().bind("tester", function () {
86 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
90 test("click()", function() {
92 $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
93 var close = $('spanx', this); // same with $(this).find('span');
94 ok( close.length == 0, "Context element does not exist, length must be zero" );
95 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
99 $("#check1").click(function() {
100 ok( true, "click event handler for checkbox gets fired twice, see #815" );
104 $('#firstp')[0].onclick = function(event) {
107 $('#firstp').click();
108 ok( counter == 1, "Check that click, triggers onclick event handler also" );
111 test("unbind(event)", function() {
113 var el = $("#firstp");
114 el.click(function() {
115 ok( true, "Fake normal bind" );
117 el.click(function(event) {
119 ok( true, "Fake onebind" );
123 el.click(function() { return; });
125 ok( !el[0].onclick, "Handler is removed" ); // Bug #964
127 el.click(function() { return; });
128 el.unbind('change',function(){ return; });
129 for (var ret in jQuery.data(el[0], "events")['click']) break;
130 ok( ret, "Extra handlers weren't accidentally removed." );
133 ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
136 var clickCounter = mouseoverCounter = 0;
137 var handler = function(event) {
138 if (event.type == "click")
140 else if (event.type == "mouseover")
141 mouseoverCounter += 1;
143 $("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
144 ok( clickCounter == 0, "unbind() with multiple events at once" );
145 ok( mouseoverCounter == 0, "unbind() with multiple events at once" );
148 test("trigger(event, [data], [fn])", function() {
151 var handler = function(event, a, b, c) {
152 equals( event.type, "click", "check passed data" );
153 equals( a, 1, "check passed data" );
154 equals( b, "2", "check passed data" );
155 equals( c, "abc", "check passed data" );
159 var handler2 = function(a, b, c) {
160 equals( a, 1, "check passed data" );
161 equals( b, "2", "check passed data" );
162 equals( c, "abc", "check passed data" );
166 var handler3 = function(a, b, c, v) {
167 equals( a, 1, "check passed data" );
168 equals( b, "2", "check passed data" );
169 equals( c, "abc", "check passed data" );
170 equals( v, "test", "check current value" );
174 var handler4 = function(a, b, c, v) {
175 equals( a, 1, "check passed data" );
176 equals( b, "2", "check passed data" );
177 equals( c, "abc", "check passed data" );
178 equals( v, "test", "check current value" );
181 // Simulate a "native" click
182 $("#firstp")[0].click = function(){
183 ok( true, "Native call was triggered" );
186 // Triggers handlrs and native
188 $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
190 // Triggers handlers, native, and extra fn
192 $("#firstp").trigger("click", [1, "2", "abc"], handler4);
194 // Simulate a "native" click
195 $("#firstp")[0].click = function(){
196 ok( false, "Native call was triggered" );
199 // Triggers handlers, native, and extra fn
201 $("#firstp").trigger("click", [1, "2", "abc"], handler2);
203 // Trigger only the handlers (no native)
205 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
207 // Trigger only the handlers (no native) and extra fn
209 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
211 // Build fake click event to pass in
212 var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
214 // Trigger only the handlers (no native), with external event obj
216 equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
218 // Trigger only the handlers (no native) and extra fn, with external event obj
220 equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
230 ok( pass, "Trigger focus on hidden element" );
232 // have the extra handler override the return
234 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
236 // have the extra handler leave the return value alone
238 equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
241 test("toggle(Function, Function)", function() {
244 fn1 = function(e) { count++; },
245 fn2 = function(e) { count--; },
246 preventDefault = function(e) { e.preventDefault() },
248 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
249 ok( count == 1, "Check for toggle(fn, fn)" );
251 $("#firstp").toggle(function () {
252 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
253 }, function() {}).trigger("click", [ 1, 2, 3 ]);
256 $("#simon1").one("click", function() {
257 ok( true, "Execute event only once" );
258 $(this).toggle(function() {
259 ok( first++ == 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
261 ok( first == 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
264 }).click().click().click();
267 test("jQuery(function($) {})", function() {
270 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");