HTML | Disabled Attribute - GeeksforGeeks

The disabled attribute in HTML indicates whether the element is disabled or not. If this attribute is set, the element is disabled. The disabled attribute is usually drawn with grayed-out text. If the element is disabled, it does not respond to user actions, it cannot be focused. It is a boolean attribute.

Usage: It can be used on the following elements: <button>, <input>, <option>, <select>, <textarea>, <fieldset>, <outgroup>, and <keygen>. 

Syntax:  

<tag disabled></tag>

Example: In this example we demonstrate the use of the disabled attribute in a <button> tag, rendering the “Click Me!” button unclickable and visually indicating it is disabled.

HTML <!DOCTYPE html> <html> <head> <title>HTML disabled Attribute</title> </head> <body style="text-align:center"> <h1 style="color: green;">GeeksforGeeks</h1> <h2>HTML disabled Attribute</h2> <!--A disabled button--> <button type="button" disabled>Click Me!</button> </body> </html>

Output: 

button

<input>: When the disabled attribute is present, it specifies that the input is disabled. A disabled input is unusable and un-clickable.

Example: In this example shows the disabled attribute in an <input> field, making it uneditable. The input field displays “This input field is disabled” as a placeholder.

HTML <!DOCTYPE html> <html> <head> <title>HTML disabled Attribute</title> </head> <body style="text-align:center"> <h1 style="color: green;">GeeksforGeeks</h1> <h2>HTML disabled Attribute</h2> <!--A disabled input--> <label>Input: <input type="text" name="value" value="This input field is disabled" disabled> </label> </body> </html>

Output: 

input

<option>: When the disabled attribute is present, it specifies that the option field is disabled. A disabled option is unusable and un-clickable.

Example: In this example we demonstrates the disabled attribute in a <select> dropdown, where the “Volvo” option is disabled, making it unselectable, while other car brands remain selectable.

HTML <!DOCTYPE html> <html> <head> <title>HTML disabled Attribute</title> </head> <body style="text-align:center"> <h1 style="color: green;">GeeksforGeeks</h1> <h2>HTML disabled Attribute</h2> <!--A disabled input--> <p>Volvo is disabled.</p> <select> <option value="volvo" disabled>Volvo</option> <option value="saab">Saab</option> <option value="vw">VW</option> <option value="audi">Audi</option> </select><br> </body> </html>

Output: 

option

<select>: When the disabled attribute is present, it specifies that the select field is disabled. A disabled select is unusable and un-clickable.

Example: In this example shows a disabled <select> element where the entire dropdown menu is disabled, preventing the user from selecting any of the search algorithm options listed.

HTML <!DOCTYPE html> <html> <head> <title>HTML disabled Attribute</title> </head> <body style="text-align:center"> <h1 style="color: green;">GeeksforGeeks</h1> <h2>HTML disabled Attribute</h2> <!--A disabled input--> <p>This select field is disabled.</p> <select disabled> <option value="binary">Binary Search</option> <option value="linear">Linear Search</option> <option value="interpolation"> Interpolation Search </option> </select> </body> </html>

Output: 

select

<textarea>: When the disabled attribute is present, it specifies that the textarea field is disabled. A disabled textarea is unusable and un-clickable.

Example: In this example we demonstrates a disabled <textarea> element, which prevents user interaction. The disabled attribute makes the textarea field uneditable, displaying the given content as read-only.

HTML <!DOCTYPE html> <html> <head> <title>HTML disabled Attribute</title> </head> <body style="text-align:center"> <h1 style="color: green;">GeeksforGeeks</h1> <h2>HTML disabled Attribute</h2> <!--A disabled textarea--> <textarea disabled> This textarea field is disabled. </textarea> </body> </html>

Output: 

textarea

<fieldset>: When the disabled attribute is present, it specifies that the fieldset is disabled. A disabled fieldset is unusable and un-clickable.

Example: In this example showcases a disabled <fieldset> containing a text input. The disabled attribute prevents interaction with all elements inside the fieldset, making them uneditable.

HTML <!DOCTYPE html> <html> <head> <title>HTML disabled Attribute</title> </head> <body style="text-align:center"> <h1 style="color: green;">GeeksforGeeks</h1> <h2>HTML disabled Attribute</h2> <!--A disabled fieldset--> <p>This field set is disabled.</p> <fieldset disabled> Name: <input type="text"><br> </fieldset> </body> </html>

Output: 

fieldset

<optgroup>: When the disabled attribute is present, it specifies that the optgroup is disabled. A disabled optgroup is unusable and un-clickable.

Example: In this example we demonstrates a disabled <optgroup> within a <select> dropdown. The disabled attribute renders all options under “German Cars” unselectable, preventing user interaction with those options.

HTML <!DOCTYPE html> <html> <head> <title>HTML disabled Attribute</title> </head> <body style="text-align:center"> <h1 style="color: green;">GeeksforGeeks</h1> <h2>HTML disabled Attribute</h2> <!--A disabled optgroup--> <select> <optgroup label="German Cars" disabled> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </optgroup> </select> </body> </html>

Output: 

optgroup

Supported Browsers: The browser supported by disabled attribute are listed below:

  • Google Chrome
  • Edge 
  • Firefox
  • Opera
  • Safari

HTML disabled Attribute – FAQs

Which HTML elements support the disabled attribute?

The disabled attribute is supported by form elements, including <button>, <input>, <select>, <textarea>, <fieldset>, <optgroup>, and <option>.

What is the effect of the disabled attribute on form submission?

When a form element is disabled, its value is not submitted with the form. The disabled element is completely ignored by the form submission process.

Can you use the disabled attribute with links (<a> tags)?

No, the disabled attribute is not applicable to links (<a> tags). To disable a link, you need to use JavaScript or CSS to prevent its default action or visually style it to look disabled.

What is the difference between readonly and disabled attributes?

The readonly attribute makes an input field non-editable but still selectable, and its value is submitted with the form. The disabled attribute makes the element entirely non-interactive, and its value is not submitted with the form.

Can the disabled attribute be used with <form> elements?

No, the disabled attribute cannot be applied to the <form> element itself, but it can be applied to its child elements, like <input>, <button>, etc.

V

Vishal Chaudhary 2 Follow Improve Previous Article HTML dirname Attribute Next Article HTML download Attribute

Từ khóa » Html A Tag Disabled