Skip to main content
Home / Code Snippets / JavaScript / Toggle (Show/Hide) Element

Toggle (Show/Hide) Element

<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>

Inline usage:

<a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
<div id="foo">This is foo</div>
icon-link icon-logo-star icon-search icon-star