# javascript hide function with multiple div tags



## DaijoubuKun (Oct 20, 2005)

I seem to be having an issue with my javascript code. I'm not very good at it so please bear with me.
I have a checkbox that when unchecked it hides some div tags from view. I have this working if I only use one 'id' in the divs, however, I have the div tag in several parts of the script; so I know I need to use 'class' instead of 'id'

for example:

```
<div class="someclass" style='display:block;'>
stuff
</div>
some stuff
<div class="someclass" style='display:block;'>
more stuff
</div>
```
I put the style tags in there to work better with the javascript so you don't have to click the box several times before the divs actually hide.

the javascript I used before was called from onClick="toggle_boxes(turnon);"

```
function toggle_boxes(turnon)
{
	var e = document.getElementById(turnon);
	if(e.style.display == 'none')
		e.style.display = 'block';
	else
		e.style.display = 'none';
}
```
I know that I can't use the document.getElementById() in this case since you can't use more than one in a page. I'm unable to simply change it to 'document.getElementByClass()' and get it to work (like my google searches have shown)

I have also taken the time to ensure all variables have matched up and if I change 'Class' to 'id' the first div disappears when the box is unchecked (it's checked by default) How in the world do I fix this?


----------



## JimmySeal (Sep 25, 2007)

Modifying the style of a CSS class is a bit trickier than modifying the style of a single element. Here is a page that provides a script to do this easily enough.

You could also try an approach like in the attached file, which is to modify the style of all elements with a certain class (change the extension from .txt to .html).


----------



## DaijoubuKun (Oct 20, 2005)

AWESOME! I finally get it. I'm not very experienced with javascript, but I had to use it for this project I'm working on. I finally understand that code you posted. Thank you very much!!!


----------

