Designing hover-activated submenus and ensuring they appear at the forefront

I am currently utilizing jquery to implement a dropdown submenu on hover action.

Here is the structure of the code:

<div id="menucontainer">
  <ul id = "topmenu">
    <li><a onmouseover="javascript:show('div_1');">menu_1</a></li>
    <li><a onmouseover="javascript:show('div_2');">menu_2</a></li>
    <li><a onmouseover="javascript:show('div_3');">menu_3</a></li> # using onmouseover since generated from templates

  </ul>
  <div id="div_1" class="submenu">
    <ul>
      <li> submenu_1 </li>
      <li> submenu_2 </li>
      <li> submenu_3 </li>
    </ul>
  </div>
  <div id="div_3" class="submenu">
    <ul>
      <li> submenu_1 </li>
      <li> submenu_2 </li>
      <li> submenu_3 </li>
    </ul>
  </div>
  <div id="div_3" class="submenu">
    <ul>
      <li> submenu_1 </li>
      <li> submenu_2 </li>
      <li> submenu_3 </li>
    </ul>
  </div>
</div>
<style>
#topmenu {
  list-style: none;
}
#topmenu > li {
  display: inline-block;
}
.submenu {
  display: none;
}

<script>
function show(divid) {
  $('.submenu').css('display', 'none');
  $('#'+divid).css('display', 'block');
}
</script>

An issue arises when show(div) function is triggered and the submenu div appears, there is another div positioned after "menucontainer" that obstructs clicks on the displayed div. How can this problem be resolved?

Answer №1

Your HTML code has been restructured for simplicity, and event listeners are now recommended over inline function calls. Feel free to reach out if any part of this modification doesn't suit your needs.

Explore the updated code on JSFiddle

<div id="menucontainer">
    <ul id="topmenu">
        <li><a href="#">menu_1</a></li>
        <li><a href="#">menu_2</a></li>
        <li><a href="#">menu_3</a></li>
    </ul>
    <div id="div_1" class="submenu">
        <ul>
            <li>submenu_1</li>
            <li>submenu_1</li>
            <li>submenu_1</li>
        </ul>
    </div>
    <div id="div_2" class="submenu">
        <ul>
            <li>submenu_2</li>
            <li>submenu_2</li>
            <li>submenu_2</li>
        </ul>
    </div>
    <div id="div_3" class="submenu">
        <ul>
            <li>submenu_3</li>
            <li>submenu_3</li>
            <li>submenu_3</li>
        </ul>
    </div>
</div>

$('#topmenu li a').hover(function () {
    var myItem = $(this).parents('li').index() + 1;
    $('#div_' + myItem).slideDown();
}, function () {
    $('.submenu').hide();
});

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

What is the best way to achieve a full-width navbar with the scrollbar positioned below it?

I have a similar code to what's shown here The menu is fixed, but the scrollbar seems to be appearing from the right of the navbar What I want is for the scrollbar to start below the menu instead see image example here ...

Adjusting font size according to the font style selected

Can conditional font size be determined based on font family using CSS or jQuery? ...

Is it possible to retrieve text from an html document?

I need to scrape a web page that is full of text and save that text into a file. I am currently attempting to use BeautifulSoup, but I'm not sure if it has the capability I need. Here's the situation: The specific text I want to extract is locate ...

HTML - Link to enable automatic printing of the page

Assignment: Develop a hyperlink (HTML <a> tag) that triggers the printing of the webpage without the user manually pressing CTRL + P. My search for information on this topic has not yielded a definitive answer. Some sources suggest using JavaScript ...

Using CURL in PHP to make a post request on a form

this is the form: <textarea name="message" id="messageContent" rows="18" wrap="virtual" tabindex="2"></textarea> <span id="formSubmit"> <a href="#" class="formSubmit" tabindex="3"> <img src="/clear.gif" class="master-sprite ...

Aligning content within a div using Bootstrap 4

