Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Hide class

  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Hide class

    Hello

    My assignment is to find all <p> and hide the ones that are <p class="show"> using a For Loop. "show" is supposed to be hidden when loading the page, because im gonna do a < a > that is gonna show them. This is what i have done, and it doesnt work. Did i do something wrong or am i way off here ?

    windows.onload = prepareShow;
    function prepareShow(){
    var fan = document.getElementsByClassName("show");
    for (var i = 0; i < fan.length; i++){
    if (fan[i].getAttribute("class")=="show"){
    fan[i].style.display="none";}
    }
    }

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Can be done a few ways but :
    Code:
    <html>
    <head>
    <style type="text/css">
    .show{
    	display: none;
    }
    </style>
    </head>
    <body>
    <p class="show">bill</p>
    <p class="abit">See</p>
    <p class="show">dick</p>
    <p class="abit">Me</p>
    <p class="show">jane</p>
    <p class="abit">Run</p>
    <button onclick="LetsSee()">Behold</button>
    
    <script type="text/javascript">
    function LetsSee()
    {
    	var x = document.getElementsByClassName("show").length;
    	for(i = 0; i < x; i++ ){
    	var fan = document.getElementsByClassName("show")[i].style.display = 'block';
    	}
    }
    </script>
    </body>
    </html>

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You can make the code a lot shorter than that:

    Code:
    function LetsSee()
    {
    	var x = document.getElementsByClassName("show");
    	for(i = x.length-1; i >0 0; i-- ){
    	x[i].style.display = 'block';
    	}
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by felgall View Post
    You can make the code a lot shorter than that:

    Code:
    function LetsSee()
    {
    	var x = document.getElementsByClassName("show");
    	for(i = x.length-1; i >0 0; i-- ){
    	x[i].style.display = 'block';
    	}
    }

    You can make the code a lot shorter than that, (and re-usable):

    Code:
    function LetsSee(c,s,r){r=(r||document).getElementsByClassName(c);
     for(c in x)(x[c].style||0).display=s}

    usage: LetsSee(classToHit , displayStyleToSet, opt_elmRoot );

    ex (do what the previous codes do):
    Code:
    LetsSee("show","block");
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    Code:
    function LetsSee(c,s,r){r=(r||document).getElementsByClassName(c);
     for(c in x)(x[c].style||0).display=s}
    Shouldn't that be

    Code:
    function LetsSee(c,s,r){x=(r||document).getElementsByClassName(c);
     for(c in x)(x[c].style||0).display=s}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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