Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Naming/Finding a group of tags

Status
Not open for further replies.

jacobster

Technical User
Mar 4, 2005
2
US
I have a page with a dozen or so tables on it and I want to be able to manipulate subsets of those tables with a javascript (e.g. change the background color of tableSubSet1 to yellow). Is there any way to "name" them so that I can pull them into a script and alter their properties? I need to use this function across multiple pages, so I don't want to give individual IDs to every table. Any ideas?

P.S. If anyone can convince the W3C to add getElementsByClassName, it would make my life so much easier.
 
You could always fake getElementsByClassName:
Code:
function getElementsByClassName(a) {mArr = new Array();d = document.getElementsByTagName('*');for (x=0;x<d.length;x++){if (d[x].className == a){mArr[mArr.length]=d[x];}}return mArr;};
then just call it:
Code:
getElementsByClassName("className");
it will return an array of the elements with the classname you specify.

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
The point of giving them class names is so you can use a stylesheet to alter the styling. Therefore why not change the stylesheet:
Code:
document.stylesheet[0].addRule("myclass", "background-color: blue;", 0)
 
Thanks guys,
Jonty, I think that might be the simplest (should have thought of that myself). Elegidito, for future reference, how much time would that add to a page load. Cycling through every element seems fairly involved (but useful in the right situstions)..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top