What is the best way to collapse additional submenus and perform a search within a menu?

Whenever a sub-menu is activated, only the current sub-menu should be open while the others remain closed.

I need the search function to be enabled on the text box and display all items containing the specified value while also changing the color of <a>

This is the code I came up with:

$(document).ready(function(){
    $("#navigation").click(function () {
      
$(this).next().slideToggle();

});

    $("#main-menu>ul>li a ").click(function () {
        
        $(".submenu").slideUp();
        $(this).next().slideToggle();
        
});
$("#main-menu>ul>li>a ").click(function(){
//$(this).toggleClass("current");

});

});
@font-face {
 font-family: 'casa';
 src: url('Fonts/Mj_Casablanca.eot?#') format('eot'), url('Fonts/Mj_Casablanca.woff') format('woff'), url('Fonts/Mj_Casablanca.ttf') format('truetype');
}
body
{
background:#eee;
font:normal 12px casa;
}
a
{
text-decoration:none;
}
li
{
list-style:none;
}
#main-menu li
{
text-align:right;
direction:rtl;
padding:0 2%;
}

#main-menu>ul>li >a:hover
{
    background:#575757 url("../img/arrow.png")  no-repeat 10px 10px;
}


#menu-search
{
background-color: transparent;
border:none;
width:80%;
text-align:right;
direction:rtl;
color: #eee;
line-height: 30px;
padding: 5px 10%;

}
input[type="text"]
{
font:normal 12px tahoma;
}
.submenu
{
display:none;
}
.current
{
color:orange !important;
}

.submenu li a
{
color:#eee !important;
padding-right:10% !important;
}
#main-menu>ul> li >a
{
border-top: 1px solid #5C5C5C !important;  
}

#main-menu
{
width:230px;
background:#303030;
height:auto;
float:right;
/*max-height:400px;*/
overflow: visible !important;
padding:5px 0;

}

#main-menu>ul
{
    display:block !important;
}
#main-menu>ul>li >a
{
    background: url("../img/arrow.png")  no-repeat 10px 10px;
}
#main-menu li a {
color:#fff;

    display: block;
    padding: 10px 10%;
    
}

