Hello! I created a menu with submenus that when clicking on it, it appears. By clicking outside, it hides, as well as clicking it again. By clicking on other menus, the submenu will also disappear. I would like to share the code here, as a lot of people have this problem and want to use javascript without frameworks. I am also sharing for criticism, as I am a beginner programmer. I started programming for about three months. Originated!
Show functions will be used as onclick on <li>
itself containing <ul>
. The hidden functions will all be used as onclick
in <body>
.
Excuse me for the cursing.
function mostra1() {
if (document.getElementById("submenu2").style.display == "table") {
document.getElementById("submenu2").style.display = "none";
}
if (document.getElementById("submenu3").style.display == "table") {
document.getElementById("submenu3").style.display = "none";
}
if (document.getElementById("submenu4").style.display == "table") {
document.getElementById("submenu4").style.display = "none";
}
if (document.getElementById("submenu1").style.display == "table") {
document.getElementById("submenu1").style.display = "none"
return;
}
document.getElementById("submenu1").style.display = "table";
}
function mostra2() {
if (document.getElementById("submenu1").style.display == "table") {
document.getElementById("submenu1").style.display = "none";
}
if (document.getElementById("submenu3").style.display == "table") {
document.getElementById("submenu3").style.display = "none";
}
if (document.getElementById("submenu4").style.display == "table") {
document.getElementById("submenu4").style.display = "none";
}
if (document.getElementById("submenu2").style.display == "table") {
document.getElementById("submenu2").style.display = "none"
return;
}
document.getElementById("submenu2").style.display = "table";
}
function mostra3() {
if (document.getElementById("submenu1").style.display == "table") {
document.getElementById("submenu1").style.display = "none";
}
if (document.getElementById("submenu2").style.display == "table") {
document.getElementById("submenu2").style.display = "none";
}
if (document.getElementById("submenu4").style.display == "table") {
document.getElementById("submenu4").style.display = "none";
}
if (document.getElementById("submenu3").style.display == "table") {
document.getElementById("submenu3").style.display = "none"
return;
}
document.getElementById("submenu3").style.display = "table";
}
function mostra4() {
if (document.getElementById("submenu1").style.display == "table") {
document.getElementById("submenu1").style.display = "none";
}
if (document.getElementById("submenu2").style.display == "table") {
document.getElementById("submenu2").style.display = "none";
}
if (document.getElementById("submenu3").style.display == "table") {
document.getElementById("submenu3").style.display = "none";
}
if (document.getElementById("submenu4").style.display == "table") {
document.getElementById("submenu4").style.display = "none"
return;
}
document.getElementById("submenu4").style.display = "table";
}
i1 = 0;
function esconde1() {
if ((document.getElementById("submenu1").style.display == "table") && (i1 ==
1)) {
document.getElementById("submenu1").style.display = "none";
}
i1 = 0;
if ((document.getElementById("submenu1").style.display == "table") && (i1 ==
0)) {
i1 = 1;
}
}
i2 = 0;
function esconde2() {
if ((document.getElementById("submenu2").style.display == "table") && (i2 ==
1)) {
document.getElementById("submenu2").style.display = "none";
}
i2 = 0;
if ((document.getElementById("submenu2").style.display == "table") && (i2 ==
0)) {
i2 = 1;
}
}
i3 = 0;
function esconde3() {
if ((document.getElementById("submenu3").style.display == "table") && (i3 ==
1)) {
document.getElementById("submenu3").style.display = "none";
}
i3 = 0;
if ((document.getElementById("submenu3").style.display == "table") && (i3 ==
0)) {
i3 = 1;
}
}
i4 = 0;
function esconde4() {
if ((document.getElementById("submenu4").style.display == "table") && (i4 ==
1)) {
document.getElementById("submenu4").style.display = "none";
}
i4 = 0;
if ((document.getElementById("submenu4").style.display == "table") && (i4 ==
0)) {
i4 = 1;
}
}