From 202da2d4687693642a6a3ad1d78acd366a52073a Mon Sep 17 00:00:00 2001 From: Owen Edwards Date: Thu, 25 Aug 2016 21:05:18 -0700 Subject: [PATCH] feat(component): attribute get/set/remove methods This adds getter and setter and remover methods for attributes on components. --- src/js/component.js | 36 ++++++++++++++++++++++++++++++++++++ src/js/utils/dom.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/js/component.js b/src/js/component.js index 141206467b..d9367fb486 100644 --- a/src/js/component.js +++ b/src/js/component.js @@ -985,6 +985,42 @@ class Component { return this; } + /** + * Get the value of an attribute on the component's element + * + * @param {String} attribute Attribute to get + * @return {String} + * @method getAttribute + */ + getAttribute(attribute) { + return Dom.getAttribute(this.el_, attribute); + } + + /** + * Set the value of an attribute on the component's element + * + * @param {String} attribute Attribute to set + * @param {String} value Value to set the attribute to + * @return {Component} + * @method setAttribute + */ + setAttribute(attribute, value) { + Dom.setAttribute(this.el_, attribute, value); + return this; + } + + /** + * Remove an attribute from the component's element + * + * @param {String} attribute Attribute to remove + * @return {Component} + * @method removeAttribute + */ + removeAttribute(attribute) { + Dom.removeAttribute(this.el_, attribute); + return this; + } + /** * Set or get the width of the component (CSS values) * Setting the video tag dimension values only works with values in pixels. diff --git a/src/js/utils/dom.js b/src/js/utils/dom.js index acb66680f7..4d6f1193e8 100644 --- a/src/js/utils/dom.js +++ b/src/js/utils/dom.js @@ -404,6 +404,41 @@ export function getElAttributes(tag) { return obj; } +/** + * Get the value of an element's attribute + * + * @param {Element} el + * @param {String} attribute Attribute to get + * @return {String} value of the attribute + * @method getAttribute + */ +export function getAttribute(el, attribute) { + return el.getAttribute(attribute); +} + +/** + * Set the value of an element's attribute + * + * @param {Element} el + * @param {String} attribute Attribute to set + * @param {String} value Value to set the attribute to + * @method setAttribute + */ +export function setAttribute(el, attribute, value) { + el.setAttribute(attribute, value); +} + +/** + * Remove an element's attribute + * + * @param {Element} el + * @param {String} attribute Attribute to remove + * @method removeAttribute + */ +export function removeAttribute(el, attribute) { + el.removeAttribute(attribute); +} + /** * Attempt to block the ability to select text while dragging controls *