Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assign variables from DIV collection.

    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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This should do the trick.

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

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you need to associate a variable with a div element why not:

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

    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by Jerome; 04-08-2003 at 06:46 PM.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What are you trying to do in the first place

    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
    Last edited by Vladdy; 04-08-2003 at 07:26 PM.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Forgot the leading "e", otherwise what I posted before should work

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

  • #8
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •