You can use an object:
var elementHandler = {
createElement: function() {
this.element1 = document.createElement("div");
this.element1.id = "el1";
// ...
},
editElement: function() {
// Use this.element1 here...
}
};
You call createElement
and editElement
like this:
elementHandler.createElement();
elementHandler.editElement();
(Or you might just call them create
and edit
as there's no need to repeat the word "element" all over the place...)
If hooking them up to an event, be sure you call them such that elementHandler
is this
within the call:
// Using an anonymous function
someElement.addEventListener("click", function() {
elementHandler.createElement();
}, false);
// Or using ES5's `bind`
someElement.addEventListener("click", elementHandler.createElement.bind(elementHandler), false);
// *NOT* like this:
someElement.addEventListener("click", elementHandler.createElement, false);