Problem: Needing an additional click after selecting a menu item in Jquery

After countless days of seeking help on this platform, I am finally making my first post here to express my gratitude for all the support I have received.

I am diving into the world of mobile app development with my very first project - a calculator/converter app using PhoneGap (jquery + html). My background is primarily in .NET development with a few years of experience under my belt.

My current challenge involves hiding the menu after clicking an item. Despite trying different methods like .toggle() and .slideToggle(), none seemed to work perfectly for me.

Once I make a selection from the menu, everything functions correctly both in the browser and on PhoneGap. However, after execution, I notice that I need to click somewhere else on the page as if the focus has shifted elsewhere. I suspected it could be related to &ui-state=dialog, but even setting changeHash to false didn't resolve this behavior.

I've scoured through various threads for solutions without much success. Any assistance or guidance would be greatly appreciated :)

 <a href="#hamburgerPopup" id="burgerLogo" data-rel="popup" data-transition="slide" aria-haspopup="true" aria-owns="popupMenu" aria-expanded="false">
                    <img src="hamburger.png" style="width:50%;" />
                </a>
                <div data-role="popup" id="hamburgerPopup" data-theme="a" >
                    <ul data-role="listview" data-inset="true">
                        <li data-icon="false"><a href="#" id="calc" onclick="GoToCalc()">Calc Weight</a></li>
                        <li data-icon="false"><a href="#" id="faq" onclick="GoToFAQ()">FAQ's</a></li>
                    </ul>
                </div>

and the jquery

    $('#burgerLogo').on('click', function () {
        $('#hamburgerPopup ul').css("display", "block");
    });

    function GoToCalc() {
        $("#about").hide();
        $("#divCalculator").show();
    }

    function GoToFAQ() {
        $("#about").show();
        $("#divCalculator").hide();
    }
$(function () {
        $("#hamburgerPopup li").click(function () {
            $('#hamburgerPopup ul').css("display", "none");
            //$("#logo").click(); - I tried this to simulate a click, does nothing
            //$('#hamburgerPopup').trigger("click");
            //$("#hamburgerPopup").hide();
        })
    });

Answer №1

Do you need a demonstration of how to navigate pages using a hamburger menu? Here's a simple example for you:

$(function(){
  $( "#hamburgerPopup" ).enhanceWithin().popup({
    theme: "a",
    transition: "turn",
    history: false,
    positionTo: "origin",
    beforeposition: function( event, ui ) {
      var currPageId = $(":mobile-pagecontainer").pagecontainer("getActivePage").prop("id");
      $("#hamburgerPopup li").removeClass("ui-state-disabled");
      $("#hamburgerPopup a[href='#"+currPageId+"']").parent().addClass("ui-state-disabled");
    }
  });
  $("[data-role='header'], [data-role='footer']").toolbar({
    theme: "a",
    position: "fixed",
    tapToggle: false
   });
});

