Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#15221 closed bug (notabug)

Button disabled when initialized with falsy 0

Reported by: heinrichmartin Owned by:
Priority: minor Milestone: none
Component: ui.core Version: 1.12.1
Keywords: Cc:
Blocked by: Blocking:

Description

When creating a button with disabled=0 then ui-state-disabled is applied.

Minimum Example

<html>
<!--
minimum example of jquery-ui bug:
"disabled" to be strict boolean for css class toggling
-->
<head>
<title>Button</title>
<link rel="stylesheet" type="text/css" href="jquery-ui-1.12.1/jquery-ui.css"></link>
</head>
<body>
<h1>Note that ui-state-disabled and ui-button-disabled are out-of-sync on the first button</h1>
<button>I am enabled</button>
<button>Me too</button>
<button>I am disabled</button>
<button>Me too</button>
<script src="jquery-3.2.1.js" type="text/javascript"></script>
<script src="jquery-ui-1.12.1/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
   $('button').eq(0).button({
      disabled: 0
   }).end().eq(1).button({
      disabled: false
   }).end().eq(2).button({
      disabled: 42
   }).end().eq(3).button({
      disabled: true
   });
})
</script>
</body>
</html>

Analysis

jquery-ui.js has three occurrences of calls to _toggleClass with the value of the disabled option being forwarded as-is. Some other occurrences already have the conversion to boolean included.

_toggleClass however uses this parameter for animation when it is not strictly boolean.

I have no build environment for jquery-ui, but I fixed it directly by adding the conversion to boolean "!!".

Workaround

Set option "disabled" to strict boolean.

Change History (2)

comment:1 Changed 5 years ago by Scott González

Resolution: notabug
Status: newclosed

Set option "disabled" to strict boolean.

Not so much a workaround as the only supported type of value: http://api.jqueryui.com/button/#option-disabled

comment:2 in reply to:  1 Changed 5 years ago by heinrichmartin

Replying to Scott González:

Not so much a workaround as the only supported type of value: http://api.jqueryui.com/button/#option-disabled

Well, then why does $('button').eq(2).button('option','disabled') return 42?

Imho a mature and reliable library should be either tolerant or fail fast.

Note: See TracTickets for help on using tickets.