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 274274
275275 /**
276276 * 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.
277279 *
278280 * Toggles the CSS `display` of `element`. Returns `element`.
279281 *
284286 * current state, but will use the `bool` argument instead if it's
285287 * provided (`true` to show the element, `false` to hide it).
286288 *
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+ *
287296 * ##### Examples
288297 *
289298 * <div id="welcome-message">Welcome</div>
335344 **/
336345 function toggle ( element , bool ) {
337346 element = $ ( element ) ;
338- if ( Object . isUndefined ( bool ) )
347+ if ( typeof bool !== 'boolean' )
339348 bool = ! Element . visible ( element ) ;
340349 Element [ bool ? 'show' : 'hide' ] ( element ) ;
341350
Original file line number Diff line number Diff line change @@ -305,6 +305,10 @@ new Test.Unit.Runner({
305305 this . assert ( $ ( 'test-toggle-hidden' ) . visible ( ) , 'test-toggle-hidden 1' ) ;
306306 $ ( 'test-toggle-hidden' ) . toggle ( ) ;
307307 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' ) ;
308312 } ,
309313
310314 testElementShow : function ( ) {
You can’t perform that action at this time.
0 commit comments