Forum Moderators: open
<span id="blah"></span>
<span id="blah2"></span>
<script>
var ar = new Array("blah", "blah2");
n = ar[0];
document.ar[0].innerHTML = "test";
document.n.innerHTML = "test2";
</script>
it gives me error message:
document.ar.0 is null or not an object
document.n is null or not an object
Thank you.
ar holds an array, n holds the string, "blah".
They have nothing to do with HTML, they are Javascript.
var ar = new Array("blah", "blah2");
ar[0] = "test"
alert(ar[0])
when you do this:
n = ar[0]
ar[0], at the time, to n.
if its helps, in php I could do it this way: <?php
$test = "";
$variable = "test";
$$variable = "This will be load into test variable";
print($test);
?>
I don't recommend the referencing method at all, but purely for a demo of syntax for this sort of thing:
document[ar[0]].innerHTML = "test";
document[n].innerHTML = "test2";
In JS,
object.property === object["property"]
So that's how you fill in arbitrary string values for 'dot' properties.
But your element referencing won't work in most browsers. The correct modern referencing method would be:
document.getElementById(ar[0]).innerHTML = "test";
In the example, you could store the element references directly in the array from the start.
var names = new Array("name1", "name2");
var values = new Array("val1", "val2");
var field = new Array("innerHTML", "value");
how can I use it?
Thanks again!
this is the code I've tested on:
<span id="blah"></span>
<span id="blah2"></span>
<script>
var names = new Array("blah", "blah2");
var values = new Array("test", "test2");
var fields = new Array("innerHTML", "innerHTML");
document.getElementById(names[0])[fields[0]] = values[0];
</script>