Revise the mobile navigation opening feature to activate by clicking on a link instead of an icon

I've been struggling with a design issue for days now. I customized a template by editing its header and footer elements, incorporating Bootstrap 3 since the template was based on it. However, I recently discovered that Bootstrap 3 does not support submenus in the mobile navigation menu.

The problem lies in the mobile nav bar. To access links with submenus, users have to click on an icon located to the right of the link (referred to as the LINK icon). Clicking directly on the link itself does not trigger the submenu. Not only is the icon visually unappealing (even after switching from fontello to font awesome), but it also causes the link to shift off-center. Additionally, the clickable area is quite small. During testing, my friends attempted to click on the links first before realizing they needed to click on the icon.

I tried manipulating the JavaScript code, but being inexperienced, my efforts were fruitless. I also experimented with using media queries to hide/unhide one of the li elements; however, this method only worked for one link.

The mobile navigation is centered vertically.

Below you'll find the JS code snippet:

var $menu = $('.nav-menu', '#primary-navigation');

// add classes
$menu.find('li').each(function() {
    if($(this).children('ul').length) {
        $(this).addClass('has-submenu');
        $(this).find('>a').after('<span class="submenu-toggle"></span>');
    }
});

var $submenuTrigger = $('.has-submenu > .submenu-toggle');
// submenu link click event
$submenuTrigger.on( "click", function() {
    $(this).parent().toggleClass('active');
    $(this).siblings('ul').toggleClass('active');
});

Here's the corresponding HTML code:

<nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation"> 

<a class="menu-toggle"><span class="lines"></span></a>

    <div class="nav-menu">
        <ul>
            <li><a href="index.html">Home</a></li>
            <li><a href="#" class="selected">Explore</a>
                <ul>
                    <li><a href="ex_languages.html">Languages</a></li>
                    <li><a href="ex_gallery.html">Gallery</a></li>
                    <li><a href="ex_glossary.html">Glossary</a></li>
                </ul>
            </li>
            <li><a href="#">About</a>
                <ul>
                    <li><a href="ab_about.html">About</a></li>
                    <li><a href="ab_faq.html">Questions &amp; Answers</a></li>
                </ul>                            
            </li>
            <li><a href="blog/index.php">Blog</a></li>
            <li><a href="contact.html">Contact</a></li>
        </ul>
    </div>
</div>

The CSS stylings are included below:

.submenu-toggle:before {
    font-family: "Font Awesome 5 Free";
    content: "\f0d7";
    color:#fff; 
    font-size: 15px; 
    font-weight: 900; 
    display: inline-block; 
    width: 26px; 
    line-height: 24px; 
    text-align: center;
    margin-left: -8px; 
    margin-bottom: 8px;
    cursor:pointer;
}
.active > .submenu-toggle:before {
    font-family: "Font Awesome 5 Free";
    content: "\f0d8";
}

If anyone has a solution or suggestions, I would greatly appreciate the help. Ideally, both the link and the icon should be clickable, or the clickable area of the icon should overlap the link for better usability. Thank you :)

Answer №1

It is true that Bootstrap 3 does not provide support for sub menus in the mobile navigation menu. To address this issue, it is recommended to take matters into your own hands.

According to your reasoning, the trigger should look like this:

var $submenuTrigger = $('.has-submenu > .submenu-toggle, .has-submenu > a');

Additionally, you will need to incorporate a small hack:

$('.nav-menu > ul').on('click', function(event) {
    event.stopPropagation();
});

The purpose of this is to prevent event bubbling, which Bootstrap relies on - essentially, by implementing this code, you are overriding Bootstrap's functionality for sub-items.

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

The d3.select function is failing to update the chart on the website

I am facing a challenge in updating data in a d3 chart with the click on an HTML object #id. After successfully coding it in jsfiddle, I encountered issues when implementing it on a web page. The scenario involves a simple leaflet map where the chart is d ...

What could be causing my JavaScript loop to replace existing entries in my object?

Recently, I encountered an issue with an object being used in nodejs. Here is a snippet of the code: for(var i = 0; i < x.length; i++) { var sUser = x[i]; mUsers[sUser.userid] = CreateUser(sUser); ++mUsers.length; ...

Aligning an SVG icon at the center of a button

I have elements that are currently being used as buttons, but I want to switch them out for actual button tags. This would improve accessibility and allow keyboard navigation using the tab key to focus on my buttons. Each button contains a centered SVG ima ...

The CSS styles can vary depending on the web browser being used

Require assistance with my CSS. I designed an HTML/CSS widget on Blogger using my own expertise, but now facing some issues. The trouble arises with the image on the front left in the code. While it displays correctly on Google Chrome, aligned with the te ...

Updating selections by appending them with Jquery Chosen

I've searched high and low for a solution to this problem, but all I keep finding is information on dynamically adding more options. My function looks like this: $("#" + id_name).val(result).trigger("chosen:updated"); This function is triggered whe ...

Clicking on an image in a jQuery autocomplete menu will trigger a data post to an Express

My jquery autocomplete menu is functioning properly, displaying a list of books with author, title, and book image. I am now looking to enhance it by allowing users to click on the book image and then have the book title posted to an express app.post metho ...

Having difficulty retrieving data despite providing the correct URL

I am encountering an issue with a fetch function designed to retrieve a JSON web token. Despite verifying the correctness of the URL, the function is not functioning as expected. Below is the front-end function: const handleSubmit = async (e) => { ...

Having trouble finding the element with the xpath/css locator in Selenium

I am currently developing a tree view folder structure. Here is the code snippet I am working with: Code: package Selenium_Practice; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.o ...

Moving from traditional web pages to a mobile application using NextJS has brought about the error "rest.status is

Currently, I am in the process of upgrading from Next 13.2.5 to version 14.1.0 and switching to using app/api/example/route.js instead of pages/api/example.js. After making these changes, I encountered an error stating TypeError: res.status is not a funct ...

Implement Expressjs on AWS using Public ACM certificate

After developing my project in expressjs, I faced the challenge of deploying it on a specific port. The deployment on my AWS EC2 instance (Ubuntu) was successful, but it only ran on HTTP, not HTTPS. In order to enable HTTPS for my expressjs project, I rese ...

What is the best way to increase the padding in a Colorbox modal window?

Is there a way to increase the padding in a Colorbox modal window? I want some extra space between the edges of the modal window and the actual content. I experimented with the innerWidth and innerHeight properties, but unfortunately, I didn't notice ...

Create an input element using JavaScript/jQuery

Looking for some help with Javascript on a simple task. When a specific option is chosen, I want to add an input field to a div element. <select name="amount" > <option value="50">50$</option> <option value="100">100$</o ...

What is the best way to transfer ctx scope to an external function?

I am encountering an issue with the code where I need to keep the function checkIfMember within the context in order to pass it as an argument to another function. If I try to separate the function, ctx will be undefined. While this setup works, I would ...

Tips for maintaining consistency between server-side Java and client-side JS DTO properties

Greetings, I am in search of a solution or plugin within Eclipse that can ensure synchronization between server-side Java DTO properties and their corresponding client-side JSON elements as the codebase evolves. For instance, in a web application with a Ja ...

What causes the first navigation tab's content to change when selecting another tab?

There seems to be a peculiar issue with my Bootstrap tabs. Whenever I navigate away from the first tab to another tab and then return to the first tab, the content of the first tab gets replaced with the content of the last tab clicked. I have exhausted al ...

Difficulty in Verifying ECDSA Signatures Across React and Python Environments

Currently, I am tackling a project that requires me to create an ECDSA signature in a React application and then validate it in a Python backend. Although the signature generation and validation processes work perfectly within their respective environments ...

Is it possible to have animations play in reverse once they have completed running?

Hi there! I'm currently tinkering with the transitioning animations for my navigation button. I've successfully implemented the opening animation where the three bars morph into a single line and the menu slides out. Now, I'm looking to crea ...

Safely render user-generated HTML content within a React application to prevent cross-site scripting vulnerabilities

A unique situation has arisen in our React app where a user inputs a string that is displayed to other users. The challenge lies in searching for specific characters within the string and replacing them with HTML elements. For example, transforming the wor ...

Bootstrap datepicker embedded within a modal will scroll along with the parent page, rather than the modal itself

I recently encountered an issue with a bootstrap template where I needed to incorporate a datepicker within a modal. The modal contains scrollable content, but when I clicked on the datepicker field and scrolled the page, the datepicker moved with the pa ...

Implementing AngularJS to make asynchronous calls to the Facebook API

I am attempting to interact with the Facebook API using AngularJS. The issue I'm facing is that all API calls from Facebook are asynchronous, so I need a way to determine when my query on Facebook is ready to be used in AngularJS. To address this, I ...