Skip to content

Instantly share code, notes, and snippets.

@ondrek
Last active August 29, 2015 14:07
Show Gist options
  • Save ondrek/208b4af7fba425cd1889 to your computer and use it in GitHub Desktop.
Save ondrek/208b4af7fba425cd1889 to your computer and use it in GitHub Desktop.
DOM manipulations for hackers
Notes:
- very low-level API with simple prototyped-based javascript
- optimised for better readability and performance
- jquery-like simple functions for append, css, remove
(after, append, attr, before, children, clone, css, …)
- supports only 2 latest browsers
(for better support can be added an extension shim)
Usage sample (almost same usage as jquery):
ele( {element: "li", text:"my text"} );
// <li>my text</li>
ele( [ {element: "div", text:"text1"}, {element: "span", text:"text2"} ] );
// [ <div>text1</div>, <span>text2</span> ]
ele( { element: "div", text:"text1", css : {…}} )
// <div style="…">text1</div>
var body = ele("body");
body.append({ element: "li", id: "myId"});
body.css({ color: "red" });
// <body style="color: red"><li id="myId"></li></body>
Implementation and beautify of the framework:
function ele(){
return ( new TheElement(arguments) );
}
function TheElement(…) {…};
TheElement.prototype = {
get text () {
return this.innerText_;
},
set text (newText) {
newText += "<br>"; // awesomeness can be here
this.innerText_ = newText;
},
get html () {…}
set html (rules) {…}
get css () {…}
set css (rules) {…}
};
var span = ele( { element: "span", text: "my text"} );
span.text = "my first text";
// this is the important difference in compare to jquery
// you dont work with functions - span.text("s");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment