Top

Javascript Visibility Toggle


Javascript Code
+ 1 likes
Please Register to submit score.
Bookmark and Share
Average Score  0.0 (of 0 scores)
Date Added  Sep 10, 2008
Last Updated  Sep 10, 2008
Tags  javascript  toggle  visibility 

Description

This is a very simple Javascript toggle that'll show or hide a div depending on its current state. Here is the HTML code:
Code:
<div onmouseover="toggle('mydiv');" onmouseout="toggle('mydiv');">Hover</div>
<div id='mydiv' style="visibility: hidden;">I see you!</div>

Grab the Code

function toggle(obj)
{
	var item = document.getElementById(obj);
	if(item.style.visibility == 'visible') { item.style.visibility = 'hidden'; }
	else { item.style.visibility = 'visible'; }
}

Comments

  (14)  RSS
vaseline28
Comments: 162
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 11, 2008 12:31 pm
Nice work, interesting the way you chose to add the document.getELementById to a variable, then seperate for the styles, rather than assign the style to a variable and do:
document.getElementById(obj).style.visibility = style;
But works nicely :-)
Hawkee
Comments: 1,092
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 11, 2008 12:36 pm
Thanks, I think it just helps make it more clear and reduces the overall number of characters used.
vaseline28
Comments: 162
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 11, 2008 12:43 pm
Also, the 'var' before the variable name is not necessary, although there's no problem having it, I guess different people prefer doing things differently, it's definitely clearer having it there - but longer.
Hawkee
Comments: 1,092
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 11, 2008 12:51 pm
Actually, you need the 'var' because IE errors out if you don't use it. I struggled with this in the past only to realize I had forgotten a var declaration.
vaseline28
Comments: 162
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 11, 2008 1:19 pm
Really? That's odd, several of my scripts have worked without it. I'll bear that in mind when creating more scripts, thanks!
Hawkee
Comments: 1,092
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 11, 2008 1:33 pm
Then I'm not sure why it made a difference for me, maybe its the version of IE I'm using.
vaseline28
Comments: 162
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 11, 2008 1:55 pm
Which one is that? And I guess it's best to add it in anyway, so as to cater for all versions.
DiGiTaL
Comments: 26
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 13, 2008 8:29 am
Your changing the visibility attribute, basically your saying if the visibility is OFF, keep that space but dont show it, if its on USING That space show it..

Visibility is not recommended it. change it to 'display'
Hawkee
Comments: 1,092
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 13, 2008 11:51 am
Yes, the point is to keep the space just as we've done in the site menu above when you hover over a link and the drop down arrow appears.
DiGiTaL
Comments: 26
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 13, 2008 2:06 pm
You can also space some lines and change line 4-5 to:

item.style.visibility = (item.style.visibility == 'visible') ? 'hidden' : 'visible';
Hawkee
Comments: 1,092
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Sep 13, 2008 4:16 pm
Good suggestion Digital, that's another way to do it.
Korvin
Comments: 421
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Mar 2, 2010 11:55 pm
jquery is much more efficient with this imo, since you have it, you should use it =p
Code:
<button>You cant see <div style='visibility:hidden;'>this</div></button>
<script>
$('button').hover(function(){jQuery(":first-child", this).css('visibility', 'visible');},function(){jQuery(":first-child", this).css('visibility', 'hidden');});</script>

The cool thing about this is the wide range of customization:
Code:
<table cellpadding=0 cellspacing=0><tr class='hov'><td>PLATFORMS</td><td style='width:30px; height:30px;'><img class='hidden' src='http://www.hawkee.com/images/bullet_arrow_down.png' /></td></tr></table>
OR
Code:
<div class='hov'>Hover<div class='hidden'>I see you!</div></div>

Code:
$(document).ready(function() {
   $('.hidden').hide();
   $('.hov').hover(function(){jQuery(".hidden",this).fadeIn();},function(){jQuery(".hidden",this).fadeOut();});
});
live example at http://left4quake.com/this.html
you'll find jquery is the best =]

outcome = unobtrusive lightweight effective dynamic code, also a really cool effect.
Hawkee
Comments: 1,092
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Mar 3, 2010 1:29 am
I've started to play around with jQuery a lot more lately and I'm very pleased with it. I'll likely be doing a lot more of the functionality here using it as I get around to it.
Korvin
Comments: 421
 
Javascript Snippet:  Javascript Visibility Toggle
Posted on Mar 3, 2010 9:35 am
if u need any help, i'm always willing =p

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  

Bottom