skip to Main Content

I’m unable to show/hide divs. It gives me error cannot read property style from null.

Any help would be appreciated.

function HideAllShowOne(d) {
  // Between the quotation marks, list the id values of each div.

  var IDvaluesOfEachDiv = "w11 W12 W21 w31";

  //-------------------------------------------------------------
  IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/[,s"']/g, " ");
  IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/^s*/, "");
  IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/s*$/, "");
  IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/  +/g, " ");
  var IDlist = IDvaluesOfEachDiv.split(" ");
  for (var i = 0; i < IDlist.length; i++) {
    HideContent(IDlist[i]);
  }
  ShowContent(d);
}


function HideContent(d) {
  document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
  document.getElementById(d).style.display = "block";
}

function ReverseDisplay(d) {
  if (document.getElementById(d).style.display == "none") {
    document.getElementById(d).style.display = "block";
  } else {
    document.getElementById(d).style.display = "none";
  }
}
<div id='menu' class="col-sm-4">
  <ul>
    <li>
      <a class='text-center'>
        <img src='student/student.jpg' class='img-responsive img-circle ' width='100px' height='100px' style=' float:none; ' />
      </a>
    </li>
    <li><a href='#'><b>harsha lenka</b></a>
    </li>
    <li><a href='#home'>Home</a>
    </li>
    <li class='active has-sub'><a href='#'>Week 1</a>
      <ul>
        <li><a href=javascript:HideAllShowOne( 'w11')>Back propagation Algorithm</a>
        </li>
        <li><a href=javascript:HideAllShowOne( 'w12')>Introduction</a>
        </li>
      </ul>
    </li>
    <li class='has-sub'><a href='#'>Week 2</a>
      <ul>
        <li><a href=href=javascript:HideAllShowOne( 'w21')>Introduction</a>
        </li>
      </ul>
    </li>
    <li class='has-sub'><a href='#'>Week 3</a>
      <ul>
        <li><a href=href=javascript:HideAllShowOne( 'w31')>Introduction</a>
        </li>
      </ul>
    </li>
    <li class=' has-sub'><a href='#'>Assignments</a>
      <ul>
        <li class='has-sub'><a href='#'>Mid Term 1</a>
          <ul>
            <li><a href='#'>Assignment 1</a>
            </li>
            <li><a href='#'>Assignment 2</a>
            </li>
          </ul>
        </li>
        <li class='has-sub'><a href='#'>Mid Term 2</a>
          <ul>
            <li><a href='#'>Assignment 1</a>
            </li>
            <li><a href='#'>Assignment 2</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href='#'>About</a>
    </li>

  </ul>
</div>

<div id="ved_dat" class="col-sm-8">
  <div id='#w11' style='display:none;'>
    <video width='500' height='500' controls>
      <source src='courses/Artificial Intelligence/week1/Back propagation Algorithm.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
  </div>
  <div id='#w12' style='display:none;'>
    <video width='500' height='500' controls>
      <source src='courses/Artificial Intelligence/week1/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
  </div>
  <div id='#w21' style='display:none;'>
    <video width='500' height='500' controls>
      <source src='courses/Artificial Intelligence/week2/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
  </div>
  <div id='#w31' style='display:none;'>
    <video width='500' height='500' controls>
      <source src='courses/Artificial Intelligence/week3/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
  </div>
</div>


<div>
</div>

</body>
<script type='text/javascript'>
  <!-- 
  
    //-->
</script>

3

Answers


  1. Chosen as BEST ANSWER

    Finally did it thanks to your help guys :)

       <script type='text/javascript'>
        var ids = $.map($('#ved_dat div'), function(n,i) {
                      return n.id
                  });
    
            function show_hide(d){
    
              for (i = 0; i < ids.length; i++) { 
                  if(ids[i]!=d){
                    document.getElementById(ids[i]).style.display='none';
                  }
              }
             document.getElementById(d).style.display='block';
    
            }
    
          </script>
    

  2. You are having many silly mistakes in your code

    1. Remove extra white space before the parameter of all function call

    eg : <a href=javascript:HideAllShowOne( 'w11')>Back propagation Algorithm</a>

    Corrected code :

    <a href=javascript:HideAllShowOne('w11')>Back propagation Algorithm</a>

    1. All your div ids have Lower-case w.

    And two of your w in var IDvaluesOfEachDiv = "w11 W12 W21 w31" are Upper-case, W12 and W21

    Note : ids are case-sensitive

    1. Remove double href= on Week 2 and Week 3 sub li

    2. You should not provide # before the id in divs

    EDIT : Provided a working snippet below

    function HideAllShowOne(d) {
      // Between the quotation marks, list the id values of each div.
    
      var IDvaluesOfEachDiv = "w11 w12 w21 w31";
    
      //-------------------------------------------------------------
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/[,s"']/g, " ");
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/^s*/, "");
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/s*$/, "");
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/  +/g, " ");
      var IDlist = IDvaluesOfEachDiv.split(" ");
      for (var i = 0; i < IDlist.length; i++) {
        HideContent(IDlist[i]);
      }
      ShowContent(d);
    }
    
    
    function HideContent(d) {
      document.getElementById(d).style.display = "none";
    }
    
    function ShowContent(d) {
      document.getElementById(d).style.display = "block";
    }
    
    function ReverseDisplay(d) {
      if (document.getElementById(d).style.display == "none") {
        document.getElementById(d).style.display = "block";
      } else {
        document.getElementById(d).style.display = "none";
      }
    }
    <div id='menu' class="col-sm-4">
      <ul>
        <li>
          <a class='text-center'>
            <img src='student/student.jpg' class='img-responsive img-circle ' width='100px' height='100px' style=' float:none; ' />
          </a>
        </li>
        <li><a href='#'><b>harsha lenka</b></a>
        </li>
        <li><a href='#home'>Home</a>
        </li>
        <li class='active has-sub'><a href='#'>Week 1</a>
          <ul>
            <li><a href="javascript:HideAllShowOne('w11')">Back propagation Algorithm</a>
            </li>
            <li><a href="javascript:HideAllShowOne('w12')">Introduction</a>
            </li>
          </ul>
        </li>
        <li class='has-sub'><a href='#'>Week 2</a>
          <ul>
            <li><a href="javascript:HideAllShowOne('w21')">Introduction</a>
            </li>
          </ul>
        </li>
        <li class='has-sub'><a href='#'>Week 3</a>
          <ul>
            <li><a href="javascript:HideAllShowOne('w31')">Introduction</a>
            </li>
          </ul>
        </li>
        <li class=' has-sub'><a href='#'>Assignments</a>
          <ul>
            <li class='has-sub'><a href='#'>Mid Term 1</a>
              <ul>
                <li><a href='#'>Assignment 1</a>
                </li>
                <li><a href='#'>Assignment 2</a>
                </li>
              </ul>
            </li>
            <li class='has-sub'><a href='#'>Mid Term 2</a>
              <ul>
                <li><a href='#'>Assignment 1</a>
                </li>
                <li><a href='#'>Assignment 2</a>
                </li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href='#'>About</a>
        </li>
    
      </ul>
    </div>
    
    <div id="ved_dat" class="col-sm-8">
      <div id='w11' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week1/Back propagation Algorithm.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
      <div id='w12' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week1/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
      <div id='w21' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week2/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
      <div id='w31' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week3/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
    </div>
    
    
    <div>
    </div>
    
    </body>
    <script type='text/javascript'>
      <!-- 
      
        //-->
    </script>
    Login or Signup to reply.
  3. In addition to @Munawir’s answer, in <div id='#w11' style='display:none;'> element id is #w11 and not w11. Remove # from id.

    function HideAllShowOne(d) {
      // Between the quotation marks, list the id values of each div.
    
      var IDvaluesOfEachDiv = "w11 W12 W21 w31";
    
      //-------------------------------------------------------------
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/[,s"']/g, " ");
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/^s*/, "");
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/s*$/, "");
      IDvaluesOfEachDiv = IDvaluesOfEachDiv.replace(/  +/g, " ");
      console.log(IDvaluesOfEachDiv)
      var IDlist = IDvaluesOfEachDiv.split(" ");
      for (var i = 0; i < IDlist.length; i++) {
        HideContent(IDlist[i].toLowerCase());
      }
      ShowContent(d);
    }
    
    
    function HideContent(d) {
      console.log(document.getElementById(d),d)
      document.getElementById(d).style.display = "none";
    }
    
    function ShowContent(d) {
      document.getElementById(d).style.display = "block";
    }
    
    function ReverseDisplay(d) {
      if (document.getElementById(d).style.display == "none") {
        document.getElementById(d).style.display = "block";
      } else {
        document.getElementById(d).style.display = "none";
      }
    }
    <div id='menu' class="col-sm-4">
      <ul>
        <li>
          <a class='text-center'>
            <img src='student/student.jpg' class='img-responsive img-circle ' width='100px' height='100px' style=' float:none; ' />
          </a>
        </li>
        <li><a href='#'><b>harsha lenka</b></a>
        </li>
        <li><a href='#home'>Home</a>
        </li>
        <li class='active has-sub'><a href='#'>Week 1</a>
          <ul>
            <li><a href=javascript:HideAllShowOne('w11')>Back propagation Algorithm</a>
            </li>
            <li><a href=javascript:HideAllShowOne( 'w12')>Introduction</a>
            </li>
          </ul>
        </li>
        <li class='has-sub'><a href='#'>Week 2</a>
          <ul>
            <li><a href=href=javascript:HideAllShowOne( 'w21')>Introduction</a>
            </li>
          </ul>
        </li>
        <li class='has-sub'><a href='#'>Week 3</a>
          <ul>
            <li><a href=href=javascript:HideAllShowOne( 'w31')>Introduction</a>
            </li>
          </ul>
        </li>
        <li class=' has-sub'><a href='#'>Assignments</a>
          <ul>
            <li class='has-sub'><a href='#'>Mid Term 1</a>
              <ul>
                <li><a href='#'>Assignment 1</a>
                </li>
                <li><a href='#'>Assignment 2</a>
                </li>
              </ul>
            </li>
            <li class='has-sub'><a href='#'>Mid Term 2</a>
              <ul>
                <li><a href='#'>Assignment 1</a>
                </li>
                <li><a href='#'>Assignment 2</a>
                </li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href='#'>About</a>
        </li>
    
      </ul>
    </div>
    
    <div id="ved_dat" class="col-sm-8">
      <div id='w11' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week1/Back propagation Algorithm.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
      <div id='w12' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week1/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
      <div id='w21' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week2/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
      <div id='w31' style='display:none;'>
        <video width='500' height='500' controls>
          <source src='courses/Artificial Intelligence/week3/Introduction.mp4' type='video/mp4'>Your browser does not support the video tag.</video>
      </div>
    </div>
    
    
    <div>
    </div>
    
    </body>
    <script type='text/javascript'>
      <!-- 
      
        //-->
    </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search