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!

accessing html classes via javascript

Status
Not open for further replies.

GabberGod

IS-IT--Management
Nov 16, 2003
73
AU

I have a class of html objects named sub_menus.

They are part of a drop down menu system. what i want to be able to do is
hide all menus in one command.

i.e

document.getElementById(sub_menu).style.visibility = 'visible';

Is it possible to do something like this with javascript??? Or will i need
to call each class object in turn by id to hide them???
 
you can use getElementById(), but that method is intended to return only one element, as an id should be unique on a page.

by "class of html objects" do you mean they all share a css class name?

i would suggest creating some sort of object to cache these elements so you can reference them at will.


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
yep thats what i was refering to css class name.

so your saying an array of element id's then call them in a loop?

isnt there a better way of doing???
 
>>isnt there a better way of doing???

An array of elements and referencing them is a loop seems a very, very, very good method.

<irony weight=&quot;heavy&quot;>The only better method I can think of is someone else writing it for me.</irony>
[smile]

________________________________________
[hippy]Roger J Coult; Grimsby, UK
In the game of life the dice have an odd number of sides.
 
I'm in a bit of a hurry to get to a workshop. But here is my initial take on a solution. The following is a funtion that scans all divs on the page and checks their class. If the class matches the required class, then it allows you to set something on the style of that div.

Code:
function flipmenu(_status)
{
  var linkArray = document.getElementsByTagName(&quot;div&quot;);
  for (var loop=0; loop<linkArray.length; loop++)
  {
    if (linkArray[loop].className == &quot;myClassName&quot;)
    {
      linkArray[loop].style.visibility = _status;
    }
  }
}

Example of use:
Code:
flipmenu('hidden');
would hide all matching divs
Code:
flipmenu('visible');
would show all matching divs

I have not tested the code and it should serve only as a guide. Maybe someone else can take it further and provide a fully working example?

Jeff
 
Jeff
Looks good to me...
[tt]<irony weight=&quot;very heavy&quot;>[/tt]Oh look, GabberGod, there was a better way.[tt]</irony>[/tt]. [smile]

________________________________________
[hippy]Roger J Coult; Grimsby, UK
In the game of life the dice have an odd number of sides.
 
You could loop through the stylesheet itself you know.

Code:
for( var i=0; i < document.styleSheets[ 0 ].rules.length; i++ ){
  if ( document.styleSheets[ 0 ].rules( i ).selectorText == '.sub_menus' ) {

  // Do whatever
  ...

It gets nasty in Mozilla IIRC though so probably best to stick with the elements loop. Might be worth you looking at though.
 
thanks guys

well accept for woja, your most unhelpful. why bother posting something if its not furthering the topic???

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top