⚖️attributes vs. properties

HTML attributes vs. DOM properties

🔸 HTML attributesDOM propertiesattr-prop sync

For element nodes, most standard HTML attributes automatically become properties of DOM objects. 📗 JS.info ⟩ Attributes and properties

<body id="page"> => body.id="page"

HTML attributes

  • attribute name is case-insensitive.

  • attribute values are always strings.

  • element.attributes collection is iterable and has all the attributes as objects with name and value properties.

DOM properties

  • DOM properties are NOT always strings. for example:

  • a.href is always a full URL. ( 👉🏻 See: 💈範例 )

property ⇔ attribute sync

  • standard attribute changes ⇒ auto-update property, and vice versa.

  • ⭐️ with some exceptions ( 👉🏻 💈範例 )

Last updated