Having Trouble with the Click Function for Div Dropdown on iPhone

After some investigation, it seems that the issue I encountered was not directly related to my code. Interestingly, when I click the link in the navigation bar, it redirects to "mobile/folder/page.html" on all browsers except for Chrome. In Chrome, however, it opens a different URL path ("/mobile/#/mobile/folder/page.html"), which causes it to malfunction. If I manually navigate to the page, everything works as expected. Does anyone have a solution for this?

While my code functions smoothly across various browsers and Android devices, I'm facing compatibility issues specifically on iPhones. I've experimented with using 'tap' and 'touch' events instead of 'onClick', switching to 'toggle' rather than 'hide' and 'show', incorporating onClick in CSS, and including cursor:pointer in CSS properties, but none of these adjustments have resolved the problem. Any guidance would be greatly appreciated!

https://jsfiddle.net/anthonyparrett7/0bmw79yw/

$(document).ready( function() {
/* Question and Answer Dropdowns */     
$(".dropdown-link").click(function(e) {         
  e.preventDefault();
  var $div = $(this).next('.dropdown-container');
  $(".dropdown-container").not($div).hide();
    if ($div.is(":visible")) {
        $div.hide()
    }  else {
       $div.show();
    }
});
$(document).click(function(e){
    var p = $(e.target).closest('.dropdown').length
    if (!p) {
          $(".dropdown-container").hide();
    }
});
});  

Answer №1

Make sure to properly initialize the function on page load.

