function showMenu(parentElement){
var uls= parentElement.getElementsByTagName("ul");
if (uls.length > 0)
uls[0].setAttribute("style","display: block");
}
function load(){
var lis = document.getElementsByTagName("li");
for (var i = 0; i < lis.length; i++) {
lis[i].addEventListener("click", function(){
showMenu(this);
});
}
}
I have this script, wich allows me to open the correct 'ul', when the 'li' is clicked. I then added this
function Meny(){
var parentElement = document.getElementById("cssmenu");
var lis = parentElement.getElementsByTagName("ul");
for (var i = 0; i < lis.length; i++) {
lis[i].setAttribute("style","background: white");
}
}
And attached it to the divs onclick outside my menu, so that the menu closes nicely, when it is clicked anywhere else. Then ran into a little problem with positioning, so I set my cssmenu to position:fixed. I can live with that, althou, I see how it can be a bad practice. Then I found out, I can hide it with opacity 0.2, when xoomed to more then 200%, and then, visible on hover.
My problem now, is that when I click on another li, it will not close the other li's ul. So the submenu opens, but wont close when I click its sibling, or any other li, that is not a part of that li's Ul.. I use the this keyword in my menu, like so:
<nav id="cssmenu">
<li onclick="showMenu(this);"><a></a>
<ul>
<li></li>
</ul>
</nav>
I have a much longer menu, with many submenu's. I have seen working jquery's for this, but cannot get jquery to work, and besides, I AM eager to learn javascript, as the logic is understandable, to some extend. All the keywords I am not familiar with, and all this ul, li, ul, is confusing to say the least..
my css is pretty straight forward, and a good way to learn all the ul li ul li rules. just simple targeting, this I can understand fine.
#cssmenu{}
#cssmenu ul li{}
#cssmenu ul ul {display:none}
#cssmenu a{}
I recently started transforming a drop down hover menu, to a clicking menu instead, to more fit touch screens, and keep some other hover effects for pc at least.
Is it doable with javascript, to have the childs ul, close, when sibling is pressed, or uncle or aunt or whatever, anything BUT that li's elements?
I have seen it with jquery, as it is in these examples:
http://jsfiddle.net/contendia/ddXBU/4/
I have tried to use the code in the 2'nd example, as it adresses ul's and li's, and not have 'class' specified in the menu. I then changed the words #leftmenu, to #cssmenu, without any luck. I have only my
<nav id="cssmenu">
as a unique element in my menu, and the THIS keyword took nicely care of that issue. Also, i read that unique id's are preferable, as ul li (tags), load slower then id's, easy to understand. I do not have such a huge page, at least not yet, that these are issues that I am concerned with, and I am not interested in giving all my menu items a unique id. I can of course use jquery, but that is something I need to read up on how to implement, i tried following these instructions, but somehow failed.
Also, I do not understand completly, why the 3'rd script, is closing the cssmenu, I tried changing the background: white, to display:none, this simply hide my entire menu. And that is strange I think, as when I tried it out earlier, it worked for changing the color, of every 'ul', except the top ones, the visible top menu items.
Appreciate any responds, I HAVE been told to use jquery, but it is a little pain in the #*# when it wont work, and I feel I am just oh so close to the solution. In my mind, another if-statement is needed, just cannot seem to figure out that one, as arrays, the FOR keyword, and such is something I really need to learn more about.