www.webdeveloper.com
Results 1 to 2 of 2

Thread: hide/show elements in html using javascript

  1. #1
    Join Date
    May 2004
    Posts
    3

    hide/show elements in html using javascript

    all i want to do is hide/show any element like list in html using javascript. is it possible. if it is then is there some code for the same. that will be very useful. secondly is there any manual for javascript justlike there are manuals for java development kits ?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>show - hide</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    <!--
    onload=function() {
    var h3List=document.getElementsByTagName('h3');
    var ulList=document.getElementsByTagName('ul');
    for(var i=0; i<ulList.length; i++) {
    	h3List[i].style.cursor='pointer';
    	h3List[i].onclick=function() {toggle(this);}
        ulList[i].style.display='none';
        }
    }
    
    function toggle(obj) {
    var ulList=document.getElementsByTagName('ul');
    for(var i=0; i<ulList.length; i++) {
        ulList[i].style.display='none';
        }
    var sibling;
    if(obj.nextSibling.nodeType==3) {
        sibling=obj.nextSibling.nextSibling;
        }
    else {
        sibling=obj.nextSibling;
        }
    sibling.style.display=(sibling.style.display=='block')? 'none' : 'block';
    }
    //-->
    </script>
    </head>
    <body>
    <h3>player 1</h3>
    <ul>
        <li>age:</li>
        <li>weight:</li>
    </ul>
    <h3>player 2</h3>
    <ul>
        <li>age:</li>
        <li>weight:</li>
    </ul>
    <h3>player 3</h3>
    <ul>
        <li>age:</li>
        <li>weight:</li>
    </ul>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles