www.webdeveloper.com
Results 1 to 2 of 2

Thread: Need help hide Class with For Loop

  1. #1
    Join Date
    Oct 2012
    Posts
    18

    Need help hide Class with For Loop

    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. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Try this
    Code:
    windows.onload = prepareShow;
    function prepareShow(){
       // All <p>
       var fan = document.getElementsByTagName("p");
       for (var i = 0; i < fan.length; i++){
          if (/show/.test(fan[i].className)){
             fan[i].style.display="none";
          }
       }
    }

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