src/dom/attr.js
/**
* @module Attr
*/
import { each } from '../util';
/**
* Get the value of an attribute for the first element, or set one or more attributes for each element in the collection.
*
* @param {String|Object} key The name of the attribute to get or set. Or an object containing key-value pairs to set as attributes.
* @param {String} [value] The value of the attribute to set.
* @return {Object} The wrapped collection
* @chainable
* @example
* $('.item').attr('attrName'); // get
* $('.item').attr('attrName', 'attrValue'); // set
* $('.item').attr({attr1: 'value1', 'attr-2': 'value2'}); // set multiple
*/
export const attr = function(key, value) {
if(typeof key === 'string' && typeof value === 'undefined') {
const element = this.nodeType ? this : this[0];
return element ? element.getAttribute(key) : undefined;
}
return each(this, element => {
if(typeof key === 'object') {
for(let attr in key) {
element.setAttribute(attr, key[attr]);
}
} else {
element.setAttribute(key, value);
}
});
};
/**
* Remove attribute from each element in the collection.
*
* @param {String} key Attribute name
* @return {Object} The wrapped collection
* @chainable
* @example
* $('.items').removeAttr('attrName');
*/
export const removeAttr = function(key) {
return each(this, element => element.removeAttribute(key));
};