vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
35 lines (34 loc) • 960 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Get/set the value of an attribute on a given DOM element
*
* @param elm - The DOM element to fetch/set the attribute from
* @param attrName - Name of the attribute to handle
* @param value - Value to insert into the attribute
* @return Data found in the attribute (the old value if {value} is defined)
*
* @example
*
* ```ts
* // Get the value of an attribute
* attr(document.documentElement, 'lang');
*
* // Set the value of an attribute
* attr(document.documentElement, 'lang', 'da-DK');
* ```
*/
function attr(elm, attrName, value) {
const currVal = elm.getAttribute(attrName);
if (value === false) {
elm.removeAttribute(attrName);
}
else if (value !== undefined) {
if (value === true) {
value = '';
}
elm.setAttribute(attrName, String(value));
}
return currVal;
}
exports.default = attr;