skacore
asked on
Dynamic Evaluation of document.getElementById
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)
{
if(validId(docObjName))
{
eval("document.getElementB yId(\"" + docObjName + "\")." + attribute + " = \"" + value + "\"");
}
}
function validId(docObjName)
{
if(document.getElementById (docObjNam e) != null)
{
return true;
}
return false;
}
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.
function updateElement(docObjName, attribute, value)
{
if(validId(docObjName))
{
eval("document.getElementB
}
}
function validId(docObjName)
{
if(document.getElementById
{
return true;
}
return false;
}
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is a simple example.
You can assign and inquire attribute for a table
Has no belts-whistles to for validate value checks (while assign attributes).
Hope this helps.
<html>
<head>
<title>Attribute Tutorial</title>
</head>
<body>
<script language="javascript">
function validId(docObjName)
{
return (document.getElementById(d ocObjName) );
}
function ApplyAttribute (objIdx, attrName, attrValue) {
var curObj= document.getElementById (objIdx);
if (!curObj) { return; }
var retCode = curObj.setAttribute (attrName, attrValue);
if (retCode) { alert ('Error '); }
}
function InquireAttribute (objIdx, attrName) {
var curObj= document.getElementById (objIdx);
if (!curObj) { return; }
var retValue = curObj.getAttribute (attrName);
alert ('Object ' + objIdx +
'\n\t Attribute Name : ' + attrName +
'\n\t Attribute Value : ' + retValue);
}
</script>
<form>
<select name="attrName">
<option value="align">align</optio n>
<option value="border">border</opt ion>
<option value="height">height</opt ion>
<option value="width">width</optio n>
</select>
<input type="text" name="attrValue">
<input type="button" value="Apply Attribute" onclick="ApplyAttribute('t bl1',
this.form.attrName.value, this.form.attrValue.value) ;">
<input type="button" value="Inquire Attribute"
onclick="InquireAttribute( 'tbl1', this.form.attrName.value); ">
</form>
<br>
<table id="tbl1" align="left" width="400" height="100" border="1">
<tr>
<td>
Table Attributes
<br>align = [center|left|right|justifi ed]
<br>border = [0|1]
<br>height = [number of pixels | % ]
<br>width = [number of pixels | % ]
</td>
</tr>
</table>
</body>
</html>
You can assign and inquire attribute for a table
Has no belts-whistles to for validate value checks (while assign attributes).
Hope this helps.
<html>
<head>
<title>Attribute Tutorial</title>
</head>
<body>
<script language="javascript">
function validId(docObjName)
{
return (document.getElementById(d
}
function ApplyAttribute (objIdx, attrName, attrValue) {
var curObj= document.getElementById (objIdx);
if (!curObj) { return; }
var retCode = curObj.setAttribute (attrName, attrValue);
if (retCode) { alert ('Error '); }
}
function InquireAttribute (objIdx, attrName) {
var curObj= document.getElementById (objIdx);
if (!curObj) { return; }
var retValue = curObj.getAttribute (attrName);
alert ('Object ' + objIdx +
'\n\t Attribute Name : ' + attrName +
'\n\t Attribute Value : ' + retValue);
}
</script>
<form>
<select name="attrName">
<option value="align">align</optio
<option value="border">border</opt
<option value="height">height</opt
<option value="width">width</optio
</select>
<input type="text" name="attrValue">
<input type="button" value="Apply Attribute" onclick="ApplyAttribute('t
this.form.attrName.value, this.form.attrValue.value)
<input type="button" value="Inquire Attribute"
onclick="InquireAttribute(
</form>
<br>
<table id="tbl1" align="left" width="400" height="100" border="1">
<tr>
<td>
Table Attributes
<br>align = [center|left|right|justifi
<br>border = [0|1]
<br>height = [number of pixels | % ]
<br>width = [number of pixels | % ]
</td>
</tr>
</table>
</body>
</html>
Oops, looks like you accepted while was gathering this tutorial.
Thanks,
_PA
Thanks,
_PA
<script>
function updateElement(docObjName, attribute, value) {
alert(typeof value === typeof document.getElementById(do
if( document.getElementById(do
document.getElementById(do
}
}
</script>
<div id="test" onClick="updateElement(thi