JavaScript - Difference Between Attributes And Properties

javascript

// JavaScript - Difference between attributes and properties:

Attributes are defined by HTML.  The type of attributes are always string.  
Properties belong to the DOM, and the DOM is an object in JavaScript.  We can
get and set properties as we do to a normal object in JavaScript, and properties
can be of any types.  Non-custom attributes have 1:1 mapping onto properties, 
like: id, class, title, etc.  Non-custom propertiy (attribute) changes when 
corresponding attribute (property) changes in most cases.  Attribute which has a 
default value doesn't change when corresponding property changes.
It is recommended to use property in JavaScript as it's much easier and faster. 
Especially for boolean type attributes like: "checked", "disabled" and 
"selected", browser automatically converts them into boolean type properties.

document.getElementById('test').checked = true;
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License