Boolean Attribute
Boolean attributes in HTML are attributes that, when present, are considered “true” and don’t require a value. They are commonly used for toggling certain behaviors or states, such as the `disabled` attribute for form elements.
Example:
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >Boolean Attribute Example</ title > < style > body { text-align: center; } h2 { color: green; } button { padding: 10px; font-size: 16px; } </ style > < script > const toggleButton = () => { const buttonElement = document.getElementById("myButton"); buttonElement.disabled = !buttonElement.disabled; }; </ script > </ head > < body > < h2 >Welcome to Boolean Attribute Example</ h2 > < button id = "myButton" onclick = "toggleButton()" > Click me (Toggle disabled attribute) </ button > </ body > </ html > |
Output:
What are HTML Attributes ?
HTML attributes are the entities that provide the extra information about the tags. Attributes are specified using name and value pair. Some HTML tags are used without attributes while for some tags it’s important to specify attributes along with them. In paired tags attributes are specified in the opening tag. The value of the attribute is specified between the quotes. They are used to add extra effects to the element.