Numerous toggles available for the mobile version

Hey there, I need some help! I have a footer navigation on my desktop website with 3 Ul elements. I want to turn each Ul into a toggle for the mobile version. Can anyone assist me with this? Thanks in advance!

   <footer class="footer">
     <div class="wrapper">
      <nav class="footer-navigat">

        <ul>
          <li><h2>Title</h2></li>
           <li><a href="#">prod1</a></li>
           <li><a href="#">prod2</a></li>
           <li><a href="#">prod3</a></li>
             <li><a href="#">prod2</a></li>
           <li><a href="#">prod3</a></li>
        </ul>
        <ul>
          <li><h2>Title 3</h2></li>
           <li><a href="#">prod1</a></li>
           <li><a href="#">prod2</a></li>
           <li><a href="#">prod3</a></li>
             <li><a href="#">prod2</a></li>
           <li><a href="#">prod3</a></li>
        </ul>
          <ul>
          <li><h2>Title 3</h2></li>
           <li><a href="#">prod1</a></li>
           <li><a href="#">prod2</a></li>
           <li><a href="#">prod3</a></li>
             <li><a href="#">prod2</a></li>
           <li><a href="#">prod3</a></li>
        </ul>
      </nav>
    </div>
  </footer>

Answer №1

This solution provides a simple fix for your problem.

To address the issue, you should adjust your html structure by including a sub ul within the existing ones like this:

<ul>
    <li class="clickme"><h2>Title</h2></li>
    <ul class="sub-nav">
        <li><a href="#">prod1</a></li>
        <li><a href="#">prod2</a></li>
        <li><a href="#">prod3</a></li>
         <li><a href="#">prod2</a></li>
        <li><a href="#">prod3</a></li>
    </ul>
</ul>

If you wrap the following code in a media query so that it only applies at mobile widths, it will hide the sub nav:

.subnav {display: none;}
.show-nav {display:block;}

Lastly, utilizing the provided jQuery script allows you to toggle a class that changes the display properties of the sub-nav section:

$(document).ready(function() {
    var button = $('.clickme');
    var subnav = $('.sub-nav');

    button.on('click',function(){
       subnav.toggleClass('show-nav'); 
    });
});

You can view all this in action through this fiddle link: http://jsfiddle.net/uuL2wyLk/


UPDATE:

Fiddle updated at: http://jsfiddle.net/uuL2wyLk/2/

The revised fiddle demonstrates the application of the display:none; property only when the window width falls below 500px. Additionally, upon page load, the script checks if .sub-nav has display:none;, enabling the accordion functionality only under certain conditions.

When viewed on mobile devices, the display property triggers due to the media query, allowing the script to execute. Conversely, desktop displays lack said property and thus do not activate the accordion function.

By testing the new fiddle with different viewport sizes, you'll observe how the accordion behavior adjusts accordingly. Shrinking the result section activates the accordion, while expanding it deactivates this feature.

Access the updated fiddle here: http://jsfiddle.net/uuL2wyLk/2/


UPDATE 2:

Latest Fiddle Link: http://jsfiddle.net/uuL2wyLk/3/

This iteration enhances the functionality to handle multiple menus. The HTML now includes both the sub nav content and click button within the same <li> element:

<li>
    <h2 class="clickme">Title</h2>
    <ul class="sub-nav">
        <li><a href="#">prod1</a></li>
        <li><a href="#">prod2</a></li>
        <li><a href="#">prod3</a></li>
        <li><a href="#">prod2</a></li>
        <li><a href="#">prod3</a></li>
    </ul>
</li>

With the added jQuery event listener, clicking on the title triggers the sibling .sub-nav section to reveal itself by applying the .shownav class. Here's an excerpt from the provided code snippet:

button.on('click',function(){
    $(this).siblings(subnav).toggleClass('show-nav'); 
});

For comprehensive details on implementing this setup, refer to the latest fiddle: http://jsfiddle.net/uuL2wyLk/3/

Answer №2

Implementing the suggested behavior involves wrapping the li in a content class and the title in a toggle class. This enables automatic closing of other open menu items when clicking on a secondary menu item.

HTML

    <ul>
      <li class="toggle">Title 1</li>
       <li class="content"><a href="#">prod1</a></li>
       <li class="content"><a href="#">prod2</a></li>
       <li class="content"><a href="#">prod3</a></li>
       <li class="content"><a href="#">prod2</a></li>
       <li class="content"><a href="#">prod3</a></li>
    </ul>
    <ul>
       <li class="toggle">Title 2</li>
       <li class="content"><a href="#">prod1</a></li>
       <li class="content"><a href="#">prod2</a></li>
       <li class="content"><a href="#">prod3</a></li>
       <li class="content"><a href="#">prod2</a></li>
       <li class="content"><a href="#">prod3</a></li>
    </ul>
      <ul>
       <li class="toggle">Title 3</li>
       <li class="content"><a href="#">prod1</a></li>
       <li class="content"><a href="#">prod2</a></li>
       <li class="content"><a href="#">prod3</a></li>
       <li class="content"><a href="#">prod2</a></li>
       <li class="content"><a href="#">prod3</a></li>
    </ul>
  </nav>
</div>

JQUERY:

$(function() {
  $("li.content").hide();
  $("ul.navigation").delegate("li.toggle", "click", function() { 
  $(this).next().toggle("fast").siblings(".content").hide("fast");
  });
});

