What is the difference between properties and attributes in HTML?
Last Updated :
01 Feb, 2023
HTML is the standard language for creating documents that are intended to be displayed on web browsers. It is very usual to get confused with attributes and properties while performing DOM object manipulation in JavaScript. Before learning about the differences, let us first define the Document Object Model.
DOM: It is the acronym for the Document Object Model. When the browser parses your HTML code, it creates a corresponding DOM node. The HTML properties are accessed from this node object.
In simpler terms, DOM is an API for the HTML, and we use languages like JavaScript or frameworks like React, Vue.js to access and manipulate the HTML using the corresponding DOM objects.
Notes:Â
- Some DOM properties don't possess corresponding attributes.
- Some HTML attributes don't possess corresponding properties.
- Some HTML attributes, like 'class', possess 1:1 mapping to properties.
Let us take a look at some quick examples to demonstrate the differences between attributes and properties.
Example: Consider the following HTML code snippet.
html
<!DOCTYPE html>
<html>
<body>
<input id="input" type="number"
value="Phone Number:">
<p id="display"></p>
<script>
var element = document.getElementById("input");
// Getting the property, returns "Phone Number:"
window.alert(element.getAttribute("value"));
element.value = 1234;
var x = element.value;
// Getting the attribute, returns "1234"
document.getElementById("display").innerHTML
= "Value Attribute: " + x;
</script>
</body>
</html>
Output:Â
Â

Â
In the webpage above, let us consider that the user inputs "1234" into the input field. Getting the value of the "value" attribute via element.getAttribute("value") would return "Phone Number:" because we have provided this as the initial value of this attribute in the source code. But getting the value of the "value" property via element.value will return "1234" because the "value" property gets updated with the new value, but not the "value" attribute.
Now we have a basic idea about the difference, let us dive deep and learn about more differences.
Attribute: Attributes are defined by HTML and are used to customize a tag.
Â
html
<!DOCTYPE html>
<html>
<body>
<p>
Click the button the display
the number of attributes
associated with it.
</p>
<button id="AttributeDemo"
onclick="myFunction()">
Try it
</button>
<p id="display"></p>
<script>
function myFunction() {
var Attrdemo = document.getElementById(
"AttributeDemo").attributes.length;
document.getElementById(
"display").innerHTML = Attrdemo;
}
</script>
</body>
</html>

The output is 2 because the two attributes are: id="AttributeDemo" and onclick="myFunction()".
Note: The document.getElementsById('AttributeDemo').attributes code snippet returns a collection of the specified node's attributes, as a NamedNodeMap object. To access the nodes, we can use the generic indexing method. Replace the following line in the above code snippet.
javascript
var Attrdemo = document.getElementById(
'AtrributeDemo').attributes[1].name;
Output:Â
onclick
The attributes have a data type of string. So no matter the value of the attribute, it will always return a string.Â
Example:Â
document.getElementById('AtrributeDemo').getAttribute('id')
Output:Â
AttributeDemo
Property: In contrast to the attributes, which are defined in HTML, properties belong to the DOM. Since DOM is an object in JavaScript, we can get and set properties.
javascript
<!DOCTYPE html>
<html>
<body>
<p>
Click the "display" button for
the number of attributes
associated with it.
</p>
<button id="AttributeDemo"
onclick="myFunction()">
Try it
</button>
<p id="display"></p>
<script>
function myFunction() {
// Setting the property
// 'geeksforgeeks' to a number
document.getElementById('AttributeDemo')
.geeksforgeeks = 777;
// Getting the property, returns 1
var x = document.getElementById(
'AttributeDemo').geeksforgeeks;
document.getElementById(
"display").innerHTML = x;
}
</script>
</body>
</html>
Output:Â

