Dynamic Evaluation of document.getElementById
Posted on 2006-05-17
So here's the scenario. I want to be able to dynamically edit any portion of any element through a function. It'll be easier explained once you look at the function:
function updateElement(docObjName, attribute, value)
eval("document.getElementById(\"" + docObjName + "\")." + attribute + " = \"" + value + "\"");
if(document.getElementById(docObjName) != null)
So here's the question. This works perfectly fine for the basic scenarios of doing simple updates of .style.height or .top, but the problem comes when I attempt to do more complex things through it. Anything with a " in it causes an issue because it will end the quoted section prematurely. Also it has problems if there are any complex tags being passed, for example loading a large set of HTML into a div. So my question is how to take this function from it's current form to one that will work no matter what I am trying to assign to whatever attribute I choose.