toggle visibility for a given css class name

hi EE,
is it possible to toggle the visibility of a unique class name?

the cms we use wont allow the html content to have css ids in it (arghh)

so this

<div id="test">...

will be rendered as

<div>...

but css classes are okay, i.e. this content:

<div class="test">...

will remain in the rendered html, i.e. the code will show:

<div class="test">...


so I need the following logic to control the visibility using javascript:

function test(classname)
if(document.GetTheClassObject['className'].style.display == 'block') {
  className.style.display = 'none'
}
//etc


note that I have created a unique class name for each item i need to control the visibility with.

many thanks
jimbona27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott BennettManager TechnologyCommented:
you would do it like this:
<style type="text/css">
<!--
.theStyle {
 
	width: 150px;
	height: 150px;
	visibility: hidden;
	background-color:#CCCCCC;
}
-->
</style>
 
<script language="JavaScript">
	function makeVis(theStyle) {
		var myclass = new RegExp('\\b'+theStyle+'\\b');
		var elem = document.getElementsByTagName('*');
		for (var i = 0; i < elem.length; i++) {
			if (myclass.test(elem[i].className)){
				elem[i].style.visibility = "visible";
			}
		}
	}
 
</script>
 
<div class="theStyle"> The First Div </div>
<div class="theStyle"> The Second Div </div>
 
<a href="javascript:makeVis('theStyle');">Make Them Visible</a>

Open in new window

0
Scott BennettManager TechnologyCommented:
Here it is again but showing the two divs with different style classes
<style type="text/css">
<!--
.numberOne {
 
	width: 150px;
	height: 150px;
	visibility: hidden;
	background-color:#CCCCCC;
}
.numberTwo {
 
	width: 150px;
	height: 150px;
	visibility: hidden;
	background-color:#CCCCCC;
}
-->
</style>
 
<script language="JavaScript">
	function makeVis(theStyle) {
		var myclass = new RegExp('\\b'+theStyle+'\\b');
		var elem = document.getElementsByTagName('*');
		for (var i = 0; i < elem.length; i++) {
			if (myclass.test(elem[i].className)){
				elem[i].style.visibility = "visible";
			}
		}
	}
 
</script>
 
<div class="numberOne"> The First Div </div>
<div class="numberTwo"> The Second Div </div>
 
<a href="javascript:makeVis('numberOne');">Make One Visible</a> | <a href="javascript:makeVis('numberTwo');">Make Two Visible</a>

Open in new window

0
jimbona27Author Commented:
ok great, this looks quite promising although I have several divs, i.e.

<div class="test1">
</div>
<div class="test2">
</div>
<div class="test3">
</div>
<div class="test4">
</div>
<div class="test5">
</div>


the problem I have is that I need to loop through any that are currently visible, only 1 at a time and hide any that might be toggled as display.

my attempts have produced some weird results using the following code

any ideas how this can work?
thanks


function makeVis(theStyle) {
        var myclass = new RegExp('\\b'+theStyle+'\\b');
        var elem = document.getElementsByTagName('*');
        
        for (var i = 0; i < elem.length; i++) {
             
                if (myclass.test(elem[i].className)) {                        
 
                        // hide any others that maybe visible
                        for(counter = 0; counter < elem.length; counter++) {
                             elem[counter].style.display = "none";
                        }
 
                        if(elem[i].style.display == "block") {
                            elem[i].style.display = "none";
                        }
                        else {
                            elem[i].style.display = "block";
                        }
                }
        }
}

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

jimbona27Author Commented:
i think the code is manipulating the wrong divs as well.

i've tried the following segment but it fails saying the search is not a function



                if (myclass.test(elem[i].className)){
                        
                        // hide any others that maybe visible
                        for(counter = 0; counter < elem.length; counter++) {
                            var div_name = elem[counter];
                            if(div_name.search('test') != -1) {                                
                                elem[counter].style.display = "none";
                            }
                        }
                        
                        
                        if(elem[i].style.display == "block") {
                            elem[i].style.display = "none";
                        }
                        else {
                            elem[i].style.display = "block";
                        }
                }

Open in new window

0
jimbona27Author Commented:
my logic is trying to say,
look for the divs that have the word test within the class name for the div, and then make them invisible if the div class name has 'test' within it.

is that right!  please advise, many thanks
0
Scott BennettManager TechnologyCommented:
something like this then?
<style type="text/css">
<!--
.numberOne {
 
        width: 150px;
        height: 150px;
        visibility: hidden;
        background-color:#CCCCCC;
}
.numberTwo {
 
        width: 150px;
        height: 150px;
        visibility: hidden;
        background-color:#CCCCCC;
}
-->
</style>
 
<script language="JavaScript">
        function makeVis(theStyle) {
                var myclass = new RegExp('\\b'+theStyle+'\\b');
                var elem = document.getElementsByTagName('*');
                for (var i = 0; i < elem.length; i++) {
                        if (myclass.test(elem[i].className)){
                                elem[i].style.visibility = "visible";
                        }
						else if (elem[i].className.search('number') != -1){
							elem[i].style.visibility = "hidden";
						}
                }
        }
 
</script>
 
 
<div class="numberOne"> The First Div </div>
<div class="numberTwo"> The Second Div </div>
 
<a href="javascript:makeVis('numberOne');">Make One Visible</a> | <a href="javascript:makeVis('numberTwo');">Make Two Visible</a>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimbona27Author Commented:
thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.