Default attributes (non-user-defined) change when corresponding property changes and vice-versa.
Â
html
<!DOCTYPE html>
<html>
<body>
<p>
Click the button the display the
current className & the edited
className
</p>
<button id="demo1" class="button"
onclick="Function1()">
Click Me
</button>
<p id="displayCurrent"></p>
<p id="diplayEdited"></p>
<script>
function Function1() {
var div = document.getElementById("demo1");
// Returns "button"
window.alert("Current Class : "
+ div.getAttribute("class"));
div.setAttribute("class", "GeeksForGeeks");
// Returns : "GeeksForGeeks"
window.alert("Edited Class : "
+ div.getAttribute("class"));
}
</script>
</body>
</html>
Output:Â
Â

Note:Â
- Non-custom attributes (like id, class, etc.) have 1:1 mapping to the properties.
- We use 'className' to access (get or set) the 'class' property because 'class' is a reserved keyword in JavaScript.
- Attributes that have a defined default value remain constant when the corresponding property changes.
Difference between HTML attributes and DOM properties:
Attribute | Property |
---|
Attributes are defined by HTML. | Properties are defined by the DOM. |
The value of an attribute is constant. | The value of a property is variable. |
These are used to initialize the DOM properties. After initialization, the job is finish.  | No such job defined. |
Similar Reads
Attributes that work differently between React and HTML
There is a minor difference between React(JSX) and HTML attributes. Like for example, the HTML attributes like class and for are replaced with className and htmlFor in React. There are a number of attributes that work differently between React and HTML. Grammar in JSX is mostly the same as in HTML,
3 min read
What's the difference between element value and data attribute ?
The element value attribute specifies the default value of the HTML element whereas the data attribute allows you to store extra data to tags in HTML when no other HTML attribute can do so. Element value: The HTML element is the collection of start and end tags with the content inserted in between t
2 min read
What's the difference between meta name and meta property?
Meta Data: The metadata means information about data. So basically Meta tag provides you the information on the HTML document. So basically this tag is an empty tag which means it only has an opening tag and no closing tag. The number of meta tags in an HTML document depends upon the HTML document a
2 min read
Difference between id and class Attributes in HTML
The id and class attributes in HTML are used for element identification and styling. The id attribute uniquely identifies a single element, while the class attribute can be applied to multiple elements. Both are essential for CSS styling and JavaScript manipulation.HTML id AttributeThe id attribute
3 min read
Difference between structural and attribute directives
Structural directives manipulate the DOM layout by adding, removing, or replacing elements, while attribute directives modify the appearance or behavior of elements without affecting their structure. Directives in Angular are nothing but the classes that allow us to add and modify the behavior of el
5 min read
What is the difference between '@' and '=' in directive scope in AngularJS ?
AngularJS is a popular JavaScript framework, that provides powerful features for building dynamic web applications. When creating custom directives in AngularJS, you may come across the need to define a scope for your directive. The two most common methods to do this are by using the @ and = symbols
4 min read
Difference between disabled and readonly attribute in HTML
In this article, we will see the basic difference between the disabled & readonly attributes in HTML, along with understanding through the basic examples. Both disabled and readonly attributes in HTML are used to restrict user input in form fields. They only differ in how they restrict input and
3 min read
What is the difference between <section> and <div> tags in HTML?
The <div> tag and <section> tag are used in the webpage, the <section> tag means that the content inside relates to a single theme, and the <div> tag is used as a block part of the web page.Table of Content HTML div Tag HTML section TagHTML div Tag It is called a division tag
3 min read
Difference between 'hidden' and 'aria-hidden' attributes in HTML
The HyperText Markup Language (HTML) is a powerful web development tool combined with CSS and JavaScript. Apart from these, HTML also uses Accessible Rich Internet Application (ARIA) to make web content affable for a person with disabilities. Although ARIA is beneficial, there are keywords common to
2 min read
What is the difference between DOM and BOM ?
In this article, we will know about the Document Object Model (DOM) & Browser Object Model (BOM), along with understanding their basic implementation through the examples & the differences between them. Document Object Model (DOM) is a programming interface for HTML and XML documents, that a
5 min read