- HTMLStyleElement: disabled property
- Value
- Examples
- Disabling an inline style
- HTML
- JavaScript
- Result
- Disabling a programmatically defined style
- HTML
- JavaScript
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- CSS disabled
- How Does disabled Done in CSS?
- Examples
- Example #1 – Field disabled
- Example #2 – Disabled Button
- Example #3 – Text Area disabled
- Conclusion
- Recommended Articles
HTMLStyleElement: disabled property
The HTMLStyleElement.disabled property can be used to get and set whether the stylesheet is disabled ( true ) or not ( false ).
Note that there is no corresponding disabled attribute on the HTML element.
Value
Returns true if the stylesheet is disabled, or there is no associated stylesheet; otherwise false . The value is false by default (if there is an associated stylesheet).
The property can be used to enable or disable an associated stylesheet. Setting the property to true when there is no associated stylesheet has no effect.
Examples
Disabling an inline style
This example demonstrates programmatically setting the disabled property on a style that was defined in the HTML using the HTML element. Note that you can also access any/all stylesheets in the document using Document.styleSheets .
HTML
button>Enablebutton> style id="InlineStyle"> p color: blue; > style> p>Text is black when style is disabled; blue when enabled.p> p>p>
JavaScript
The code below gets the style element using its id, and then sets it as disabled. As the style already exists, as it is defined in the SVG, this should succeed.
const style = document.getElementById("InlineStyle"); style.disabled = true;
We then add an event handler for the button that toggles the disabled value and button text.
const button = document.querySelector("button"); button.addEventListener("click", () => style.disabled = !style.disabled; const buttonText = style.disabled ? "Enable" : "Disable"; button.innerText = buttonText; >);
Result
The result is shown below. Press the button to toggle the disabled property value on the style used for the paragraph text.
Disabling a programmatically defined style
This example is very similar to the one above, except that the style is defined programmatically.
HTML
The HTML is similar to the previous case, but the definition does not include any default styling.
button>Enablebutton> p>Text is black when style is disabled; blue when enabled.p> p>p>
JavaScript
First we create the new style element on the HTML. This is done by first creating a style element using Document.createElement() , creating and appending a text node with the style definition, and then appending the style element to the document body.
// Create the `style` element const style = document.createElement("style"); const node = document.createTextNode("p < color: blue; >"); style.appendChild(node); document.body.appendChild(style);
We can then disable the style as shown below. Note that this is the earliest point at which setting the property to true will succeed. Before this point the document did not have an associated style, and so the value defaults to false .
//Disable the style style.disabled = true;
Last of all we add an event handler for the button that toggles the disabled state and button text (this is the same as in the previous example).
const button = document.querySelector("button"); button.addEventListener("click", () => style.disabled = !style.disabled; const buttonText = style.disabled ? "Enable" : "Disable"; button.innerText = buttonText; >);
Result
The result is shown below. Press the button to toggle the disabled state on the style used for the text.
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Apr 7, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
CSS disabled
The disabled is a selector in CSS, which is used to turn off the HTML elements. This disabled selector mainly works on form elements like text areas, buttons, checkboxes, drop-down boxes, etc.
Web development, programming languages, Software testing & others
Real-Time Example: Suppose we fill a form with all our credentials like name, mobile number, Employee number, etc. Once we have entered all details, it will ask for confirmation to submit them. Once we submit, not all the details will allow for updating. The employee number is disabled to update because it is unique. Automatically it is disabled. In this kind of situation, we have used disabled selectors in CSS.
How Does disabled Done in CSS?
As we discussed, disabled done on form pages in HTML. We can turn off the buttons, text fields, checkboxes, drop-down boxes, etc. by using the disabled selector on required HTML form elements.
input[type=text]:disabled < /*CSS declarations*/ > Country:
Examples
Example #1 – Field disabled
h1 < color:orange; >input[type=text]:enabled < background: lightgray; >input[type=text]:disabled < background: red; font-size: 20px; >input < margin-top:10px; border:1px solid brown; font-size: 20px; >body < text-align:center; >p < color: brown; font-size: 22px; border: 2px ridge blue; >label Disabled Demo for Text Field
Let suppose I am filling a form with all my credentials like my name, my mobile number, Employee number etc. Once I have entered all my details it will ask you for are sure confirm to submit the details. Once we submitted not all the details are going to allow for updating. Employee number is disabled to update because it is unique. Automatically is disabled. This kind of situation we have used disabled selector in CSS.
Explanation: As you can see in the output, you can edit the fields for First Name and Last Name, while the Address field is disabled and cannot be edited.
Example #2 – Disabled Button
h1 < color:orange; >input[type=text]:enabled < background: lightblue; >input[type=text]:disabled < background: red; font-size: 20px; >input < margin-top:10px; border:1px solid pink; font-size: 20px; >body < text-align:center; >p < color: red; font-size: 22px; border: 2px ridge green; >label < font-size: 20px; color: blue; font-weight: bold; >button Disabled Demo for Button Field
Let suppose I am filling a form with all my credentials like my name, my mobile number, Employee number etc. Once I have entered all my details it will ask you for are sure confirm to submit the details. Once we submitted not all the details are going to allow for updating. Employee number is disabled to update because it is unique. Automatically is disabled. This kind of situation we have used disabled selector in CSS.
lt;/html>
Explanation: As you can see in the output First Name and LastName can be editable, but the button is disabled, so we can’t edit it.
Example #3 – Text Area disabled
h1 < color:red; >input[type=text]:enabled < background: lightgreen; >input[type=text]:disabled < background: red; font-size: 20px; >input < margin-top:10px; border:1px solid red; font-size: 20px; >body < text-align:center; >p < color: fuchsia; font-size: 22px; border: 2px ridge navy; >label < font-size: 20px; color: orange; font-weight: bold; >button Disabled Demo for Button Field
Let suppose I am filling a form with all my credentials like my name, my mobile number, Employee number etc. Once I have entered all my details it will ask you for are sure confirm to submit the details. Once we submitted not all the details are going to allow for updating. Employee number is disabled to update because it is unique. Automatically is disabled. This kind of situation we have used disabled selector in CSS.
Explanation: As you can see, we can’t edit the text area as it is disabled.
Conclusion
The “disabled” selector in CSS is used to style and disable elements. We can use this disabled selector on form fields like buttons, text area, button, text field, etc.
Recommended Articles
We hope that this EDUCBA information on “CSS disabled” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
38+ Hours of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
149+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
253+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
CSS Course Bundle — 19 Courses in 1 | 3 Mock Tests
82+ Hours of HD Videos
19 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5