Closed Bug 171840 Opened 22 years ago Closed 22 years ago

Make setAttribute('disabled', true|false) work expectedly

Categories

(Core :: Layout: Form Controls, defect)

x86
Linux
defect
Not set
normal

Tracking

()

VERIFIED INVALID

People

(Reporter: khwbugzilla, Assigned: john)

Details

Attachments

(1 file)

setAttribute() and getAttribute() do not work properly, at least for the form
input field properties of readonly and disabled.  If you disable a field, it can
never be enabled again; same for readonly.  I noticed this 0.94, but assumed it
would be fixed by 1.0; and am astonished that it hasn't apparently been reported.
getAttribute() returns the wrong values.

Here's code for a problem reproduction:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<title>setAttribute test</title>
</head>
<body>
<form name="foi" id="foi" method="Post">
<input type="checkbox" name="experiencer" value="1"
onclick="form.elements.submit.setAttribute('disabled', this.checked);
if (this.checked) alert ('setting disabled');
else alert('setting enabled');
if (form.elements.submit.getAttribute('disabled')) alert ('getAttribute()
returned: disabled');
else alert('getAttribute() returned: enabled');
return true;
">
<input id="submit" type="submit" value="Submit" > 
</form>
</body>
</html>
Attached file Reporter's testcase
Confirming bug with Mozilla trunk build 20020930xx on WinNT.

STEPS TO REPRODUCE
1.  Load the testcase
2.  Click on the checkbox
3.  IE6 alerts 'getAttribute() returned: disabled'
4.  Sure enough, the Submit button is disabled
5.  Moz alerts 'getAttribute() returned: enabled'
6.  However, the Submit button is disabled, as in IE6!
7.  Click on the checkbox again
8.  IE6 alerts 'getAttribute() returned: enabled'
9.  Sure enough, the Submit button is now enabled again
10. Moz alerts 'getAttribute() returned: enabled'
11. However, the Submit button remains disabled!


Browser, not engine ---> HTML Form Controls
Assignee: rogerl → jkeiser
Status: UNCONFIRMED → NEW
Component: JavaScript Engine → HTML Form Controls
Ever confirmed: true
QA Contact: pschwartau → tpreston
Ya, looks like a real bug.  Note that you can work around it by setting
form.elements.submit.disabled = this.checked instead of using the attribute.
Status: NEW → ASSIGNED
The problem here is that setAttribute('disabled', false) does not remove the
disabled attribute; it sets the disabled attribute to the string value "false".
 This is how it is *supposed* to work, as defined by the specs:.

To fix it, instead of toggling the value, use this:
if (this.checked) {
  form.submit.setAttribute('disabled', 'disabled');
} else {
  form.submit.removeAttribute('disabled');
}

As a side note, be aware that naming your buttons "submit" can cause
problems--specifically when you call the submit() method it will not do what you
want, because the "submit" button is not a function.

Our current behavior is compliant with the HTML / DOM specs, and it's not clear
there is sufficient web content using setAttribute('X', false) to warrant making
a change.  There probably isn't.
Status: ASSIGNED → RESOLVED
Closed: 22 years ago
Resolution: --- → INVALID
Summary: bugs in set(and get)Attribute() with disabled and readonly → Make setAttribute('disabled', true|false) work expectedly
http://www.w3.org/TR/html401/interact/forms.html#h-17.12

disabled [CI]
    When set for a form control, this boolean attribute disables the control for
user input.

If you keep doing setAttribute(), you keep setting it, and as the spec says, you
keep disabling it.  Since you're working with HTML, why not just use the DOM
HTML APIs?  Instead of doing setAttribute/removeAttribute, you can simply use

  .disabled = true;
  .disabled = false;

This will work as you are expecting it, and is perfectly valid per the specs.
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: