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
    New Coder
    Join Date
    Feb 2008
    Posts
    54
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Shorthand for list of id tags?

    As is in order to trigger the text turning back to black it seems I need to put the lines like below one by one for all the id's. I'm wondering if there is a less lengthy way to get this done?

    // JavaScript Document
    function CheckTheForm() {

    var result = true;
    var msg="";

    document.getElementById('maincontactemail').style.color='#000000';
    document.getElementById('maincontact').style.color='#000000';

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Last edited by abduraooft; 07-25-2011 at 04:48 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    And of course this is a perfect application area for classes. Give your elements a class and set the properties (like color) in CSS

  • #4
    New Coder
    Join Date
    Feb 2008
    Posts
    54
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Thanks guys but this still doesn't help me. If I was that smart to make the connection from that seemingly not related link I wouldn't have posted here and would have figured it out already.

    Bottom line is I have seen it in js where there was a sequence I think... so that inside the main quotes there is a series of variables... I'm just not a guru js writer so wouldn't know how to do it. but I thought it'd be REAL slick if instead of like 20 lines for a long form you could put a sequence of the variables in a row something like this:

    document.getElementById("'maincontactemail',maincontact'").style.color='#000000';

    I'm sure that's wrong but hope u get the idea...
    Last edited by johnwboyd; 07-27-2011 at 06:56 AM.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    document.getElementById("'maincontactemail',maincontact'").style.color='#000000';

    I'm sure that's wrong but hope u get the idea...
    Variables and loops in any programming language are there for a reason! If you post your HTML, we may be able to give a better answer.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may use an array and a loop:
    Code:
    var id=['maincontactemail','maincontact','mainwhichever'];
    var div, i=0;
    while(div=document.getElementById(id[i++])){
    div.style.color='#000';
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by johnwboyd View Post
    As is in order to trigger the text turning back to black it seems I need to put the lines like below one by one for all the id's. I'm wondering if there is a less lengthy way to get this done?

    // JavaScript Document
    function CheckTheForm() {

    var result = true;
    var msg="";

    document.getElementById('maincontactemail').style.color='#000000';
    document.getElementById('maincontact').style.color='#000000';
    This might be what you are looking for ?

    Never use getElementById again.
    put this simple code in the onload function...

    Code:
    els = document.getElementsByTagName("*");
    for(var i = els.length;i--;)
      if(els[i].id)window[els[i].id]=els[i];
    or just put it at the bottom like this ...


    Code:
    <html>
    <head>
    <title>no more getElementById</title>
    </head>
    <body>
     
    <div id="main">
    <div id="sub1"></div>
    <div id="sub2"></div>
    <div id="sub3"></div>
    </div>
     <script>
    els = document.getElementsByTagName("*");
    for(var i = els.length;i--;)
      if(els[i].id)window[els[i].id]=els[i];
    
    sub1.innerHTML = "I am sub1";
    sub2.innerHTML = "I am sub2";
    sub3.innerHTML = "I am sub3";
    </script>
    </body>
    </html>

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by DaveyErwin View Post
    Never use getElementById again.
    Good idea. But not exactly like that. There is a waste of memory to search all the elements (document.getElementsByTagName('*')). It would be a better idea to group those elements within a common parent (and give it an id), and give them a common class. Now you can start by referring that parent, and subsequently to circle through is children which may a common class. Or tag name. Or common something.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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