Hi,
Is it possible to have something like the following?
Thanks for any ideas.Code:document.VARIABLE1.style.VARIABLE2 = (some color);
Printable View
Hi,
Is it possible to have something like the following?
Thanks for any ideas.Code:document.VARIABLE1.style.VARIABLE2 = (some color);
I am not sure why you would want to do that, but yes, it is possible.
Just make sure it is the right syntax. And you reference by "id", not by "name".
It makes a lot more sense to simply use document.getElementById() then continue from there.
For example, you have some elements called "element1", "element2", "element3".
You want to put different value for each of them.
Note the position of the <script> is after the <div>. Putting <script> at the top will not work, because the div cannot be identified.HTML Code:<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="element1">1</div>
<div id="element2">2</div>
<div id="element3">3</div>
<script>
for (i = 1; i < 4; i++)
{
var someelement = document.getElementById("element" + i);
alert(someelement.innerHTML);
}
</script>
</body>
</html>
You can check out www.w3schools.com if you want to learn about javascript. It has saved my coding life several times. I learn a lot from that website.
You can also check for any errors using the Javascript Error Console, just look for it under "Tools" and look for "Error Console" or "Javascript Console" or something like that. I do use it frequently to debug javascript code on Chrome and Firefox.
Good Luck!
A short example:
Code:<div id="someElement">Hello</div>
<div id="anotherElement">World</div>
<script>
function setStyle(elementId, styleName, value) {
var element = document.getElementById(elementId);
element.style[styleName] = value;
}
setStyle('someElement', 'border', '2px solid blue');
setStyle('anotherElement', 'backgroundColor', 'red');
</script>