Looking to include a sub element in my menu items using jQuery and CSS

Using jQuery and CSS, I have created a menu item. How can I navigate from one menu item to another sub menu item, as illustrated in the image below?

Below is my HTML markup:

<link rel="stylesheet" href="css/style_j.css" type="text/css" media="screen"/>
    <div class="content">
        <ul id="sdt_menu" class="sdt_menu">
            […] <!-- HTML code continues -->
        </ul>
    </div>
<div>
    <span class="reference">

    </span>
    </div>

Here is my CSS:

[CSS code here]

And here is my Javascript code with jQuery:

[Javascript/jQuery code here]

Answer №1

If you're already using unordered lists for your top-level nav items, I recommend sticking with that format as you go deeper into the hierarchy. Changing things up unnecessarily will only add complexity to your code. However, if you do want to continue with your current structure (using My Work > Websites as an example), consider putting the link inside a container and hiding the sub-menu within that container. This will allow you to easily target the sub-menu when the link is clicked or hovered over.

<!-- just a snippet -->
            <span class="sdt_descr">My work</span>
        </span>
    </a>
    <div class="sdt_box">
        <div class="nameThisSomethingMeaningful">
            <a href="#">Websites</a>
            <div class="hiddenSubmenu">
                <a href="amazingthingImade.com">Amazing Website</a>
                <!-- more links here -->
            </div>
        <div>
        <div class="nameThisSomethingMeaningful">
            <a href="#">Illustrations</a>
            <div class="hiddenSubmenu">
                <a href="mysite.com/illustration/client1">Client Work 1</a>
                <!-- more links here -->
            </div>
       <div>
<!-- etc. etc. -->

<!-- now the JS -->
<script>
$(document).ready(function(){
  $(".sdt_box a").on("click", function(){ // feel free to use mouseenter or mouseleave, or w/e
    $(this).parent().find(".hiddenSubmenu").fadeIn();
  });
});
</script>

However, for simplicity and consistency in your navigation structure, it's best to treat each level of the menu the same way. Since you're already using <li>s for the top-level nav, I suggest continuing that pattern to maintain a natural hierarchy without having to deal with complex DOM traversal. Here's how you can approach it:

  <nav>
    <ul>
      <li><a href="#">Portfolio</a>
        <ul class="subMenu">
          <li><a href="#">Websites</a>
            <ul class="subSubMenu">
              <li><a href="...">Client 1</a></li>
              <li><a href="...">Client 2</a></li>
              <li><a href="...">Client 3</a></li>
            </ul>
          </li>
          <li><a href="#">Illustration</a>
            <ul class="subSubMenu">
              <li><a href="...">Client 1</a></li>
              <li><a href="...">Client 2</a></li>
              <li><a href="...">Client 3</a></li>
            </ul>
          </li>
        </ul>
      </li>    
      <li><a href="#">Some Other Menu</a>
        <ul class="subMenu">
          <li><a href="#">Some Other Submenu</a>
            <ul class="subSubMenu">
              <li><a href="...">Client 1</a></li>
            ...

To create a visually appealing layout with proper hierarchy, utilize CSS positioning techniques like relative and absolute positioning. This will help communicate the menu structure effectively. You seem familiar with CSS, but here's a rough version of how you could replicate your current layout along with a third-level submenu floating to the right of the item:

/* basic boilerplate stuff */
* {  box-sizing: border-box;
  margin: 0px; padding: 0px;
  font-family: Helvetica, Arial, sans-serif;
}
a {
  color: #aaa;
  font-weight: 600;
  transition-duration: .3s;
  text-decoration: none;
}
/* end of boilerplate */

nav li {
  ...
}

https://i.sstatic.net/jpbXd.png

For a more detailed implementation with JavaScript handling hover effects, check out this source code:

Simply click on "view code" at the top right.

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

Adjust 3D Model in ThreeJS to Fit Bounding Box Dimensions

I need to adjust the scale of the imported GLB Model to match the size of a cube in my scene. This is necessary to ensure that the model remains within the shadow casting areas and is large enough to display the shadows effectively. I have already determi ...

Tips for importing a .geojson document in TypeScript using webpack?

I am trying to extract data from a .geojson file but faced some challenges while attempting two different methods: const geojson = require('../../assets/mygeojson.geojson'); The first method resulted in an error message stating: Module parse f ...

What criteria does Angular use to determine when the aot compiler should be utilized?

This page discusses the concept of modules in Angular and explains the two approaches to bootstrapping - dynamic and static. The configuration for these approaches is typically found in main.ts: // Using the browser platform with a compiler import { platf ...

