...

View Full Version : Setting element attributes in a different way



v1d4l0k4
07-30-2006, 06:08 PM
Hi anyone ! :thumbsup:

My doubt is the following one...

I would like to change the attributes of an element in such a way:


setElement('table1', {
border : 0,
rows : {
0 : {
cells : {
0 : {
innerHTML : 'foo',
},

1 : {
innerHTML : 'bar',
},
},
},
},
});
This would be interpreted by the JavaScript as:

document.getElementById('table1').border = 0;
document.getElementById('table1').rows[0].cells[0].innerHTML = 'foo';
document.getElementById('table1').rows[0].cells[1].innerHTML = 'bar';
Such function would be of much utility for me, therefore it would go to save much code... :)

Somebody would know to say me as I can make this ?! :(

Many thanks.

Cheers,
v1d4l0k4

----------

Excuse-me for my poorly english, I'm brazilian and I don't speak english fluently. :D

dumpfi
07-30-2006, 07:16 PM
Try this and not to use so many misplaced commas:
<html>
<head>
<script type="text/javascript">
Object.prototype.setProperties = function(props)
{
var x;

for(x in props)
{
if(x in props.constructor.prototype)
{
continue;
}
if(undefined == typeof this[x] || false == /^\s*function Object()/.test(props[x].constructor.toString()))
{
this[x] = props[x];
}
else
{
this[x].setProperties(props[x]);
}
}
}

function setElement(id, props)
{
var element = document.getElementById(id);

element.setProperties(props);

return element;
}

window.onload = function()
{
setElement(
'table1',
{
border : 10,
rows :
{
0 :
{
cells :
{
0 :
{
innerHTML : 'foo'
},
1 :
{
innerHTML : 'bar'
}
}
}
}
}
);
}
</script>
</head>
<body>
<table id="table1">
<tr>
<td>xxx</td>
<td>yyy</td>
</tr>
</table>
</body>
</html>
dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum