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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy show/hide div jscript not working on IE

    Hi, i have this jscript code and I want to show the first div onLoad. Trying to figure this out, what I did was:

    <body onload="showdiv(0);">

    Running on firefox works fine, the first div is showing onload BUT testing on IE doesn't work at all. It displays an error:

    document.getElementById()is null or not an object

    here's the jscript i'm using:
    <script type="text/javascript">

    var c;
    var k=0;
    var j=0;
    var ary=[];
    var divs=document.getElementsByTagName('div');
    var lnks=document.getElementsByTagName('a');
    var splt;

    function init() {
    for(c=0;c<divs.length;c++) {
    if(divs[c].className=='show') {
    divs[c].className='hide';
    divs[c].id='d'+k++;
    }
    }
    for(c=0;c<lnks.length;c++) {
    if(lnks[c].className=='hide') {
    ary[c]=true;
    lnks[c].className='hs';
    lnks[c].id='a'+j++;
    lnks[c].onclick=function() {
    splt=this.id.split('a')[1];
    if(ary[splt]==true) {
    this.firstChild.nodeValue='hide '+(parseFloat(splt)+1);
    ary[splt]=false;
    showdiv(splt);
    return false;
    }
    else {
    this.firstChild.nodeValue='show '+(parseFloat(splt)+1);
    ary[splt]=true;

    document.getElementById('d'+splt).className='hide';
    return false;
    }
    }
    }
    }
    }


    function showdiv(num) {
    for(c=0;c<j;c++){
    document.getElementById('a'+c).firstChild.nodeValue='show '+(c+1);
    document.getElementById('a'+num).firstChild.nodeValue='hide '+(parseFloat(num)+1);
    ary[c]=true;
    ary[num]=false;
    }
    for(c=0;c<divs.length;c++) {
    if(divs[c].className=='show') {
    divs[c].className='hide';
    }
    }
    document.getElementById('d'+num).className='show';<--- this is the line error
    }

    if(window.addEventListener){
    window.addEventListener('load',init,false);
    }
    else {
    if(window.attachEvent){
    window.attachEvent('onload',init);
    }
    }

    </script>


    How will i solve this issue? So frustrating...
    Hope someone can take a peek on my codes.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Please post your complete code including html(with DOCTYPE)

    btw, use [CODE][/CODE] tags to wrap your code while posting here. You may edit your above post too.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 503 Times in 490 Posts
    Code:
    <script type="text/javascript">
    
    var ary=[];
    var divs;
    
    function init() {
     divs=document.getElementsByTagName('div');
     var lnks=document.getElementsByTagName('a');
     for(var c=0;c<divs.length;c++) {
      if(divs[c].className=='show') {
       divs[c].className='hide';
       divs[c].id='d'+c;
      }
     }
     for(c=0;c<lnks.length;c++) {
      if(lnks[c].className=='hide') {
       ary[c]=true;
       lnks[c].className='hs';
       lnks[c].id='a'+c;
       lnks[c].onclick=function() {
          var splt=this.id.split('a')[1];
          if(ary[splt]==true) {
           this.firstChild.nodeValue='hide '+(parseFloat(splt)+1);
           ary[splt]=false;
           showdiv(splt);
           return false;
          }
          else {
           this.firstChild.nodeValue='show '+(parseFloat(splt)+1);
           ary[splt]=true;
           document.getElementById('d'+splt).className='hide';
           return false;
          }
         }
      }
     }
    }
    function showdiv(num) {
     for(var c=0;c<divs.length;c++){
      document.getElementById('a'+c).firstChild.nodeValue='show '+(c+1);
      document.getElementById('a'+num).firstChild.nodeValue='hide '+(parseFloat(num)+1);
      ary[c]=true;
      ary[num]=false;
     }
     for(c=0;c<divs.length;c++) {
      if(divs[c].className=='show') {
      divs[c].className='hide';
      }
     }
     document.getElementById('d'+num).className='show';//<--- this is the line error
    }
    
    function Init() {
     init();
     showdiv(0);
    }
    
    
    if(window.addEventListener){
    window.addEventListener('load',Init,false);
    }
    else {
    if(window.attachEvent){
    window.attachEvent('onload',Init);
    }
    }
    </script>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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