For a demonstration of this code, please visit the following Codepen link: codepen <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstr ...

ReactStrap: Transparency issue with DropDown items on mobile devices

I've encountered an issue with my dropdown menu that works perfectly on desktop: https://i.sstatic.net/73Z0X.png However, on mobile devices, it appears to be transparent for some reason: https://i.sstatic.net/dhtOw.png Here is the code snippet ...

Deactivate the other RadioButtons within an Asp.net RadioButtonList when one of them is chosen

Is there a way to disable other asp.net radio buttons when one of them is selected? I have three radio buttons, and I want to disable the other two when one is selected. After doing some research, I managed to disable the other two when the third one is se ...

Revitalize a section of the webpage following a user's successful login on ASP.NET MVC4

This is the Index view of the Home controller @{ ViewBag.Title = "Index"; } @if (Request.IsAuthenticated) { @Html.Partial("_UserIsAuthenticated") @Html.Partial("~/Views/Task/_Tasks.cshtml") } else { @Html.Partial("_UserIsNotAuthenticated" ...

Troubleshooting a hover problem in CSS navigation bar

Having some trouble with the CSS menu on my website click here. I've tried adjusting the code, but can't seem to get all the menu levels to line up properly. As a beginner in CSS, I'm sure I'm overlooking something simple. When hovering ...

Cells within a table are equivalent

I am attempting to ensure that all table cells match the size of the image, but for some reason, they are not aligning properly. Visit this link for reference. Here is a condensed version of the HTML code: <table> <tr> <td> ...

What is the best way to access the attributes of a particular object following a triggered event?

I'm a bit unsure if my title makes sense, so allow me to explain. In a nutshell, I have 2 li elements within a ul list. What I aim for is that when one of these li elements is clicked, the text within that particular li is retrieved, and then an obje ...

"Marquee animation functions correctly on the emulator but fails to display on the physical

Greetings! I recently incorporated a marquee tag into my application, and it functions properly in the Android simulator. However, once I install it on my device, the marquee does not display. Below is the code I used for the marquee: <marquee behavi ...

What could be the reason for my jQuery function functioning in IE8 but not in FF3.6.11?

I have a group of checkboxes with an option for 'None'. When 'None' is clicked, it should uncheck the rest of the checkboxes in the set. If any other checkbox is clicked, 'None' should be unchecked. This functionality is worki ...

The Django ajax request returned an unexpected error when parsing the JSON response

I am attempting to implement a Django AJAX HttpResponse JSON functionality using medium-editor. view.py def test(request, union_id): if request.is_ajax(): t = Union.objects.get(id=union_id) message = json.loads(request.body) t.description = m ...

jQuery modal does not open when dynamic buttons are clicked

I am currently working on a tabbed panel that contains Datatables lists with buttons for editing or removing each row in the tables. The issue I am facing is that only the edit actions button in the first pane works correctly, while the buttons in the othe ...

Instructions for encoding a video using PHP, saving the encoded video to a file, and then decoding and storing it in a database

I have successfully encoded a video in PHP using HTML, and now I am trying to figure out how to decode and save it in PHP. However, I'm uncertain about the process of saving the encoded video and how to access it for decoding. Any assistance would be ...

Modifying the background image of the <body> element in CSS to reflect the season based on the current month in the calendar

I'm struggling to change my HTML background based on the date. The code I've tried isn't working as expected and I can't seem to find any relevant examples to guide me. My goal is simple - I want the background of my HTML page to be ch ...

Ensuring that links are functional across all directory levels: a comprehensive guide

Looking at the image included here, you can see a preview of the website I'm currently building. The plan is to have individual pages for each machine in the company's lineup. Since the navigation bar and menu will be consistent across the entire ...

Encode session data for Ajax requests

I am trying to serialize session content for an Ajax request. I need something that works similar to this example: var data = $.session("data").serialize(); This is intended for a standard jQuery ajax function, like so: $.ajax({ type: "POST", url: ...