DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Chara has posted 50 posts at DZone. View Full User Profile

Toggling Elements with JavaScript

  • submit to reddit
    <script type="text/javascript">
/* Source: */
    function toggle(id, id2){  
        var toggle_one = document.getElementById(id);  
        var toggle_two = document.getElementById(id2);  
        if( == "block"){  
   = "none";  
            toggle_two.innerHTML = "Show";  
   = "block";  
            toggle_two.innerHTML = "Hide";  
    <a href="#" id="tlink" onclick="toggle('comments', 'tlink');">Show</a> Comments<br><br>  
    <div id="comments" style="display:none;padding 10px;">Now you can see the comments</div>

  This function allows you to pass two variables that represent ID of two elements on your page. What you click on one of them it hide it and shows another one.