CSS:

.toggle { color: blue; }
.content { border: slid 1px #ddd; padding: 5px; }

For similar behavior, check out this example fiddle http://jsfiddle.net/nick_craver/K6TSv/1/

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

Issue with distinguishing JavaScript code from an SVG file

Seeking assistance as I have an SVG file that is mostly composed of a script. My goal is to separate the script for compression purposes, but I am struggling to find a way to achieve this. Any guidance or help on this matter would be greatly appreciated. ...

Troubleshooting a Border Problem in Bootstrap 5 Accordions

Having trouble with a Bootstrap 5 accordion header that has an icon drop-up menu. The bottom border disappears under the icon and I can't figure out why? HTML <div class="w-50 mt-5 mx-auto"> <div class="accordion favorites-f ...

The content within the tab is currently being loaded

Incorporating jQuery UI tabs into my webpage, I encounter a delay of 5-6 seconds when clicking on a tab as it triggers an ajax call for preparing and displaying content. The screen stays idle during this time, leading to user confusion. I am seeking a sol ...

Using AngularJS to convert a JSON object into an array

Hey there, I've got a JSON return object that looks like this: color_selected = [ { id: 4}, { id: 3} ]; Any tips on how I can convert it to the following format? color_selected = [4,3] Appreciate any help or s ...

"Issues Arising from Compatibility between Internet Explorer, jQuery,

I need help creating a function that will transfer items from the basket to the cart. The code I have written works well in Firefox and Chrome, however, it is not recognizing the products. var modals_pool = {}; $('.deal_order_btn').on('clic ...

What drawbacks come with developing an Express.js application using TypeScript?

Curious about the potential drawbacks of using TypeScript to write Express.js applications or APIs instead of JavaScript. ...

In ReactJS, the way to submit a form using OnChange is by utilizing the

Is there a way to submit a form using Onchange without a button? I need to fire the form but can't insert routes as it's a component for multiple clients. My project is built using react hook forms. const handleChange = (e: any) => { c ...

JavaScript function to copy the first row of a table to the clipboard

I am new to JavaScript and I'm facing an issue with copying cells in a table. I have successfully created a table based on my MySQL database, but when I try to copy a cell using the copy button, only the first cell gets copied. I understand that I nee ...

The 'props.p' navigation in react-native is undefined

I have gone through various forums and discussions regarding this issue, but none of the solutions seem to work for me. For some reason, I am facing difficulties passing props to react-navigation when I attempt to navigate, resulting in the following erro ...

AngularJS implemented to trigger a popup alert after a certain duration of time has elapsed since the

Can we create a popup alert that says "Error Contacting Server" when the http request does not receive any response? .controller('items_ctrl',['$scope','$http',function($scope,$http){ $scope.shop_id=localStorage.getItem(" ...

Steps for incorporating the count() value in Django for web developmentTo embed the count() value while creating

I'm currently working on a web visual board project using Django. Here is the specific HTML section where I need to populate values from the database: <tbody> {% for obj in data_list %} <tr> <th>{{ ob ...

Comparing transition effects: scale, transform, and all

My goal is to apply transition effects to specific transform functions in CSS, such as scale(), while excluding others like translate(). Initially, I attempted the following code snippet: input:focus + label, input:not(:placeholder-shown) + label { tran ...

Techniques for finding the total value of a JSON array

After retrieving values from a database, I am using them in JSON/javascript as an array. For example, see The issue arises when trying to calculate the sum of the array elements. I attempted to solve it with this code: var obj1 = JSON.parse(data); var m ...

Is it possible to turn off the fallback color for bourbon radial gradients?

Is there a way to prevent the fallback from being applied when using the radial gradient mixin in Bourbon? I've consulted the documentation at , which states that the fallback is optional. However, no matter what I attempt, it seems that the primary ...

Is there a way for me to display an alert notification when a website requests access to additional storage on my computer?

Is there a way to set up an alert notification in Internet Explorer when a website requests additional storage on your computer? The notification would ask: Do you want to grant permission for this website to use additional storage on your computer? ...

Button for AngularJS delete request

I have developed a live polling application that allows users to create, view, and delete questions from the table pools stored in the RethinkDB database. The issue lies with the delete functionality. While sending a DELETE request using POSTMAN successfu ...

Eliminating and restoring the background in a form field when it is in focus

/* This function clears the default text in a field when clicked on */ $('#edit-field-activity-notes-und-0-value') .each(function() { $(this).data('default', this.value); var mybackground = $(this).css('backgroun ...

Is Flex still wrapping elements even when set to nowrap?

My goal is to create a layout with 4 blocks, where the first section contains 2 blocks each occupying 50% of the width, followed by 2 other blocks in the next section. I am utilizing the flex property for this layout. I want the flex-container to take up ...

Tips for managing multiple input fields sharing the same name and submitting to PHP

If I have an HTML structure like this where each left-context-field and right-context-field pair are related, how can I link the left to its right counterpart in the backend? <table> <tbody> <tr> <td> <inpu ...

Is CodeMirror's PHP mode failing to function properly?

There seems to be an issue with displaying the highlighted text, and I'm not sure why. Links: <link rel=stylesheet href="http://codemirror.net/mode/php/../../doc/docs.css"> <link rel="stylesheet" href="http://codemirror.net/mode/php/../../l ...