.getAttribute()
String? Element.getAttribute(String name)
Returns the value of the specified attribute.
element.getAttribute(name);
Looks up the attrbiute name of element.
Note:
Attributes differ from properties on an element. A property is simply a property on the object, which can hold any type, while an attribute is always a string. For a variety of interactive properties, such as
checked
or value
of an input, you probably want to simply access the property, eg. let isChecked = checkboxElement.checked;
or let val = inputElement.value;
.
See also: .setAttribute()
Example
HTML:
<input type="checkbox">
JavaScript:
let value = document.query("input").getAttribute("type");
Result is "checkbox"
.