$(document).on("pagecontainerchange", function() {
  $("[data-role='header'] h2" ).text($(".ui-page-active").jqmData("title"));
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
</head>
<body>
  <div data-role="header">
    <a href="#hamburgerPopup" data-rel="popup" class="ui-btn-left ui-btn ui-btn-inline ui-mini ui-corner-all ui-btn-icon-left ui-icon-bars ui-btn-icon-notext" aria-haspopup="true" aria-owns="popupMenu" aria-expanded="false">View</a>
    <h2>Header</h2>
  </div>
  <div data-role="page" data-title="Calculator" id="page-calculator">
    <div data-role="content">
      Calculate Weight
    </div>
  </div>
  <div data-role="page" data-title="FAQ" id="page-faq">
    <div data-role="content">
      Read FAQ
    </div>
  </div>
  <div data-role="footer">
    <h2>Footer</h2>
  </div>
  <div data-role="popup" id="hamburgerPopup" data-theme="a">
      <ul data-role="listview" data-inset="true">
          <li data-icon="false"><a href="#page-calculator">Calc Weight</a></li>
          <li data-icon="false"><a href="#page-faq">FAQ's</a></li>
      </ul>
  </div>
</body>
</html>

Explanation:

To initialize the widgets outside the pages, like the header, footer, and hamburger menu, manual initialization in the code is required. This allows for more control over the options available during widget setup, eliminating the need for certain unnecessary data tags.

The page navigation and popup closing functionality is already built into jQuery Mobile through anchor tags, so additional JavaScript code is not needed to handle these actions.

Additional features include disabling the menu item for the current active page, achieved through the Popup beforeposition function by adding or removing jQM classes in the hamburger menu listview.

Setting the page title manually is necessary as the header remains constant across all pages. A custom tag data-title stores the text information to display when switching between pages.

I hope this explanation helps!

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

using CSS to position elements that are generated dynamically

Hey there, I'm facing a little issue with my elements. These elements are being generated dynamically and I'd like to display them in a 4 column layout. The challenge is that they arrive separately and I can't simply wrap them in a div and u ...

Performing an HTTP POST request in Angular 2

After starting my work with Angular 2 and TypeScript, everything was going great. However, I encountered an issue when working with a REST API (POST) where the console log displayed Response {_body: "", status: 204, statusText: "Ok", headers: Headers, type ...

Display a dialog box in jQuery UI 1.7 autocomplete when an item is selected

After implementing an autocomplete widget, I noticed that a dialog appears when an item is selected. However, I am struggling to get a specific field in the dialog to receive focus upon opening. Here is what I have attempted so far: //HTML <form actio ...

Tips for Preventing Ant Design from Overriding Existing CSS Styles

Currently, I am working on a ReactJS project. Initially, the entire project was designed using pure CSS. However, I decided to incorporate a small Antd component into my project. Following the provided instructions, I imported the component like this in on ...

What is the best way to create space between table rows without using border-spacing?

I've been attempting to use the margin-bottom property on my table in order to separate the rows, but it doesn't seem to have any effect. I then tried using flex, which did work, but the table rows did not inherit the width and I had to specify a ...

A DataTable cannot be reinitialized within an AJAX response

After a user chooses an option from a select dropdown and hits submit, an ajax call is made. If data is found, it will be displayed in a data table. If no data is found, an alert saying "No data found" will be displayed. While the above scenario is workin ...

I tried to import Bootstrap into my project, but it seems to be malfunctioning

Is there a way to display cards in a slider format? I want the carousel to show the next card on each click. I tried importing Bootstrap into my project but it doesn't seem to be functioning as expected. Here's the link to the tutorial I am usi ...

Split CSS files apart with code-splitting in create-react-app without the need to eject

I have implemented react-loadable to enable code-splitting in my application, as it has grown too large to manage without this feature. While the code-splitting is functioning properly, the CSS is being embedded within the new JavaScript chunks and loaded ...

Assertion error: 1 does not match the expected value of 6

I am faced with a challenging test that requires me to write code that will pass successfully. However, no matter what I try, all I get is the following error messages: "Failed asserting that 1 matches expected 6" when I return 6 and "Too few arguments t ...

Obtaining information from within the <div> element containing data in <p> tags

Seeking to extract information from the specified tag. Successfully completed the following steps. Document doc = Jsoup.parse(currMsg); Elements ele = doc.select("p"); This code returns <p>data</p> I am looking for only data. Next, attempte ...

Add different input strings to an array within the scope when there is a change in the input value (AngularJS)

My goal is to populate an empty array within the scope with strings. The number of elements in this array can vary depending on what the user types in the player count input field. $scope.playerNames = []; To achieve this, I am using a $watch function th ...

<ul> hover effect and text </ul>

Looking for a solution to activate hover state on both the box and text when rolling over tiled images with text underneath. Check out this example in the Fiddle: http://jsfiddle.net/techydude/GF8tS/ Any suggestions on how I can achieve this effect? ...

Retrieve the chosen items from a Syncfusion listview

I'm looking to display the selected items from a listview in a grid on the home page, but I'm having trouble figuring out how to do it. I've included the code from the js file and a screenshot of the popup. var subItemsLoaded = false, ...

What is the process of generating enum values using ES6 in Node.js?

Can JavaScript support enumerations with assigned integer values, similar to how other programming languages handle it? In C#, for instance, I can define an enum as follows: enum WeekDays { Monday = 0, Tuesday =1, Wednesday = 2, Thursday ...

Ensuring the container height remains consistent with fluctuating content dimensions

Imagine a container with content, where the container's width is fixed but its height adjusts based on its inner content. Initially, when the content causes the container to be a specific height, the challenge arises in shrinking the inner elements w ...

Showing events from MySQL database on Vue.js Fullcalendar

I am trying to fetch events from my MySQL database and pass them to my Vue component to be displayed on the FullCalendar. However, the event array is being populated with a full HTML document. Below is my EventController: public function getEvents() { ...

Struggling with adding headers in React application

When I try to include an h1 heading, either the heading doesn't show up if I comment out the buttons, or if I comment out the heading, then the buttons don't display. But when both are included, nothing is displayed. Any suggestions on how to fix ...

Obtaining the client's IP address using socket.io 2.0.3: a comprehensive guide

Currently, I am facing a challenge using socket.io v2.0.3 in my node.js server as I am unable to retrieve the client's IP address. Although there are several suggestions and techniques on platforms like stackoverflow, most of them are outdated and no ...

Deleting MySQL records with JQuery and PHP

Gradually, I've been transforming a PHP web application that I'm currently developing into Jquery AJAX form submissions. The specific issue I am tackling involves removing table rows. It seems like there is an issue with my JavaScript code becau ...

Show submenu upon hovering and make sure it remains visible

Is there a way to keep a submenu displayed and the background color changed even after moving the mouse away from the menu item onto the submenu? This is my HTML: <div class="shoplink"><a>Online Shop</a></div> <div id="shop-men ...