473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to Refrence to HTML Object

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.

Apr 30 '07 #1
5 6376
On Apr 30, 4:03 pm, vunet...@gmail.com wrote:
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

Apr 30 '07 #2
vu******@gmail.com wrote:
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[i].id)
{
arrayOfIDs[ps[i].id] = document.getElementById(ps[i].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).

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 1 '07 #3
-Lost wrote:
vu******@gmail.com wrote:
>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 = '...';

>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:
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[i].id)
{
arrayOfIDs[ps[i].id] = document.getElementById(ps[i].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[i].id) arrayOfIds.push(ps[i].id);

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

if (ps[i].id) arrayOfEls.push(ps[i]);
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[i].id) obj[ps[i].id] = ps[i];
I doubt that this method offers any speed advantages, however it might
be preferable for other reasons.
--
Rob
May 1 '07 #4
On Apr 30, 10:16 pm, -Lost <maventheextrawo...@techie.comwrote:
vunet...@gmail.com wrote:
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[i].id)
{
arrayOfIDs[ps[i].id] = document.getElementById(ps[i].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).

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
That's what I want. Thanks. Let me think of it.

May 1 '07 #5
On May 1, 4:50 am, RobG <r...@iinet.net.auwrote:
-Lost wrote:
vunet...@gmail.com wrote:
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 = '...';
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:

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[i].id)
{
arrayOfIDs[ps[i].id] = document.getElementById(ps[i].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[i].id) arrayOfIds.push(ps[i].id);

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

if (ps[i].id) arrayOfEls.push(ps[i]);

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[i].id) obj[ps[i].id] = ps[i];

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

--
Rob
Thank you.

May 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Phillips | last post by:
Hello everyone, I have read the section on templates in The C++ Programming Language 3rd edition 3 times over the last 4 days. Still I am stumped. I'll explain a bit about what I am doing...
6
by: A.M | last post by:
Hi, Ho can I send a refrence type by value as a method parameter. Thanks, Alan
2
by: | last post by:
i have a form name Form1 i want to create a copy of Form2 which know th refrence of Form1. example: private: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { Form2...
1
by: Mike John | last post by:
I have created checkbox in the aspx form (html) not the design. Syntax:<input type =checkbox name ="mycheckbox"> now how can I read i's value from aspx. I have type Me which stand for the...
5
by: KraftDiner | last post by:
I understand that everything in python is a refrence.... I have a small problem.. I have a list and want to make a copy of it and add an element to the end of the new list, but keep the...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
5
by: giddy | last post by:
hi , i'm a C / C# programmer .. have'nt done C++, in C# .. . object instances of classes ( TextBox txt = new TextBox();) are reference types. Structs on the other hand are value types. In...
5
eragon
by: eragon | last post by:
I am making a password script, i have most of it done, ill show you, but i hit a brick wall.... lol, I have it so when you log in it takes you to your main page, that works, but when you enter the...
3
by: preitymathur0422 | last post by:
I m working on a MDI application. Here on the parent form I had two variables declared as public int curUserID = 0, ViewUserID = 1; the form is inherited by the "Form" class. I m using these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.