Extend allows you to chain one function after another, and introduces support for helper functions.
| The Extend Namespace | Extend allows you to chain one function after another, and introduces support for helper functions. |
| Version | 1.0 |
| Element | These functions are bound to elements. |
| Properties | |
| addClass | Adds class name to element |
| removeClass | Removes class name to element |
| replaceClass | Replaces element’s class with another one |
| hasClass | Tests if element has class |
| toggleClass | Toggles a class on/off |
| getElementsByAttribute | Grabs elements By Attribute and optional Value pair |
| insertAfter | Inserts an element after a specified reference point (Honestly, why the heck isn’t this spec’d?) |
| elementName | Returns the proper (lowercased) nodeName |
| getFirstChild | Returns the first child of a parent element |
| getLastChild | Returns the last child of a parent element |
| hasChildNode | Tests if element has a specified child node |
| hasParentNode | Tests if element has a specified parent node |
| getChildNodes | Get an element’s child nodes |
| getParentNode | Get an element’s parentNode (can be specified) |
| removeNode | Remove an element from the DOM http://msdn2.microsoft.com- /en-us- /library- /ms536708.aspx (shock!) |
| getText | Get an element’s text value |
| setText | Set an element’s text value |
| setOpacity | Set an element’s opacity value |
| getComputedStyle | Shortcut for the verbose document.defaultView.getComputedStyle |
| setStyle | Shortcut for foo.style[property] = value; |
| getPosition | Get an element’s current position relative to its parent |
| getDocumentPosition | Get an element’s current position relative to the document |
| setInnerHTML | Set an element’s innerHTML |
| getInnerHTML | Get an element’s innerHTML |
| Chaining | Extend introduces a way to chain getters and setters. |
1.0
These functions are bound to elements.
| Properties | |
| addClass | Adds class name to element |
| removeClass | Removes class name to element |
| replaceClass | Replaces element’s class with another one |
| hasClass | Tests if element has class |
| toggleClass | Toggles a class on/off |
| getElementsByAttribute | Grabs elements By Attribute and optional Value pair |
| insertAfter | Inserts an element after a specified reference point (Honestly, why the heck isn’t this spec’d?) |
| elementName | Returns the proper (lowercased) nodeName |
| getFirstChild | Returns the first child of a parent element |
| getLastChild | Returns the last child of a parent element |
| hasChildNode | Tests if element has a specified child node |
| hasParentNode | Tests if element has a specified parent node |
| getChildNodes | Get an element’s child nodes |
| getParentNode | Get an element’s parentNode (can be specified) |
| removeNode | Remove an element from the DOM http://msdn2.microsoft.com- /en-us- /library- /ms536708.aspx (shock!) |
| getText | Get an element’s text value |
| setText | Set an element’s text value |
| setOpacity | Set an element’s opacity value |
| getComputedStyle | Shortcut for the verbose document.defaultView.getComputedStyle |
| setStyle | Shortcut for foo.style[property] = value; |
| getPosition | Get an element’s current position relative to its parent |
| getDocumentPosition | Get an element’s current position relative to the document |
| setInnerHTML | Set an element’s innerHTML |
| getInnerHTML | Get an element’s innerHTML |
Grabs elements By Attribute and optional Value pair
getByAttr
| elAttribute | the attribute to retrieve. |
| elValue | (optional) a matching value pair |
var foo = document.getElementsByAttribute("type", "submit");
var foo = document.getByAttr("type", "submit"); // shortcutInserts an element after a specified reference point (Honestly, why the heck isn’t this spec’d?) http://developer.mozilla.org- /en- /docs- /DOM:element.insertBefore#Specification
| newNode | the node to insert. |
| referenceNode | the reference point to insert after |
var foo = document.createElement("div").addClass("foo");
foo.insertAfter(foo, document.getById("zomg"));Get an element’s parentNode (can be specified)
| parentNode | (optional) Filter results by element type |
var foo = document.getElementById("foo");
var children = foo.getParentNode(); // Returns immediate parentNode
children = foo.getChildNodes("div"); // Returns first "div" parentRemove an element from the DOM http://msdn2.microsoft.com- /en-us- /library- /ms536708.aspx (shock!)
var foo = document.getElementById("foo");
foo.parentNode.removeChild(foo); // The DOM 3 way
foo.removeNode(); // faster methodShortcut for the verbose document.defaultView.getComputedStyle
| cssProp | The property to retrieve |
var foo = document.getElementById("foo");
var width = document.defaultView.getComputedStyle(foo, null).getPropertyValue("width");
width = foo.getComputedStyle("width"); // Much shorterSet an element’s innerHTML
| markup | The markup to insert |
| method | (optional) If defined, append or prepend to element |
var foo = document.getElementById("foo");
foo.setInnerHTML("<li>zomg</li>"); // overrides innerHTML
foo.setInnerHTML("<li>lolz</li>", "append"); // appends HTML to foo
foo.setInnerHTML("<li>wtf</li>", "prepend"); // prepends HTML to fooExtend introduces a way to chain getters and setters.
Chaining getters:
Grab all .zomg elements inside all .foo elements
var foo = document.getByClass("foo").getByClass("zomg");Let’s filter that. Grab all li.zomg elements inside all ul.foo elements
var foo = document.getByTag("ul").filter(function(ul) {
return ul.hasClass("foo");
}).getByTag("li").filter(function(li) {
return li.hasClass("zomg");
});Chaining setters:
Grab all .zomg elements inside all ul.foo elements, add an event listener to each
var foo = document.getByTag("ul").filter(function(ul) {
return ul.hasClass("foo");
}).getByClass("zomg").addEventListener("click", function() {
console.log(this);
});Grab all .zomg elements inside all ul.foo elements, add an event listener to each, add classname "done"
var foo = document.getByTag("ul").filter(function(ul) {
return ul.hasClass("foo");
}).getByClass("zomg").addEventListener("click", function() {
console.log(this);
}).addClass("done");