A guide on verifying a phone number using just one character

I am looking to validate a phone number with only one character being allowed. For example, the format should be XXX-XXXXXXX where "-" is the only allowed character. Below is my validation function: function validatePhoneNumber() { if(addform.staff_m ...

Creating a function within an ajax call presents a challenge that needs to be

Below is an example of my ajax function: for (var i = 0; i < 50; i++) (function(i) { jQuery('#skin_'+i).click( function() { var result = $('#idskin_'+i).val(); $.ajax({ url: 'aja ...

Creating input fields dynamically within Yii2 framework

I'm currently facing a challenge with forms in Yii2. My objective is to design a form that includes three dropdown menus, inquiring about the user's preferred time(s) during the week. The first menu will focus on selecting days of the week, while ...

Retrieve HTML content from a JSON object and render it on a web page

I am facing a challenge with decoding html that is in json format. I'm struggling to figure out how to retrieve my html and display it on a page. It seems like json_decode isn't the solution. Can anyone help me with this issue? Appreciate any as ...

Sending JSON Data from C# to External JavaScript File without Using a Web Server

Trying to transfer JSON data from a C# (winforms) application to a static HTML/JavaScript file for canvas drawing without the need for a web server. Keeping the HTML file unhosted is preferred. Without involving a server, passing data through 'get&ap ...

Transferring information from a Java file to AJAX

I have a Java class that prints progress as 1%, 2%, 3% of a process. Now, I am looking to utilize AJAX requests to read this progress and update it continuously on the webpage. Unfortunately, I cannot convert the Java file into a servlet since it is being ...

Issue: A SyntaxError is triggered due to an unexpected termination of the JSON input while utilizing the fetch()

I encountered an issue while working on sending JSON between a Go API server and a React front-end. The error message I received was: Error: SyntaxError: Unexpected end of JSON input The specific line where the error is occurring is Line 25, which contai ...

Managing variables or properties that are not defined in ES6

Currently, I am utilizing Angular and Firebase Firestore in my project. However, the question posed could be relevant to Typescript and pure Javascript as well. The functionality of my application relies heavily on the data retrieved from Firestore. A sim ...

Is there a way to search for a specific item within a nested array?

I have 2 arrays within an array, each containing objects. How can I locate the object with the name "Sneijder"? const players = [ [ { id: 1, name: "Hagi", }, { id: 2, name: "Carlos", }, ], [ { id: 3 ...

Struggling to incorporate JSON data and javascript functions into an HTML file

I've been struggling to create a feed from a json link and display it in separate divs within an html document. Despite multiple attempts with different approaches for three different newspaper sources, I have not been successful. I'm hoping som ...

Prevent external scrolling while Bootstrap modal is active

<div class="modal mt-5p" role="dialog" [ngStyle]="{'display':IONotes}"> <div class="modal-dialog modal-md mt-0px width-70p"> <div class="modal-content" style="height:500 ...

In Internet Explorer 11, React 15.4.1 does not support using objects as valid child components

When trying to render a collection of children in React, make sure to use an array instead of objects. If you encounter the error "Objects are not valid as a React child," consider wrapping the object using createFragment(object) from the React add-ons. Do ...

Keeping the scroll in place on a Bootstrap4 modal while scrolling through content that is already at the top

On my Bootstrap 4 modal, I have encountered an issue with scrollable content. When I am at the top of the content and try to scroll downwards, nothing happens because I am already at the top. However, if I continue to hold the touch without releasing it, t ...

The Wagtail/Django block is struggling to properly display content from a custom or nested StructBlock template

I have a block in the header of my base template that will apply "extra" CSS styles. These styles are dynamic and based on fields from a Wagtail CMS instance. In the base.html template, I define: <head> {% block extra_css %}{% endblock %} </he ...

Creating an HTML form that submits data to a PHP script, which then saves the

Looking for some assistance with a PHP issue I am having. I know my way around PHP, but I wouldn't say I'm an expert. Here's the problem: I have created an HTML form (form.html) and now I want the output from formular.php to be saved as a u ...

Manipulating and transforming data through Puppeteer's iterative process into an object structure

As a beginner with the puppetteer library, I'm trying to iterate through Amazon reviews and save each comment as an object. Although my code seems to be functioning, it only retrieves the first comment and then stops. async function scrapeProduct(ur ...

Selenium WebDriver is struggling to locate an element using link text

I'm currently facing a challenge in selecting an element that contains an anchor within a paragraph inside a div. The HTML structure I am working with is as follows: <a class="item" ng-href="#/catalog/90d9650a36988e5d0136988f03ab000f/category/DATA ...