Forum Moderators: open

Message Too Old, No Replies

How to update object using object names from an array?

something simular to this: document.ar[0].value = "blah";

         

Vano

12:25 am on Sep 18, 2004 (gmt 0)

10+ Year Member



Hello!
I'm trying create a function that would update object on the page. The idea is to store object names and their values in arrays and then call the function that would update the objects.
here is a not working example of this idea:

<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

and
document.n is null or not an object

is there any way I can accomplish this?

Thank you.

Bernard Marx

11:21 am on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are getting very confused.
Plain talk, but true.

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]

You are simply assigning whatever's in
ar[0]
, at the time, to
n
.

Vano

4:28 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



yes...I know that, and that's why I said it doesnt work.
that was just an example of what I'm trying to accomplish.
using ar[0] directly or load it into an variable n was an example of two ways that I understand should work.

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);
?>

Bernard Marx

9:13 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aaah, sorry. Gottit now.

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.

Vano

9:28 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



thank you very much!
now I have extended question:
what about the .innerHTML part?
I mean, not all of my object being updated through .innerHTML, some of them have .value or maybe .checked
if I load all that info into three arrays:

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!

Bernard Marx

10:57 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer to that is in my previous.
Still..


document.getElementById(ids[a])[fields[b]] = values[c]

Vano

11:14 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



hmm...probably as u mentioned this method wont work in most browsers...I just cant get it to work in IE...
It sais "Object required". I even tryed add a dot infront of [values[0]] and then it sais "Expected identifier"
I've tryed all sort of combinations, no luck..

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>

Vano

11:24 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



woohoo!
Thank you very much!
its working now.
Apperiantly getElementById() function doesnt work outside or without <body></body> tags...I didnt know that.
Its working perfectly now, lets see if it works on other browsers :D