
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Attribute specified Property
HTML DOM attribute specified property is used to check that the mentioned attribute is specified or not. If the mentioned attribute is specified it will return true else will throw an error.
Syntax
attribute.specified
Return value
The `specified` property returns true if the attribute is mentioned in that tag, otherwise program stops running.
Examples of HTML DOM Attribute 'specified' Property
Below examples illustrating how to use the `specified` property in JavaScript and HTML.
Check if the onclick attribute is specified
The following code demonstrates the usage of the `specified` attribute to check if an attribute of an HTML element is explicitly specified.
<!DOCTYPE html> <html> <head> <title>HTML DOM Attribute specified Property</title> </head> <body> <h3>HTML DOM Attribute specified Property</h3> <div id="Div" style="background-color:blue;height:50px" onclick=""> </div> <p>Is the onclick attribute specified?</p> <p id="par"></p> <script> document.getElementById("par").innerHTML = document.getElementById("Div").getAttributeNode("onclick").specified; </script> </body> </html>
Crash the program when attribute not mentioned
The following code shows what will happen when we try to check presence of an attribute that is not present. It crashes after showing two results.
<!DOCTYPE html> <html> <head> <title>HTML DOM Attribute specified Property</title> </head> <body> <h3>HTML DOM Attribute specified Property</h3> <div id="TestDiv" style="background-color:red;height:50px" onclick=""> </div> <p> Are the following attributes specified for the element? </p> <p id="onclickResult"></p> <p id="idResult"></p> <p id="classResult"></p> <p id="styleResult"></p> <script> var element = document.getElementById("TestDiv"); // Check if the 'onclick' attribute is specified document.getElementById("onclickResult").innerHTML = "onclick: " + element.getAttributeNode("onclick").specified; // Check if the 'id' attribute is specified document.getElementById("idResult").innerHTML = "id: " + element.getAttributeNode("id").specified; // Check if the 'class' attribute is specified // We didnt mentioned class attribute for div // tag, hence program crashes here, you can't. // see result of class and style attributes. document.getElementById("classResult").innerHTML = "class: " + element.getAttributeNode("class").specified; // Check if the 'style' attribute is specified document.getElementById("styleResult").innerHTML = "style: " + element.getAttributeNode("style").specified; </script> </body> </html>
Handle the program crashing
Here we will see how to handle program crash. We will check if the attribute exists before trying to access the specified property using ternary operator.
<!DOCTYPE html> <html> <head> <title>HTML DOM Attribute specified Property</title> </head> <body> <h3>HTML DOM Attribute specified Property</h3> <div id="TestDiv" style="background-color:red;height:50px" onclick=""> </div> <p> Are the following attributes specified for the element? </p> <p id="onclickResult"></p> <p id="idResult"></p> <p id="classResult"></p> <p id="styleResult"></p> <script> var element = document.getElementById("TestDiv"); // Check if the 'onclick' attribute is specified var onclickAttr = element.getAttributeNode("onclick"); document.getElementById("onclickResult").innerHTML = "onclick: " + (onclickAttr ? onclickAttr.specified : false); // Check if the 'id' attribute is specified var idAttr = element.getAttributeNode("id"); document.getElementById("idResult").innerHTML = "id: " + (idAttr ? idAttr.specified : false); // Check if the 'class' attribute is specified var classAttr = element.getAttributeNode("class"); document.getElementById("classResult").innerHTML = "class: " + (classAttr ? classAttr.specified : false); // Check if the 'style' attribute is specified var styleAttr = element.getAttributeNode("style"); document.getElementById("styleResult").innerHTML = "style: " + (styleAttr ? styleAttr.specified : false); </script> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
specified | Yes | Yes | Yes | Yes | Yes |