getElementById variable

Discussion in 'JavaScript' started by damiantaylor, Feb 20, 2010.

  1. #1
    Hi,
    I've been banging my head against a wall for hours on this and can't work out how it's done so can someone please help me?

    I call a javascript function using embedded javascript in HTML as follows:
    
    <SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
    <!--
    window.onload = function ()
    {
    initSlideDownMenu("accordionactivity");
    }
    //-->
    </SCRIPT>
    
    Code (markup):
    The function is in a separate javascript file. When the first alert pops up I get 'accordionactivity', when the second one pops up I get '[object]' but when the third one pops up I get 'null'. The rest of the function fails to work properly too:
    
    function initSlideDownMenu(slMenuName)
    {
    
    alert(slMenuName);
    alert(document.getElementById('accordionactivity'));
    alert(document.getElementById(slMenuName));
    
     dhtmlgoodies_slmenuObj = document.getElementById(slMenuName);
     dhtmlgoodies_slmenuObj.style.visibility='visible';
     var mainUl = dhtmlgoodies_slmenuObj.getElementsByTagName('UL')[0];  
     var mainMenuItem = mainUl.getElementsByTagName('LI')[0];
     mainItemCounter = 1;
     while(mainMenuItem){   
      if(mainMenuItem.tagName=='LI'){
       var aTag = mainMenuItem.getElementsByTagName('A')[0];
       var subUl = mainMenuItem.getElementsByTagName('UL');
       if(subUl.length>0){
        mainMenuItem.id = 'mainMenuItem' + mainItemCounter;
        initSubItems(subUl[0],2);
        aTag.onclick = showSubMenu;
        mainItemCounter++;
       }    
      }   
      mainMenuItem = mainMenuItem.nextSibling; 
     }  
     
     if(location.search.indexOf('mainMenuItemToSlide')>=0){
      var items = location.search.split('&');
      for(var no=0;no<items.length;no++){
       if(items[no].indexOf('mainMenuItemToSlide')>=0){
        values = items[no].split('=');
        showSubMenu(false,document.getElementById('mainMenuItem' + values[1])); 
        initMenuIdToExpand = false;    
       }
      }   
     }else if(expandFirstItemAutomatically>0){
      if(document.getElementById('mainMenuItem' + expandFirstItemAutomatically)){
       showSubMenu(false,document.getElementById('mainMenuItem' + expandFirstItemAutomatically));
       initMenuIdToExpand = false;
      }
     }
    
     if(expandMenuItemByUrl)
     {
      var aTags = dhtmlgoodies_slmenuObj.getElementsByTagName('A');
      for(var no=0;no<aTags.length;no++){
       var hrefToCheckOn = aTags[no].href;
       if(location.href.indexOf(hrefToCheckOn)==0 && (location.href.length==hrefToCheckOn.length || location.href.indexOf('?')==hrefToCheckOn.length)){
        initMenuIdToExpand = false;
        var obj = aTags[no].parentNode;
        while(obj && obj.id!=slMenuName){
         if(obj.tagName=='LI'){       
          var subUl = obj.getElementsByTagName('UL');
          if(initialMenuItemAlwaysExpanded)alwaysExpanedItems[obj.parentNode] = true;
          if(subUl.length>0){        
           objectsToExpand.unshift(obj);
          }
         }
         obj = obj.parentNode; 
        }
        showSubMenu(false,objectsToExpand[0]);
        break;     
       }   
      }
     }
       
     if(initMenuIdToExpand)
     {
      objectsToExpand = new Array();
      var obj = document.getElementById(initMenuIdToExpand)
      while(obj && obj.id!=slMenuName){
       if(obj.tagName=='LI'){
        var subUl = obj.getElementsByTagName('UL');
        if(initialMenuItemAlwaysExpanded)alwaysExpanedItems[obj.parentNode] = true;
        if(subUl.length>0){      
         objectsToExpand.unshift(obj);
        }
       }
       obj = obj.parentNode; 
      }
      showSubMenu(false,objectsToExpand[0]);
     }
    }
    
    Code (markup):
    I've tried using slMenuName.toString() and appending quotes before and after the variable but nothing works.
    Can you help me get this working?

    Thank you :)
     
    damiantaylor, Feb 20, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Can you write here html code too?
     
    s_ruben, Feb 21, 2010 IP