How to Refrence to HTML Object

V

vunet.us

I have array elements referenced to HTML objects:

myArr[0] = document.getElementById("myDiv1");
myArr[1] = document.getElementById("myDiv2");

How do I reference to these HTML objects through array using object's
properties to make something like:

myArr["some_proprty_here"].innerHTML = "div content changed"

I know this can be done but I do not see how.
 
V

vunet.us

I have array elements referenced to HTML objects:

myArr[0] = document.getElementById("myDiv1");
myArr[1] = document.getElementById("myDiv2");

How do I reference to these HTML objects through array using object's
properties to make something like:

myArr["some_proprty_here"].innerHTML = "div content changed"

I know this can be done but I do not see how.

this works in a similar case:
var JeansAge = array["Jeans"].age;

so I need the same for my case to reference to HTML div object through
the name or id.

Thank you
 
L

-Lost

I have array elements referenced to HTML objects:

myArr[0] = document.getElementById("myDiv1");
myArr[1] = document.getElementById("myDiv2");

How do I reference to these HTML objects through array using object's
properties to make something like:

myArr["some_proprty_here"].innerHTML = "div content changed"

I know this can be done but I do not see how.

Create a function to store all the IDs that you want into an array
indexed by the IDs you fetched. Something like:

var arrayOfIDs = [], ps = document.getElementsByTagName('p');
for (var i = 0; i < ps.length; i++)
{
if (ps.id)
{
arrayOfIDs[ps.id] = document.getElementById(ps.id);
}
}

Then you could use:

arrayOfIDs['validID'].innerHTML = 'whatever';

Is this what you are thinking about?

I make no claims as to the efficiency of this code (untested).
 
R

RobG

-Lost said:
I have array elements referenced to HTML objects:

myArr[0] = document.getElementById("myDiv1");
myArr[1] = document.getElementById("myDiv2");

How do I reference to these HTML objects through array using object's
properties to make something like:

myArr["some_proprty_here"].innerHTML = "div content changed"

If you are using an Array as a plain object, then it is much better to
use an Object:

var obj = {};
obj[propertyName].innerHTML = '...';

Create a function to store all the IDs that you want into an array
indexed by the IDs you fetched. Something like:

I think it would be more efficient to use an object of references.

var arrayOfIDs = [], ps = document.getElementsByTagName('p');
for (var i = 0; i < ps.length; i++)
{
if (ps.id)
{
arrayOfIDs[ps.id] = document.getElementById(ps.id);


It doesn't seem to be a good idea to sift through all the elements to
find the ones that have an ID, then use it's ID property with
getElementById to return the same object so it can be stored in an array.

If the intention is to store just the id, then:

if (ps.id) arrayOfIds.push(ps.id);

will do the trick. If the intention is to store an array of element
references, then:

if (ps.id) arrayOfEls.push(ps);


If the intention is to have an object with property names that are the
same as the element IDs and have their values reference the matching
elements, then:

var obj = {};
if (ps.id) obj[ps.id] = ps;


I doubt that this method offers any speed advantages, however it might
be preferable for other reasons.
 
V

vunet.us

I have array elements referenced to HTML objects:
myArr[0] = document.getElementById("myDiv1");
myArr[1] = document.getElementById("myDiv2");
How do I reference to these HTML objects through array using object's
properties to make something like:
myArr["some_proprty_here"].innerHTML = "div content changed"
I know this can be done but I do not see how.

Create a function to store all the IDs that you want into an array
indexed by the IDs you fetched. Something like:

var arrayOfIDs = [], ps = document.getElementsByTagName('p');
for (var i = 0; i < ps.length; i++)
{
if (ps.id)
{
arrayOfIDs[ps.id] = document.getElementById(ps.id);
}

}

Then you could use:

arrayOfIDs['validID'].innerHTML = 'whatever';

Is this what you are thinking about?

I make no claims as to the efficiency of this code (untested).


That's what I want. Thanks. Let me think of it.
 
V

vunet.us

-Lost said:
I have array elements referenced to HTML objects:
myArr[0] = document.getElementById("myDiv1");
myArr[1] = document.getElementById("myDiv2");
How do I reference to these HTML objects through array using object's
properties to make something like:
myArr["some_proprty_here"].innerHTML = "div content changed"

If you are using an Array as a plain object, then it is much better to
use an Object:

var obj = {};
obj[propertyName].innerHTML = '...';
Create a function to store all the IDs that you want into an array
indexed by the IDs you fetched. Something like:

I think it would be more efficient to use an object of references.
var arrayOfIDs = [], ps = document.getElementsByTagName('p');
for (var i = 0; i < ps.length; i++)
{
if (ps.id)
{
arrayOfIDs[ps.id] = document.getElementById(ps.id);


It doesn't seem to be a good idea to sift through all the elements to
find the ones that have an ID, then use it's ID property with
getElementById to return the same object so it can be stored in an array.

If the intention is to store just the id, then:

if (ps.id) arrayOfIds.push(ps.id);

will do the trick. If the intention is to store an array of element
references, then:

if (ps.id) arrayOfEls.push(ps);

If the intention is to have an object with property names that are the
same as the element IDs and have their values reference the matching
elements, then:

var obj = {};
if (ps.id) obj[ps.id] = ps;

I doubt that this method offers any speed advantages, however it might
be preferable for other reasons.


Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top