OK so I'm dynamically creating a bunch of HTML elements to render a tree view and now I'm wondering if I'm better off having a single function to handle onclick which receives a nodeID and looks up the right node to apply changes like this:
var nodesArray // this contains a references to every node in the tree
function switchStyles(nodeID) {
// look up the node in a huge array of all nodes
var node = nodesArray(nodeID);
var element = node.htmlElement;
// check properties, find
if (node.boolSelected) {
// apply the 'selected' class to it
element.className = 'selectedStyle';
}
//etc
}
Or whether I should attach properties and functions to each html element like this:
var labelDiv = document.createElement('div');
outerDiv.appendChild(labelDiv);
labelDiv.id = this.sRootElementID + '_' + aChild.iNodeID + '_l';
labelDiv.className = this.classNameLabel;
labelDiv.isSelected = false;
var sOver = this.classNameLabelOver
var sOut = this.classNameLabel;
var sSel = this.classNameLabelSelected
labelDiv.onmouseover = function () {
if (this.isSelected) {
this.className = sSel;
} else {
this.className = sOver;
}
}
labelDiv.onmouseout = function () {
if (this.isSelected) {
this.className = sSel;
} else {
this.className = sOut;
}
}