Forum Moderators: open

Message Too Old, No Replies

Using JS to access the class attribute

Can it be done somehow?

         

Wertigon

12:45 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



Well, I'm now working on a table-layout based page, which in turn have multiple "real" tables on (next to) every page. The site is large, information-driven, with 5000+ HTML documents. The number of tables varies from page to page, and so does the number of tables in the menu.

The HTML-code is a nightmare to work with, but I'm not allowed to change it around. So the only thing I can do is to give the "real" tables a class. Actually, they already have that class.

Now, the people in charge wants to add JS-driven sorting of the tables for *all* the informational tables. Trouble is, such a script would potentially mess up the layout, which wouldn't be a good thing. However, if I could somehow get the class attribute, it'd be a piece of cake. Going around 5000+ pages adding the proper tables an id each is just not doable, especially since some of those pages have multiple tables.

But when I tried to find a way to do it, I got stumped. As far as I can see, the class element doesn't exist as far as the DOM is concerned. So is there anyone out there that knows how to access the class attribute through the DOM? ^^;

BlobFisk

1:32 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to build an array of tags and find the class.

var alltags = new Array();
function doSomethingWithAClass(image_dir) {
var inc=0;
var alltags=document.getElementsByTagName("TABLE");
for (i=0; i<alltags.length; i++) {
if (alltags[i].className=='tableClass') {
alltags[i].style.border='5px solid red';
}
}
}

If you want to include more than just tables, then you can change the getElementsByTagName to * which will look at all the tags on a page.

Remember that there is an overhead with this as the JS needs to build the array and cycle through it performing the for conditional arguements.

HTH