Results 1 to 9 of 9

Thread: Toggling The Visibility of Divs of Classes

  1. #1
    Join Date
    Jun 2008
    Location
    Norwich, UK
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Toggling The Visibility of Divs of Classes

    I'm new to javascript, I can understand it but when it comes to writing it I'm pretty useless.

    Basically what I'm looking to do is toggle the visibility of divs with a set class within the page. I'm damn sure its possible but I just can't execute it.

    This is the toggling script I've nicked from: http://blog.movalog.com/a/javascript-toggle-visibility/

    HTML Code:
    <a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
    <div id="foo">This is foo</div>
    Code:
    <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>
    And instead of the getElementById(id) I want to getElementByClassName(

    using this script: http://www.robertnyman.com/2008/05/2...ame-anno-2008/

    Basically I need help integrating one into the other. I've tried anything my newbie brain can perceive.

    Your help will be greatly appreciated.
    Last edited by BlueStunt; 06-03-2008 at 07:11 PM. Reason: Making it more comprehendable

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    1st thing... change the id= to class=

    Code:
    <a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
    <div class="foo">This is foo</div>
    2nd thing... include the getElementsByClassName .js file in the head
    Code:
    <script type="text/javascript" src="getElementsByClassName.js"></script>
    3rd... change the toggle_visibility function to use getElementsbyClassName

    All together:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Show/Hidetle>
    <style type="text/css">
    .foo { display:none };
    </style>
    <script type="text/javascript" src="getElementsByClassName.js"></script>
    <script type="text/javascript">
    <!--
        function toggle_visibility(className) {
           var e = getElementsByClassName(className);
    	   for ( var i=0, len=e.length; i<len; ++i ){
    			if(e[i].style.display == 'block')
    				e[i].style.display = 'none';
    			else
    				e[i].style.display = 'block';
    		}
        }
    //-->
    </script>
    </head>
    <body>
    
    <a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
    <div class="foo">This is foo</div>
    
    
    </body>
    </html>

  3. The Following User Says Thank You to Medyman For This Useful Post:

    BlueStunt (06-03-2008)

  4. #3
    Join Date
    Jun 2008
    Location
    Norwich, UK
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Medyman View Post
    1st thing... change the id= to class=

    Code:
    <a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
    <div class="foo">This is foo</div>
    2nd thing... include the getElementsByClassName .js file in the head
    Code:
    <script type="text/javascript" src="getElementsByClassName.js"></script>
    3rd... change the toggle_visibility function to use getElementsbyClassName

    All together:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Show/Hidetle>
    <style type="text/css">
    .foo { display:none };
    </style>
    <script type="text/javascript" src="getElementsByClassName.js"></script>
    <script type="text/javascript">
    <!--
        function toggle_visibility(className) {
           var e = getElementsByClassName(className);
    	   for ( var i=0, len=e.length; i<len; ++i ){
    			if(e[i].style.display == 'block')
    				e[i].style.display = 'none';
    			else
    				e[i].style.display = 'block';
    		}
        }
    //-->
    </script>
    </head>
    <body>
    
    <a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
    <div class="foo">This is foo</div>
    
    
    </body>
    </html>
    Thanks for the help

    Can I just query what this little bit does:

    Code:
    var i=0, len=e.length; i<len; ++i

  5. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Can I just query what this little bit does:

    Code:
    var i=0, len=e.length; i<len; ++i
    It is a part of a looping construct, which is for loop. In the for loop you can start using a new variable, initialize, have a condition check, etc.

    Have a look at this page and read the section that describes for loop http://www.howtocreate.co.uk/tutoria...cript/controls

  6. #5
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    It is a part of a looping construct, which is for loop. In the for loop you can start using a new variable, initialize, have a condition check, etc.

    Have a look at this page and read the section that describes for loop http://www.howtocreate.co.uk/tutoria...cript/controls
    Just to add to this...

    The loop is necessary because the getElementsByClass functions return an array of all of the elements with that class. Even if it's only one element, it'll be in an array.

    So, you need to loop through the array to set properties to the divs contained within it.

  7. The Following User Says Thank You to Medyman For This Useful Post:

    BlueStunt (06-04-2008)

  8. #6
    Join Date
    Jun 2008
    Location
    Norwich, UK
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    I'm now trying to expand the function to change the background of a div with id the same as the class name I'm modifying. I assumed I'd be able to botch it together myself. I was wrong.


    This is one of my attempts, which i feel basically highlights what I'm attempting to do.

    Code:
    <script type="text/javascript" src="scripts/getElementsByClassName-1.0.1.js"></script>
    <script type="text/javascript">
    
        function toggle_visibility(className) {
           var e = getElementsByClassName(className),
           var f = document.getElementById(className);
           
    	   for ( var i=0, len=e.length; i<len; ++i ){
    			if(e[i].style.display == 'none')
          {
           e[i].style.display = 'block';
           f[i].style.background='url(graphics/*className*.gif';
          }
          else
          {
    	e[i].style.display = 'none';
    	f[i].style.background='none';
           }
        }
        }
    
    </script>
    However it only show's/hides a single post rather than multiple ones. And nothing changes with the id's background.

    Oh and another problem. When the 'else' is executes it takes two clicks on the button to work.

    Please help :/

  9. #7
    Join Date
    Dec 2004
    Posts
    177
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default

    First, forget the word id. Classes and id's are two separate beasts. The easy way to recall the difference? An id refers to ONE and only ONE object. A class can be a billion objects, if you were really that needy. :P

    Your current code:
    Code:
    <script type="text/javascript" src="scripts/getElementsByClassName-1.0.1.js"></script>
    <script type="text/javascript">
    
        function toggle_visibility(className) {
           var e = getElementsByClassName(className),
           var f = document.getElementById(className);
           
    	   for ( var i=0, len=e.length; i<len; ++i ){
    			if(e[i].style.display == 'none')
          {
           e[i].style.display = 'block';
           f[i].style.background='url(graphics/*className*.gif';
          }
          else
          {
    	e[i].style.display = 'none';
    	f[i].style.background='none';
           }
        }
        }
    
    </script>
    Basically, like I said above, you don't need f. An id does not exist here. I fixed the syntax, however, I'm not 100% sure if the background will work as intended. I'm not testing it at the moment.

    Code:
    <script type="text/javascript" src="scripts/getElementsByClassName-1.0.1.js"></script>
    <script type="text/javascript">
    
        function toggle_visibility(className) {
           var e = getElementsByClassName(className);
            
           for ( var i=0, len=e.length; i<len; ++i ){
    	 if(e[i].style.display == 'none'){
               e[i].style.display = 'block';
               e[i].style.background='./graphics/' + className + '.gif';
             }
             else{
    	   e[i].style.display = 'none';
    	   e[i].style.background= '';
             }
           }
         }
    
    </script>
    Verzeihung!

  10. #8
    Join Date
    Jun 2008
    Location
    Norwich, UK
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Minos View Post
    First, forget the word id. Classes and id's are two separate beasts. The easy way to recall the difference? An id refers to ONE and only ONE object. A class can be a billion objects, if you were really that needy. :P

    Your current code:
    Code:
    <script type="text/javascript" src="scripts/getElementsByClassName-1.0.1.js"></script>
    <script type="text/javascript">
    
        function toggle_visibility(className) {
           var e = getElementsByClassName(className),
           var f = document.getElementById(className);
           
    	   for ( var i=0, len=e.length; i<len; ++i ){
    			if(e[i].style.display == 'none')
          {
           e[i].style.display = 'block';
           f[i].style.background='url(graphics/*className*.gif';
          }
          else
          {
    	e[i].style.display = 'none';
    	f[i].style.background='none';
           }
        }
        }
    
    </script>
    Basically, like I said above, you don't need f. An id does not exist here. I fixed the syntax, however, I'm not 100% sure if the background will work as intended. I'm not testing it at the moment.

    Code:
    <script type="text/javascript" src="scripts/getElementsByClassName-1.0.1.js"></script>
    <script type="text/javascript">
    
        function toggle_visibility(className) {
           var e = getElementsByClassName(className);
            
           for ( var i=0, len=e.length; i<len; ++i ){
    	 if(e[i].style.display == 'none'){
               e[i].style.display = 'block';
               e[i].style.background='./graphics/' + className + '.gif';
             }
             else{
    	   e[i].style.display = 'none';
    	   e[i].style.background= '';
             }
           }
         }
    
    </script>
    Thanks for the help, but I'm afraid you've misunderstood what I'm looking to do.

    I wish to use divs with ID X to toggle the visibility of all items with Class X, that has been accomplished, however I also wish to change the background of the ID X so that the user knows whether the Classes are visible or not.

    I'm using it here to filter out blog entries from different categories.

  11. #9
    Join Date
    Dec 2004
    Posts
    177
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default

    Ahh, I see what you are trying to do. Well, that shouldn't be too bad. What I would do, and someone can correct me if I'm wrong, I would change the name of the ids. I'm not too keen on the idea of them being the same, that can get a little confusing. Then, simply pass another variable to the function:

    Code:
    <script type="text/javascript">
    
        function toggle_visibility(className, idName) {
           var e = getElementsByClassName(className),
           var f = document.getElementById(idName);
           
    	 for ( var i=0, len=e.length; i<len; ++i ){
    	   if(e[i].style.display == 'none'){
                 e[i].style.display = 'block';
                 f[i].style.background='url(./graphics/' + className '.gif');
               }
               else{
    	     e[i].style.display = 'none';
    	     f[i].style.background='none';
               }
            }
        }
    </script>
    Just remember to add the variable when calling the function, ex. toggle_visibility("className", "idName")
    Verzeihung!

  12. The Following User Says Thank You to Minos For This Useful Post:

    BlueStunt (06-09-2008)

Bookmarks

Posting Permissions

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