Element.setAttribute >= 2.0.0

Purpose

Add an attribute getter/setter

Syntax

Element.setAttribute ( StringnameFunctiongetterFunctionsetter );


Parameters

name
The name of the attribute
getter
The function that will return the value when accessed as a property
setter
The function that will transform the value before it's set

Examples

Add a simple attribute by name

You can choose to only provide a name. That way, a property will be created that is basically a reference to a getAttribute(name) call

// Say you have a class called "MyElement":
MyElement.setAttribute('fullname');

// The element will look like this:
// 

// Now let's create a new instance of this element in the browser:
let element = document.createElement('my-element');

// Now we can set the fullname attribute via a property:
element.fullname = 'John Skerit';

// Now the element looks like
// 

// You can access the value in 2 ways:
element.fullname === element.getAttribute('fullname');