From 679a2d85323c8656522522655a1e0ddaaa3a56d1 Mon Sep 17 00:00:00 2001 From: chylex Date: Tue, 25 Oct 2016 22:47:05 +0200 Subject: [PATCH] Add a smaller version of dom.js to renderer --- src/renderer/scr.dom.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/renderer/scr.dom.js diff --git a/src/renderer/scr.dom.js b/src/renderer/scr.dom.js new file mode 100644 index 0000000..0cd1a7b --- /dev/null +++ b/src/renderer/scr.dom.js @@ -0,0 +1,40 @@ +var DOM = (function(){ + return { + /* + * Returns a child element by its ID. Parent defaults to the entire document. + */ + id: function(id, parent){ + return (parent || document).getElementById(id); + }, + + /* + * Returns an array of all child elements containing the specified class. Parent defaults to the entire document. + */ + cls: function(cls, parent){ + return Array.prototype.slice.call((parent || document).getElementsByClassName(cls)); + }, + + /* + * Returns an array of all child elements that have the specified tag. Parent defaults to the entire document. + */ + tag: function(tag, parent){ + return Array.prototype.slice.call((parent || document).getElementsByTagName(tag)); + }, + + /* + * Creates an element, adds it to the DOM, and returns it. + */ + createElement: function(tag, parent){ + var ele = document.createElement(tag); + parent.appendChild(ele); + return ele; + }, + + /* + * Removes an element from the DOM. + */ + removeElement: function(ele){ + ele.parentNode.removeChild(ele); + } + }; +})();