...

View Full Version : Assign variables from DIV collection.



Jerome
04-08-2003, 10:13 AM
Hi,

I have a collection of divs like:

<div id="abce"></div>
<div id="de3"></div>
<div id="kdgkh"></div>

with unique id's without any similarity in length or use of caracters.

On the moment I assign variables like:

var eabce=document.getElementById('abce');

In other words I just add an - e - in front of the div.id.name

Because of the ammount of divs used I like to have a routine for it, something like:

function func()
{
var divs=document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++)
{
var a=divs[i];
var b=divs[i].id;
var c=document.getElementById("a");
}
alert(a);
alert(b);
alert(c);
}

Any ideas?

Thanks for Your effort,
Jerome

Roy Sinclair
04-08-2003, 04:45 PM
This should do the trick.



function useALotOfGlobal()
{
var divs=document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++)
{
window[divs[i].id]=divs[i]
}
}

Vladdy
04-08-2003, 05:49 PM
If you need to associate a variable with a div element why not:

document.getElementById('divName').myVariable = someValue;

:thumbsup:

Roy Sinclair
04-08-2003, 07:23 PM
Vladdy,

I think he's trying to emulate the IE way of making everything available via the window object for browsers that aren't IE.

Jerome
04-08-2003, 07:41 PM
Hmmm,

Hard to explane, what I like is

function func()
{
var divs=document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++)
{

var e+divs[i].id=document.getElementById("div[i].id");

}
}
With one div like:

<div id="mydiv"></div>

The result needs to be:

var emydiv=document.getElementById('mydiv')

So in words: var e+div id = document.getElementById('div id');

And that ofcourse for all divs

Thanks gentlemen,
Jerome

Vladdy
04-08-2003, 08:09 PM
What are you trying to do in the first place :confused: :confused:

Roy, in this case why not run this onload:

alldivs = document.getElementsByTagName('div');
divs = new Array();
for(var i=0; i<alldivs.length; i++)
if(alldivs[i].id.length>0)
divs[alldivs[i].id] = alldivs[i];

Then all divs with id are accessible by
divs[id]

It still makes no sense when you can achieve the same by
document.getElementById('id')

... seems like a bad case of ignoring KISS principle :D :D :D

Roy Sinclair
04-08-2003, 09:34 PM
Forgot the leading "e", otherwise what I posted before should work



function useALotOfGlobal()
{
var divs=document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++)
{
window["e" + divs[i].id]=divs[i]
}
}

Jerome
04-10-2003, 11:50 AM
Thanks for the reactions!

I implemented the solution from Roy (which I more or less understand), however I have some questions:

First off all let's say I have a div with id="mydiv"

When i alert like

alert(mydiv) - it says "[object]" (i understand)
alert(mydiv.id) - it says "mydiv" (i understand)
alert(mydiv.innerHTML) - i get the innerHTML (works also fine)

However when I do:

mydiv.style.display="block";
or
mydiv.id.style.display="block";

I get an alert saying this is "no object"

With the old methode it's working fine:
document.getElementById('mydiv').style.display="block";

Q1.
What do i do wrong?

Q2.
Is it an overkill to have a lot off global variables?

Q3.
When no at Q3
Is it possible to have:

var hmydiv=mydiv.innerHTML
var smydiv=mydiv.style
And that ofcourse for all divs

Thanks for Your advice!
Jerome



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum