File tree Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Original file line number Diff line number Diff line change 274
274
275
275
/**
276
276
* Element.toggle(@element[, bool]) -> Element
277
+ * - bool (Boolean): Whether the element should be shown or hidden. If not
278
+ a boolean, this argument will be ignored.
277
279
*
278
280
* Toggles the CSS `display` of `element`. Returns `element`.
279
281
*
284
286
* current state, but will use the `bool` argument instead if it's
285
287
* provided (`true` to show the element, `false` to hide it).
286
288
*
289
+ * If the `bool` argument is not a boolean, **it will be ignored**. This
290
+ * preserves the ability to toggle elements through comparisons (e.g.,
291
+ * `errorElement.toggle(errors > 0)`) while also letting a user do
292
+ * `someElements.each(Element.toggle)` without falling victim to
293
+ * JavaScript's famous [problems with variadic arguments](http://www.wirfs-brock.com/allen/posts/166).
294
+ *
295
+ *
287
296
* ##### Examples
288
297
*
289
298
* <div id="welcome-message">Welcome</div>
335
344
**/
336
345
function toggle ( element , bool ) {
337
346
element = $ ( element ) ;
338
- if ( Object . isUndefined ( bool ) )
347
+ if ( typeof bool !== 'boolean' )
339
348
bool = ! Element . visible ( element ) ;
340
349
Element [ bool ? 'show' : 'hide' ] ( element ) ;
341
350
Original file line number Diff line number Diff line change @@ -305,6 +305,10 @@ new Test.Unit.Runner({
305
305
this . assert ( $ ( 'test-toggle-hidden' ) . visible ( ) , 'test-toggle-hidden 1' ) ;
306
306
$ ( 'test-toggle-hidden' ) . toggle ( ) ;
307
307
this . assert ( ! $ ( 'test-toggle-hidden' ) . visible ( ) , 'test-toggle-hidden 2' ) ;
308
+
309
+ $ ( 'test-toggle-hidden' , 'test-toggle-visible' ) . each ( Element . toggle ) ;
310
+ this . assert ( ! $ ( 'test-toggle-visible' ) . visible ( ) , 'test-toggle-visible-3' ) ;
311
+ this . assert ( $ ( 'test-toggle-hidden' ) . visible ( ) , 'test-toggle-hidden-3' ) ;
308
312
} ,
309
313
310
314
testElementShow : function ( ) {
You can’t perform that action at this time.
0 commit comments