Jump to content

Hiding and Displaying DIV help


Darkmatter5

Recommended Posts

Here's my code:

<style type="text/css"> 
#site_admin, #user_admin, #news_man 
{ 
display: none; 
} 
</style>
<script type="text/javascript">
function toggle(thediv) {
    document.getElementById("site_admin").style.display = "none";
    document.getElementById("user_admin").style.display = "none";
    document.getElementById("news_man").style.display = "none";
    document.getElementById(thediv).style.display = "block";
}
</script>
<a href="" onClick="toggle('site_admin');" />Site Administration</a> | <a href="" onClick="toggle('user_admin');" />User Administration</a> | <a href="" onClick="toggle('news_man');" />News Manager</a>
<input type="radio" name="toggledivs" onclick="toggle('site_admin');" />Site Admin<br>
<input type="radio" name="toggledivs" onclick="toggle('user_admin');" />User Admin<br>
<input type="radio" name="toggledivs" onclick="toggle('news_man');" />News Man<br>

 

Now the radio buttons work fine, but the href's don't.  They'll display the div if clicked, but after a second it'll disappear again, how do I make it say there until another link is clicked?

Link to comment
Share on other sites

I created a version of the hide and show, let me know if this helps

<html>
<head>
	<title>Display/Hide Content</title>
	<style type="text/css">
		#ContentToDisplay {
			display: none;
		}
	</style>
	<script type="text/javascript" language="Javascript">
		function showContent(idAlternate) {
			if(document.getElementById(idAlternate).style.display == 'block') {
				document.getElementById(idAlternate).style.display = 'none';
			} else if(document.getElementById(idAlternate).style.display = 'none') {
				document.getElementById(idAlternate).style.display = 'block';
			}
		}
	</script>
</head>
<body>
	<a href="#" onclick="javascript:showContent('ContentToDisplay');">Show/Hide Div</a>
	<div id="ContentToDisplay">
		<p>This is the Hidden Content</p>
	</div>
</body>
</html>

Link to comment
Share on other sites

      <script type="text/javascript" language="Javascript">
         function showContent(idAlternate) {
            var hideShow = document.getElementById(idAlternate);
            if(hideShow.style.display == 'block') {
               hideShow.style.display = 'none';
            } else if(hideShow.style.display == 'none') { // added another = here it should be ==
               hideShow.style.display = 'block';
            }
         }
      </script>

 

Added an extra = to the elseif and create a var to house the document element, this way you do not have to keep calling that, just the variable.

 

I have not clue if the below would break it but I never use javascript:

      <a href="#" onclick="showContent('ContentToDisplay'); return false;">Show/Hide Div</a>

 

That should work, untested but yea. I would suggest getting FireFox as it has a "Error Console" that will post you all the javascript errors, if there are any, your page has. Great for debugging javascript.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.