When collapsing on mobile, display all Bootstrap sub menus as expanded

I am currently utilizing wp-bootstrap-navwalker.php to generate bootstrap navigation menus on my Wordpress site. I am working on a project where the client requires the parent of a submenu in the navigation to be clickable. After modifying the code in wp-bootstrap-navwalker.php to allow for this functionality, I encountered some issues on smaller devices.

        // If item has_children add atts to a.
        if ( $args->has_children && 0 === $depth ) {
            $atts['href']           = '#';
            $atts['data-toggle']    = 'dropdown';
            $atts['class']          = 'dropdown-toggle';
            $atts['aria-haspopup']  = 'true';
        } else {
            $atts['href'] = ! empty( $item->url ) ? $item->url : 
        '';
        }

  $(window).resize(function() {
  //1200 is my modified breakpoint on this design for the collapse menu 
    if ($(window).width() < 1200)  {
      $(".dropdown-toggle").attr('data-toggle', 'dropdown');
    } else {
      $(".dropdown-toggle").removeAttr('data-toggle dropdown');
    }
  });

Since the parent menu item typically triggers an event that displays the submenu with child links, changing this functionality has made it only work on desktop browsers capable of hover events.

I have dedicated time trying to find a solution, and the only idea I have come up with is automatically expanding the collapsed bootstrap menu on smaller devices to show all parent and child items by default.

Despite my efforts, I have not been able to achieve this. Is there anyone who knows how this can be accomplished?

Answer №1

There's no need for that extra step. If you're working on mobile devices, all you have to do is include preventDefault() in your code. This will stop the default action of the event from occurring; essentially, the anchor link won't take you to the specified href, but instead, it will activate the dropdown menu.

$(window).resize(function() {
  if ($(window).width() < 1200)  {
    $(".dropdown-toggle").attr('data-toggle', 'dropdown');
  } else {
    $(".dropdown-toggle").on('click', function(e) {
      e.preventDefault();  
    }
  }
});

Give it a try and let me know how it goes.

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

Switch up the Javascript popup code without any specific pattern

I am looking to add variety to my pop up ads on my website by randomly changing the Javascript code for each ad every time a page is loaded. How can I achieve this? Script 1 <script type="text/javascript"> var uid = '12946'; var w ...

Utilizing Sticky First Column and Row Scroll in `ngx-datatable`

Is there a way to implement a scroll feature in a table to ensure that the first column and row remain fixed while scrolling, even when the number of rows or columns increases? <ngx-datatable id="table" #table class="" [rows]="t ...

Arrange flex list items in vertical rows on smaller screens for a more organized layout

Struggling to have CSS flex list stagger text on two lines for smaller screens, but so far, my attempts have failed. I've tried using spans and br at various media queries, but nothing seems to work. Any assistance would be greatly appreciated. This ...

Creating a custom HTML table with dynamically generated content - tips on controlling the width restrictions

I'm currently working on a project in MVC3 where I need to create a dynamic table. However, I've encountered an issue where the table extends beyond the page layout and doesn't stay contained within the shared div. @{ ViewBag.Title = " ...

Unable to drag and drop functionality is not functioning properly in Android Emulator with Html and jQuery

As a newcomer to Android, I have successfully created a project in HTML that runs perfectly as intended. However, when trying to run this HTML project on an Android emulator, I encountered an issue with drag and drop functionality not working. I utilized ...

Trouble Arising from the Lack of Coordination Between CSS Transition and JavaScript Update Triggered by Element

I'm currently working on a web development project that involves a list of clickable elements. When one of these elements is clicked, it should become active and trigger a CSS transition (such as a transform) with a duration of 200ms. Additionally, I ...

Implementing jquery into Angular 2 webpack project is essential as jQuery is necessary for Bootstrap's JavaScript to function properly

I am currently utilizing the angular-cli .net core starter. As I attempt to incorporate my own CSS and JavaScript files, I encounter the following error: An unhandled exception occurred while processing the request. Exception: Call to Node module fail ...

Arrange the Elements of Select Options

I am currently facing an issue with organizing the elements in my select list. I have two interconnected lists, one containing a list of "models" which I have successfully sorted using the following function: var sel = $('#model'); var opts_lis ...

What steps can I take to ensure this fading divider is responsive across all devices?

My current divider element is styled with CSS properties as follows: .divider { height: 2px; background: black; background: -webkit-gradient(radial, 50% 50%, 0, 50% 50%, 350, from(#000), to(#fff)); } I have two questions regarding this design. First ...

Having trouble displaying my klout score with ajax

I'm attempting to showcase a Klout score on a webpage Here's my code: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> var settings = { "url": "http://api.klout.co ...

How can the entire menu be closed in Bootstrap 5 when clicking on a link or outside of the page?

I'm currently utilizing BootStrap 5 for constructing my webpage. Within my page, I have a NavBar Menu containing various links. <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__ ...

Issues with responsiveness in CSS

I'm currently working on a website and facing some challenges. Right now, I'm building the dashboard section of the site, which includes a header with basic settings buttons (Home, Profile, and Log out), a navigation bar on the left side of the b ...

Is it considered safe to display the error message from an AJAX call for security reasons?

When I make my ajax calls, they usually follow this pattern: $.ajax({ type: 'POST', url: '/Controller/DoSomethingSpecial', contentType: 'application/json;', ...

Ways to conceal a div with jQuery

Is there a way to make the div disappear when the user clicks on the checkbox? I have tried using this code, but it doesn't seem to be working. $('input.maxtickets_enable_cb').change(function() { if ($(this).is(':checked')) ...

Detecting duplicate items with AJAX calls

Employing AJAX for a "Show More" functionality, I currently have my customized post type displaying 12 items initially. When the user clicks on the "Show More" button, another set of 12 post types are loaded onto the page. The Dilemma: Upon the first clic ...

How to create a "top 3-column bar" using divs and CSS

Looking to create a top bar with two images and text in the middle, but having trouble centering the text. This is specifically for mobile devices. Below is my HTML code: <img src="1.jpg" /> Company Name <img src="2.jpg" /> I attempted the f ...

When using the HTML5 input type time in Chrome and the value is set to 00:00:00, it may not be fully completed

After inputting the html code below <input id="time_in" type="time" step="1" name="duration"> and setting the value in the browser to "00:00:00", everything appears fine. However, upon inspecting the console or submitting the form, the value gets ...

The functionality of jQuery typeahead ceases to perform after I choose an option from the dropdown menu

Initially, my jQuery typeahead autocomplete function works perfectly. However, after selecting an option from the list, it fails to show results when I try to use it again by typing in the input field. It seems like the typeahead functionality gets destro ...

Trouble with comparing two datetime values in JavaScript

I have a dilemma with two different appointments: appointment1 = "2013-07-08 12:30:00" appointment2 = "2013-07-08 13:30:00" My goal in JavaScript is to compare these two appointment dates. If they don't match, I want to remove the appointment; if t ...

How to slowly adjust a CSS attribute using jQuery

Looking for a way to add a fade effect to this code: $('#uiAdminPanelMenu li a').hover( function(){ $(this).css('background-color', '#D3E1FA'; }, function(){ $(this).css('background-color', '#F4F4F4&a ...