Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why attribute "disabled" but not "enabled"

Tags:

html

I'm always feel the disabled attribute for HTML <input> and all is twisting my brain. Why choose a negated attribute name, isn't enabled more intuitive?

Just compare:

enabled=advancedUser
disabled=not(advancedUser)

enabled=not(locked)
disabled=locked

enabled=advancedUser and not(locked)
disabled=not(advancedUser) or locked
disabled=not(advancedUser and not(locked))
like image 497
Xiè Jìléi Avatar asked Oct 10 '11 03:10

Xiè Jìléi


People also ask

How do I remove disabled attributes?

To remove the disabled attribute, select the element and call the removeAttribute() method on it, passing it disabled as a parameter, e.g. btn. removeAttribute('disabled') . The removeAttribute method will remove the disabled attribute from the element.

Why is disabled false not working?

But if we double quote the false (I mean, "false") then it doesn't work. Then don't double-quote it. In HTML boolean attributes such as disabled and readonly can only legally take the name of the attribute. Most browsers however accept any value for the attribute as being in the affimative.

How do you set a disabled attribute?

To set the disabled attribute, select the element and call the setAttribute() method on it, passing it disabled as the first parameter, e.g. button. setAttribute('disabled', '') . The setAttribute method will add the disabled attribute to the element.

How do I enable a disabled HTML element?

An element can be disabled in HTML by setting disable property to true and enabled again by setting disabled=false.


1 Answers

The fundamental reason behind this is that it was a later addition to the HTML input fields, and needed to be that way to maintain backward compatibility with existing web pages.

When the <input> tag was originally defined, its functionality was extremely limited. It did not have disabled or readonly attributes, nor many of the other properties we take for granted today.

These were all added later, but by the time they were added, many web sites were already using <input> fields, so the ability to disable it had to work without affecting existing code that didn't use it. Therefore the default state had to be enabled.

It also had to be a boolean flag, which is why it is disabled rather than enabled=true. The latter would have been a key-value pair attribute. This wouldn't have been a good choice.

Consider the following:

enabled=false
enabled=0
enabled=FALSE
enabled=no
enabled=disabled
enabled=flase
etc...

The browser would have had to be able to cope with a huge number of possible values. Making it a boolean flag simplifies things enormously. It makes the spec easier to understand, both for the web site developer and the browser developer.

The other thing to bear in mind is that the time when this property was added to HTML was in the middle of the so-called 'browser wars'. Many features were being added to the competing web browsers, in a hurry and without the benefit of formal specs, and many features were added which we can indeed look back on and wish it were slightly different.

I don't believe this is one of those features: the disabled flag is perfectly logical really if you stop and think about it. But it's quite possible that it may have been better designed if the browser developers had been co-operating a bit more back then.

But whatever the case, the situation today is that this is what we have. The HTML spec may be evolving, but existing features such as this are not going to change now.

like image 116
Spudley Avatar answered Sep 29 '22 21:09

Spudley