0

Please help with this function for a sliding menus. For each object in the slideMenus Array I want to reference the 1st unordered list element within that object and set the value of the ul elements left style property to 0px. Please help! thank you!

function makeMenus(){
var slideMenus=new Array();
var allElems=document.getElementsByTagName("*");
for(var i=0;i<allElems.length;i++){
        if(allElems[i].className=="slideMenus")menus.push(allElems[i]);
}
for(var i=0;i<slideMenus.length;i++){
    slideMenus[i].onclick=showSlide;
    slideMenus[i].ul.style.left="0px";
    /*for each object in slideMenus Array, 
    ref 1st ul element within that object 
    and set the value of the ul elements 
    left style property to 0px */
}   
document.getElementById("head").onclick=closeSlide;
document.getElementById("main").onclick=closeSlide; 

}

Vynce82
  • 119
  • 3
  • 15
  • 1
    Would you consider using JQuery or another library to streamline and enhance node selection? – MasterAM Jun 23 '13 at 14:09
  • thank you for the suggestion, i just starting to learn Javascript but many people are telling me I should start with jquery – Vynce82 Jun 25 '13 at 13:30
  • @Vynce82 IMHO you should continue learning javascript after that you can learn jquery, so that you can apply javascript or jquery according to the situation. – Balaji Sivanath Jun 25 '13 at 14:24

1 Answers1

1

You can use getElementsByTagName to get ul

function makeMenus(){

//No need to get all the nodes in the page, you can just get elements by class name.
var slideMenus = document.getElementsByClassName("slideMenus");

for(var i=0;i<slideMenus.length;i++){
  slideMenus[i].onclick = showSlide;
  slideMenus[i].getElementsByTagName("ul")[0].style.left = "0px";
}   
document.getElementById("head").onclick=closeSlide;
document.getElementById("main").onclick=closeSlide; 
}
Balaji Sivanath
  • 506
  • 4
  • 14
  • @Vynce82 As a improvement you can add event listener, instead of direct onclick. In future if you want to do one more action when clicking the element, you can add another listener easily. https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener – Balaji Sivanath Jun 25 '13 at 14:29