...

View Full Version : Shorthand for list of id tags?



johnwboyd
07-25-2011, 04:54 PM
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';

abduraooft
07-25-2011, 05:41 PM
Have a look into https://developer.mozilla.org/En/DOM/Node.childNodes and https://developer.mozilla.org/en/DOM/document.getElementsByTagName

devnull69
07-25-2011, 09:27 PM
And of course this is a perfect application area for classes. Give your elements a class and set the properties (like color) in CSS

johnwboyd
07-27-2011, 07:52 AM
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...

abduraooft
07-27-2011, 08:58 AM
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.

Kor
07-27-2011, 04:36 PM
You may use an array and a loop:


var id=['maincontactemail','maincontact','mainwhichever'];
var div, i=0;
while(div=document.getElementById(id[i++])){
div.style.color='#000';
}

DaveyErwin
07-27-2011, 06:42 PM
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...


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 ...



<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>

Kor
07-27-2011, 08:45 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum