0

In this program the sub menu item are does not hide when the mouse move to other items,I want to hide the sub menu items when mouse move from their area .can you help me?.I really want a drop-down menu on click.

    #main li{
        list-style-type: none;
        display: none;
        text-decoration: none;
    }
    #main{
        cursor: pointer;
        
    }
    #pappaya {
        background-color:#339933;
         text-decoration: none;
    }
     #pappayas {
            background-color:#339933;
          text-decoration: none;
        }
    .orang{
        text-decoration: none;
    }
    #pappaya li{
        text-decoration: none;
    }
 <html>
        <head>
            <link rel="stylesheet" type="text/css" href="mango.css">
            <script>
                 function mangoGrape(selector){
                document.querySelectorAll(selector)
                    .forEach(function(node){
                        node.style.display="block";
                    })
                 }
            </script>
        </head>
        <body>
        </body>
        <ul id="main" onclick="mangoGrape('.apple')">main1
            <div id="pappaya">
            <li class="apple"> <a href="#">sub1</a></li>
             <li class="apple"><a href="#">sub2</a></li>
             <li class="apple"><a href="#">sub3</a></li>
            </div>
        </ul>
         <ul id="main" onclick="mangoGrape('.orang')">main2
            <div id="pappayas">
             <a href="#"><li class="orang">sub21</li></a>
             <a href="#"><li class="orang">sub22</li></a>
             <a href="#"><li class="orang">sub23</li></a>
            </div>
        </ul>
    </html>

In this program the sub menu item are does not hide when the mouse move to other items,I want to hide the sub menu items when mouse move from their area .one another problem associated with this code is the text decoration property is not working properly.

ADH - THE TECHIE GUY
  • 4,125
  • 3
  • 31
  • 54

2 Answers2

2

You just forget hiding another item:

/*Do not show li when page showed/ li { display: none; }*/


#main li{
    list-style-type: none;
    display: none;
    text-decoration: none;
}
#main{
    cursor: pointer;

}
#pappaya {
    background-color:#339933;
     text-decoration: none;
}
 #pappayas {
        background-color:#339933;
      text-decoration: none;
    }
.orang{
    text-decoration: none;
}
#pappaya li{
    text-decoration: none;
}
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="mango.css">
        <script>
            function mangoGrape(selector){
                 // hide all lis first
                 document.querySelectorAll('li')
                    .forEach(function(node){
                        node.style.display="none";
                    })
                // then show what you want
                document.querySelectorAll(selector)
                    .forEach(function(node){
                        node.style.display="block";
                    })
             }
        </script>
    </head>
    <body>
      <ul id="main" onclick="mangoGrape('.apple')">main1
          <div id="pappaya">
          <li class="apple"> <a href="#">sub1</a></li>
           <li class="apple"><a href="#">sub2</a></li>
           <li class="apple"><a href="#">sub3</a></li>
          </div>
      </ul>
       <ul id="main" onclick="mangoGrape('.orang')">main2
          <div id="pappayas">
           <a href="#"><li class="orang">sub21</li></a>
           <a href="#"><li class="orang">sub22</li></a>
           <a href="#"><li class="orang">sub23</li></a>
          </div>
      </ul>
    </body>
</html>
ADH - THE TECHIE GUY
  • 4,125
  • 3
  • 31
  • 54
CoderLim
  • 1,222
  • 3
  • 11
  • 23
1

The onmouseout event occurs when the mouse pointer is moved out of an element, or out of one of its children.

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="mango.css">
        <script>
             function mangoGrape(selector){
            document.querySelectorAll(selector)
                .forEach(function(node){
                    node.style.display="block";
                })
             }
            function hide(selector){
            document.querySelectorAll(selector)
                .forEach(function(node){
                    node.style.display="none";
                })
             }
        </script>
    </head>
    <body>
    </body>
    <ul id="main" onmouseout="hide('#pappaya')" onclick="mangoGrape('.apple')">main1
        <div id="pappaya">
        <li class="apple"> <a href="#">sub1</a></li>
         <li class="apple"><a href="#">sub2</a></li>
         <li class="apple"><a href="#">sub3</a></li>
        </div>
    </ul>
     <ul id="main" onmouseout="hide('#pappayas')" onclick="mangoGrape('.orang')">main2
        <div id="pappayas">
         <a href="#"><li class="orang">sub21</li></a>
         <a href="#"><li class="orang">sub22</li></a>
         <a href="#"><li class="orang">sub23</li></a>
        </div>
    </ul>
</html>
ADH - THE TECHIE GUY
  • 4,125
  • 3
  • 31
  • 54
Nihar Sarkar
  • 1,187
  • 1
  • 13
  • 26