In general, you can use element.setAttribute('attributeName', 'value')
or element.propertyName = value
to switch the attributes or properties of the elements.
Logical attributes
For boolean attributes, set the attribute with the same name:
element.setAttribute('disabled', 'disabled');
Removing the boolean attribute works the same as other attributes:
element.removeAttribute('disabled');
However, none of your two examples are logical attributes !
contenteditable
contenteditable
not a boolean attribute, its enumerated attribute. Its possible values ββare empty string, "true"
and "false"
.
While setAttribute
seems redundant in this case, you can use it:
element.setAttribute('contenteditable', 'true'); // to undo: element.removeAttribute('contenteditable');
The property name for the attribute is contenteditable
contenteditable
(note the capital E
), but recognizes the values 'true'
, 'false'
and 'inherit'
- so you can simply use:
element.contentEditable = 'true'; // to undo: element.contentEditable = 'false';
Note that 'true'
and 'false'
are strings here, not booleans.
data-example
For the data-example
attribute, you can use:
element.setAttribute('data-example', 'some value');
Or, in browsers that support dataset
(see those highlighted in green at http://caniuse.com/dataset ), you can use:
element.dataset.example = 'some value';