SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I set a single value to multiple variables without using arrays?

    hey,

    i'm using a javascript script to disable form elements depending on conditions. part of the script requires a div tag with a unique id to surround the elements that are to be disabled. because of the way this works, each small group of elements needs its own div tag and hence, its own id.

    so this is how it breaks down:

    disableDiv.style.color ='black';
    disableDiv2.style.color ='black';
    disableDiv3.style.color ='black';
    disableDiv4.style.color ='black';
    disableDiv5.style.color ='black';
    disableDiv6.style.color ='black';

    as you can see, these div ids have a text color attribute of 'black'. i have to set 6 of these variables to the same value, which is annoying to say the least when doing it manually. i can not use an array because the div id's can not be arrays.

    so the question is, how can i use a for loop to set the same value for all of these div id's?

    i was thinking something along the lines of this:

    for (var x = 0; x <= 2; x++)
    {
    (disableDiv + x) ='black'
    }

    i don't know javascript very well so i'm not sure what to change to get it to work.

  2. #2
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, this is the reason i want to do this:


    disableDiv.style.color ='black'; disableDiv2.style.color ='black'; disableDiv3.style.color ='black'; disableDiv4.style.color ='black'; disableDiv5.style.color ='black'; disableDiv6.style.color ='black'; disableDiv7.style.color ='black'; disableDiv8.style.color ='black'; disableDiv9.style.color ='black'; disableDiv10.style.color ='black'; disableDiv11.style.color ='black'; disableDiv12.style.color ='black'; disableDiv13.style.color ='black'; disableDiv14.style.color ='black'; disableDiv15.style.color ='black'; disableDiv16.style.color ='black'; disableDiv17.style.color ='black'; disableDiv18.style.color ='black';
    disableDiv19.style.color ='black'; disableDiv20.style.color ='black'; disableDiv21.style.color ='black'; disableDiv22.style.color ='black'; disableDiv23.style.color ='black'; disableDiv24.style.color ='black';

    that mess is what i have in my code currently, and i'm probably going to have to add about 10 more.

    to repeat, i can not use arrays because the disableDiv is used for an id in a div tag in html.

  3. #3
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what eval is for:

    for (var x = 0; x <= 2; x++)
    {
    eval("disableDiv" + x + ".style.color = 'black' ");
    }

  4. #4
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Anarchos
    This is what eval is for:

    for (var x = 0; x <= 2; x++)
    {
    eval("disableDiv" + x + ".style.color = 'black' ");
    }
    you rule man, thanks.


Bookmarks

Posting Permissions

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