Hide a specific div

A previous question was answered successfully when changing the visibility of a specific div but now I have changed the function so that it hides the div so that the div won't take up space. Unfortunately, now the function does not work.

Any help would be greatly appreciated!
<script type="text/javascript">
 
 var state1 = 'none';
 function showhide1(layer_ref) {
   state1 = document.getElementById(layer_ref).style.display ;
   state1 = state1 ==  'none'; ? 'block' : 'none';
   if (document.getElementById) { // any normal browser nowadays
      document.getElementById(layer_ref).style.display =  state1;
   } else if (document.all) { //IS IE 4 or 5 (or 6 beta)
      eval( "document.all." + layer_ref + ".style.display =  state1");
   } else if (document.layers) { //IS NETSCAPE 4 or below
      document.layers[layer_ref].style.display =  state1;
   }
}
</script>
 
<a href="javascript:;" onclick="showhide1('Content_<%=rsComments("MsgID")%>');"><%=rsComments("MsgHeader")%></a>
 
 
 <div id="Content_<%=rsComments("MsgID")%>" style="display:none;">
   							<%=rsComments("MsgContent")%>
							</div>

Open in new window

gilaligAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

WysGCommented:
Line 6 reads
state1 = state1 ==  'none'; ? 'block' : 'none';
Notice the ; after 'none'

Should be
state1 = state1 ==  'none' ? 'block' : 'none';

Or clearer
state1 = (state1 ==  'none' ? 'block' : 'none');

0
David S.Commented:
You've got an errant ";" in this line:

   state1 = state1 ==  'none'; ? 'block' : 'none';

Also NS4 doesn't support the style object. Besides, branching for IE4 and NS4 these days is rather silly.

Try this:

function showhide1(layer_ref) {
   if(!document.getElementById) return;
   var s = document.getElementById(layer_ref).style;
   s.display = (s.display == 'none') ? 'block' : 'none';
}

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
Göran AnderssonCommented:
As you are getting the current value from the element instead of the variable, you should skip the global variable and keep it local in the function. Any global variable is a potential conflict with other scripts.

Netscape 4 can not remove elements from the flow, as it doesn't reflow the layout. So, you have to drop the support for Netscape 4. You can keep the support for IE5, if you like.

In order for the IE5 support to work, you have to get the style object from the element first, so that you can use that both for getting and setting the value:
<script type="text/javascript">
 
function showhide1(layer_ref) {
   var s = null;
   if (document.getElementById) { // any normal browser nowadays
      s = document.getElementById(layer_ref).style;
   } else if (document.all) { //IS IE 4 or 5 (or 6 beta)
      s = document.all(layer_ref).style;
   }
   if (s != null) {
      s.display = (s.display == 'none' ? 'block' : 'none');
   }
}
</script>

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Göran AnderssonCommented:
> you should skip the global variable and keep it local in the function

Well, as you see from the code I posted, you actually don't need a variable for that at all.
0
David S.Commented:
@GreenGhost

IE5+ support document.getElementById().
0
tommytcchanCommented:
you might want to install firebug as well to check that the id names are indeed what you want. firebug is a firefox add-on for debugging pages
0
Göran AnderssonCommented:
@Kravimir

Ok, then the comment in the original code was incorrect.
0
David S.Commented:
@GreenGhost

Yeah, it is incorrect.
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.