-1
Hello! I created a menu with submenus that when clicking on it, it appears. When clicking outside, it hides, as well as clicking on it again. By clicking on other menus, the submenu also disappears. I would like to share the code here, because many people have this problem and want to use javascript without frameworks. I am also sharing so that criticisms are made, because I am a beginner programmer. I started programming about three months ago. Originated!
The display functions will be used as onclick in the <li>
containing a <ul>
. The hidden functions will all be used as onclick
in the <body>
.
Sorry for the bullshit.
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;
}
}
Can wec0n add HTML to the question? So you can run the code here.
– Sergio
This is not a question or difficulty, so I think outside the scope
– user60252
no stackoverflow can also answer your own questions, this is not a question, but do not think it is outside the scope
– isaque