#navigation
{
display:none;
}
.submenu
{
display:none;
}
#footer
{
min-height: 40px;
width: 100%;

clear: both;

background: #5C5C5C;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<div id="main-menu">
<a href="#" id="navigation">Menu</a>
<ul>
<li>
<input type="text" name="txt" id="menu-search" placeholder="Search..."/>
</li>
<li><a href="#">Items 1</a>
<ul class="submenu">
<li><a href="#">Item1</a>
<ul class="submenu">
<li><a href="#">Items 2</a>
<li><a href="#">Items 3</a>
<li><a href="#">Items 4</a>
</ul>
</li>
<li><a href="#">Items2</a>
    <ul class="submenu">
      <li><a href="#">Items 1</a>
        <li><a href="#">Items 2</a>
          <li><a href="#">Items 3</a>
            <li><a href="#">Items 4</a>
    </ul>
    </li>
<li><a href="#">Items3</a></li>
</ul>
</li>
<li><a href="#">Items4</a>
    <ul class="submenu">
     <li><a href="#">Items 1</a>
       <li><a href="#">Items 2</a>
         <li><a href="#">Items 3</a>
    </ul>
    </li>
<li><a href="#">Items5</a>
    <ul class="submenu">
      <li><a href="#">Items 1</a>

            <ul class="submenu">
           <li><a href="#">Items 1</a>
             <li><a href="#">Items 2</a>
               <li><a href="#">Items 3</a>
            </ul>
        </li>
        <li><a href="#">Items 1</a>
          <li><a href="#">Items 2</a>
            <li><a href="#">Items 3</a>
    </ul>
    </li>


</ul>
<div>
<div id="footer">
</div>

Answer №1

To begin, the first step is to assign the class nested to the internal submenu. Additionally, apply the following style to the nested class:

padding-right: 10%;

Next, utilize the provided JavaScript code:

    $(document).ready(function(){
       
$('#main-menu ul li a').on('click', function (e) {$('#main-menu ul li a').removeClass("current");$(this).toggleClass("current");$(this).next().slideToggle();
$(this).parent().siblings('li').find('ul').hide();
e.stopPropagation();
});

    });
@font-face {
 font-family: 'casa';
 src: url('Fonts/Mj_Casablanca.eot?#') format('eot'), url('Fonts/Mj_Casablanca.woff') format('woff'), url('Fonts/Mj_Casablanca.ttf') format('truetype');
}
body
{
background:#eee;
font:normal 12px casa;
}
a
{
text-decoration:none;
}
li
{
list-style:none;
}
#main-menu li
{
text-align:right;
direction:rtl;
padding:0 2%;
}

#main-menu>ul>li >a:hover
{
    background:#575757 url("../img/arrow.png")  no-repeat 10px 10px;
}

<!-- More CSS styles here -->

</code></pre>
<pre><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<div id="main-menu">
<a href="#" id="navigation">Menu</a>
<ul>
<!-- More HTML code here -->
</ul>
<div>
<div id="footer">
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to adjust the width of the react-select component

I've been struggling to adjust the width of the select element but no matter what I try, it remains at a default width of about 75px. const CustomContainer = styled('div')` width: 100%; height: 100%; display: flex; flex-flow: row wr ...

Updating select boxes using PHP, MySQL, jQuery, and AJAX

As the title suggests, I am facing issues updating select boxes and unable to find a solution. This is my HTML: <div class="col-sm-3"> <select class="form-control" id="answer9"> <option value="00">Select</option> ...

A guide on updating the Date Textfield value in Material UI

In my data structure, I have an array of objects with unique ids and date values. For instance, the array named stepDates might appear like this: [ { id: 1, date: "2021-07-23" }, { id: 2, date: null }, { id: 3, d ...

Challenges with Image Placement and Alignment

Having trouble with the sizing of my image in the skill portion of my website. The clouds and mountains look fine, but for some reason, the image isn't the same size. Additionally, the text "full stack developer" is not centered under my name anymore ...

What is the best method to deallocate and remove the background-color property of CSS once it has been defined?

There is a perplexing issue that I am unable to resolve. The "Shop" section on this page has an intriguing translucent blue-green background: This background is set using the ID "#left-area". Interestingly, on another page, which can be found at , the sam ...

Activate hover effect on toggle button

When I hover over the "CHANGE" button, the orange color appears as expected. Clicking the button once turns the color red but removes the hover color, which is fine. However, clicking it twice brings back the original blue color but the hover effect is m ...

How can you align a div next to another using the float property in CSS?

/*Custom Styling for Divs*/ #box1 { border: 1px solid; border-radius: 25px; width: 900px; } #box2 { border: 1px solid; border-radius: 25px; width: 430px; } #box3 { float: right; border: 1px solid; border-radius: ...

Tips for automatically incorporating animation upon page initialization

I'm looking to add an automatic image effect when the page is loaded. I currently have this code in my js file: $(window).ready(function(){ $(pin).click(function(){ $("#pin01").show().animate({left: '650px'}); }) }); Here is the HTML wit ...

Element remains hidden until the developer console is activated

On my website, I've noticed that certain LayerSlider elements are remaining invisible until: The window is resized I disable the Bookmarks bar in Chrome (quite strange, right?) I switch on the Chrome debugger tools This issue is not exclusive to Ch ...

Steps for deactivating the submit button once confirming the presence of the username and email

When using AJAX to verify the existence of an email or username in the database, I want to deactivate the submit button if either one (or both) already exist. Currently, I have successfully changed the input border color but I am facing a challenge with t ...

Is the JavaScript file not being stored in the cache?

As I work on optimizing my web application, I am facing a challenge with a javascript file size of approximately 450K even after compressing it. While I intend to redo the javascripting in due time, for now, I need to go live with what I have. Initially, I ...

"Efficiently Switching Between div Elements: A Guide to Toggling Content

I am looking to use two separate divs to toggle my content. By clicking on the "open" div, the contents will open, and by clicking on the "close" div, the contents will close. <div class="open"></div> <div class="close"></div> < ...

How do I determine the appropriate image type to use?

I'm a beginner in the world of Node.js and I'm currently working on an application that stores image files. However, I am unsure about what type of data the images should be. const userSchema = new mongoose.Schema({ userImage: { type ...

Looking to capture a specific div in a React JS component? I initially tried using the "use-screenshot-hook" package, but unfortunately, it captured the entire page instead

const reference = useRef(null); const { capturedImage, takeScreenshot } = useScreenshot(); const imageStyle = { height: imgHeight, width: imgWidth }; const fetchImage = () => takeScreenshot({reference}); const saveImage = () => ...

Incorporating Masonry-inspired images into a website's design section

I have been working on incorporating a masonry layout of images into my website, but I am facing alignment issues. The images are simply stacking on top of each other instead of creating the desired masonry effect. Are there any tips on how to create a mas ...

Can a stroke in an SVG image be created using a gradient color?

Is it possible to create a gradient in the stroke color of these two lines, currently set to #389967, within this SVG? .chart-three svg .circle-foreground { stroke: #389967; stroke-dasharray: 494.55px 549.5px; stroke-dashoffset: 494.55px; stroke- ...

JavaScript promise will return a value that is undefined

Trying to pass a jQuery ajax request through a promise like this: var foo; foo = bar().then(function(response) { console.log("Success!", response); console.log(foo[0].bool); }, function(error) { console.error("Failed!", error); }); console.lo ...

React: image appearing in DOM but failing to render on screen

As I work on developing a web application using React, my focus is on creating cards that consist of header text and an image. However, I have encountered a problem with displaying the image properly. When I import the image and attempt to use it in my co ...

Babylon entities failing to run

Encountering a strange issue in my current project. I am working on a react application that incorporates Babylonjs for 3D rendering. While I am able to successfully load objects into the scene, I am facing a problem when attempting to create a PBR Materia ...

Implementing a Vue feature where an object is utilized as the v-model within a select

I am currently working with a select tag that displays an array of objects as its options. Each option only shows the name property of the object. The v-model on the select tag is initially set to one of the object's name properties. My aim is to use ...