Consider using $(document).ready(function() instead of

$(document).on("pageinit", "#page-container", function()

Take a look at this example:

$(document).ready(function() {
  $(".dropdown-link").click(function(e) {
    var $div = $(this).next('.dropdown-container');
    $(".dropdown-container").not($div).hide();
    if ($div.is(":visible")) {
      $div.hide()
    } else {
      $div.show();
    }
  });
  $(document).click(function(e) {
    var p = $(e.target).closest('.dropdown').length
    if (!p) {
      $(".dropdown-container").hide();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dropdown-1" class="dropdown dropdown-processed">
  <div class="dropdown-link">
    <h4>What is a Question?</h4>
  </div>
  <div class="dropdown-container" style="display: none;">
    <p>Bla bla bla blablablabla words Ipsum</p>
  </div>
</div>

<div id="dropdown-2" class="dropdown dropdown-processed">
  <div class="dropdown-link">
    <h4>What is a Question 2?</h4>
  </div>
  <div class="dropdown-container" style="display: none;">
    <p>Bla bla bla blablablabla words Ipsum</p>
  </div>
</div>

Answer №2

Have you considered trying a more straightforward approach, such as:

$(document).ready( function() {
    $(".dropdown-link").click(function(e) {
      $(".dropdown-container").hide();
      $(this).next(".dropdown-container").toggle();
    });
    $(document).click(function(e) {
      var p = $(e.target).closest('.dropdown').length
      if (!p) {
        $(".dropdown-container").hide();
      }
    });
  });

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

Is there a way to include a button at the top of the Notiflix.Loading() overlay for closing or stopping it?

I've integrated the "notiflix" library into my project, and I'm currently using notiflix.loading.pulse() for a lengthy process. While this works perfectly fine, I would like to add a button (for closing/stopping the process) on top of the loading ...

Temporary pseudo-element that appears upon hovering

My animated arrows, implemented using pseudo elements, are experiencing an unusual behavior. When the link is hovered over, the arrows animate smoothly. However, upon leaving the link, a pseudo element briefly appears in the top left corner of the screen b ...

What is the best way to apply the CssClass "active" when clicking on a link

How can we update the cssClass of a link button on each click event, considering that the page refreshes every time? Currently, when I click on any LinkButton, the default behavior sets the cssClass to Plus LinkButton. ---index.aspx----------- <ul cl ...

Encountering a blank screen on the Swagger route in ExpressJs: How to troubleshoot

I have set up swagger-ui-express with swagger-jsdoc in my Express.js project following various tutorials and official documentation. However, despite implementing everything correctly, I am facing an issue where the Swagger URL displays a blank white scree ...

Trouble with Javascript slideshow: Incorrect display and malfunctioning

Struggling with implementing a slideshow banner on my webpage, despite following the W3 tutorial. Here is the code I am currently using: HTML: <div class="slide-content" style="max-width:1000px"> <img class="slidepic" src="testheadphoto.jpg" st ...

Developing Multiple Views in Django

I am completely new to using Django and I am facing a challenge in creating multiple views with corresponding buttons on the main page. I managed to create different views for various filters of the table, but now I am stuck trying to figure out how to set ...

The issue code R10, indicating a boot timeout, has arisen as the web process failed to connect to $PORT within 60 seconds of initiating on Heroku platform

After deploying my Node.js WebApp to Heroku, I encountered the following error: 2021-06-01T09:19:42.615419+00:00 heroku[web.1]: State changed from crashed to starting 2021-06-01T09:19:47.259832+00:00 heroku[web.1]: Starting process with command `node app.j ...

Challenges with displaying content in Bootstrap tabs

I am currently working on the following code: HTML <div class="card"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs" role="tablist" id="tabs_list"> <li class="nav-item"> <a class="nav-li ...

Once the data in the React context has been updated, the next step is to trigger a re-render of the

In my React application, I have implemented a system where certain components will display based on whether the user is logged in or not. To ensure this state is shared throughout the entire application, I created a context file called AuthProvider: import ...

Calculate the percentage using JavaScript, then convert the percentage back to the target value

In one place, I am determining the percentageDifference by adding the baseValue and targetValue. However, in another location, I am calculating the targetValue using the baseValue and percentageDifference. The problem I encounter is that the result of the ...

What is the best way to implement multiple ngIf conditions in Angular code?

Is it possible to set multiple conditions in an ngIf statement? I have created a template for multiple users, but there are some content that I don't want certain users to see, such as Super Admin, Admin, Section Users, Division User, and Unit Leader ...

What methods can be used to identify modifications in a URL using a chrome extension?

Currently, I am diving into the world of creating chrome extensions. I encountered an issue where my context scripts fail to run unless onload is triggered. Even something as simple as alert("test"); doesn't work. This problem persists when ...

Looking to enhance the size of pagination icons for previous and next pages on a Material-UI React table?

I am looking to adjust the size of the previous page and next page icons in a material table implemented in React. Below is the code snippet: localization = { { body : {} , toolbar: { searchTooltip: 'Search'} , pagination: ...

ajax: ensure utf-8 encoding is applied

I am facing an issue with my webpage that is saved in UTF-8 encoding. In the head section of the HTML document, I have defined it as follows: <meta charset="utf-8" /> Within an external JavaScript file, there is a function called ajax_db() which ma ...

Guide on transferring datetime information from a popup dialog to an MVC controller

My goal is to create a button that, when clicked, opens a dialog allowing the selection of start and end dates. Upon clicking ok/submit, the selected datetime should be passed to a controller [HttpPost] action method. Here's what I have attempted so f ...

Combine various data sets in Apex Charts with the power of Vue.js for exciting visualizations

In my Vue.js project, I am attempting to generate N unique bar charts using a for loop. However, each chart currently displays the same data series. My goal is to bind different data series to each individual chart. I have tried the following approach: &l ...

Tips for reconstructing a path in Raphael JS

I am encountering a unique issue with my implementation of Raphael JS. Currently, I am working on drawing a donut element and seeking advice similar to the content found in this helpful link How to achieve 'donut holes' with paths in Raphael) var ...

How can I style a series of <li> elements to display next to each other using CSS?

Currently, I am working on enhancing a dropdown menu using CSS. One unique requirement is placing two separate rows of submenu items side by side within one dropdown menu. To better illustrate this, I have included an image for reference: If anyone has e ...

Submitting a JavaScript array to MongoDB using a React application: A guide to success!

As a beginner delving into the world of React and MongoDB, I'm encountering difficulties in establishing communication between the two technologies. Recently, I've been following a detailed tutorial on Medium that focuses on utilizing the Plaid A ...

Encountering an issue with blogs.map function in a Next.js application

import Head from 'next/head' import { useState } from 'react' import Image from 'next/image' import styles from '../styles/Home.module.css' const Home = (props) => { const [blogs, setblogs] = useState(props.dat ...