iPhone - touchstart event not functioning in JavaScript

Looking for a solution to dynamically resize a div in JavaScript when clicking on a link element (<a>). However, the code doesn't seem to function properly on my iPhone.

Let's start with a basic link:

<a href="#" onfocus="menu()">MENU</a>

When this link is clicked, it expands a specific div to fill the screen and displays content. Inside that div, there's another link to close the expanded area:

<a href="#" onfocus="revert()">close</a>

Here's the corresponding JavaScript code:

<script>
        var headerHeight = document.getElementById("header_container").clientHeight;
        function menu() {
            document.getElementById("nav_container").style = "display:block;";
            document.getElementById("header_container").style = "height:100%";
            document.getElementById("header").style = "height:100%";
        }
        function revert(){
            document.getElementById("nav_container").style = "display:none;";
            document.getElementById("header_container").style = "height:"+headerHeight+";";
            document.getElementById("header").style = "height:"+headerHeight+";";
        }
</script>

Although this code works perfectly on desktop browsers like Firefox Developer Edition in responsive design mode with touch events simulated, it fails to perform as expected on my mobile device 😞

Upon clicking the button, nothing happens. Any suggestions or insights on how to resolve this issue?

Answer â„–1

When an element is prepared to accept keyboard input, it is considered 'focused'. Perhaps using onclick would be more suitable for your needs.

If you are not directing to another page, it's recommended to use a button in your HTML code:

<button onclick="menu()">MENU</button>
<button onclick="revert()">close</button>

The above code will function correctly, but it can be improved. In order to make this work smoothly, ensure that the functions menu and revert are defined first, prior to including your buttons. This requires loading your JavaScript before the DOM, which prevents referencing the DOM within your script.

To enhance performance, place your JavaScript after all DOM elements have loaded and bind your events accordingly:

function menu() {
  document.getElementById("msg").innerHTML = 'Showing menu';
}

function revert(){
  document.getElementById("msg").textContent = 'Not showing menu';
}

// Bind all events
document.getElementById('open-menu-button').addEventListener('click', menu);
document.getElementById('close-menu-button').addEventListener('click', revert);
<button id="open-menu-button">MENU</button>
<button id="close-menu-button">close</button>
<div id="msg"></div>    

Many resources suggest waiting for a domloaded event or enclosing everything in $( document ).ready(), but this isn't necessary if your JavaScript is positioned at the bottom of the page.

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 it truly impossible to align text around an image using flexbox?

In typical situations, you can easily float an image left or right to wrap text around it. However, in flexbox layouts, floating elements do not work as expected and finding a solution can be challenging. It is worth noting that Bootstrap 4 will be implem ...

Issue with Material-UI Dialog Reusable Component: No error messages in console and app remains stable

Here is a component I've created for reusability: import { Dialog, DialogContent, DialogContentText, DialogTitle, Divider, Button, DialogActions, } from "@mui/material"; const Modal = ({ title, subtitle, children, isOpen, hand ...

AngularJS bidirectional binding with numerous select choices

I am faced with a unique challenge - I have a list of non-exclusive fields that users want to see presented in a select box allowing multiple selections, rather than individual checkboxes bound to Boolean values in the model. While I know how to create t ...

Ways to validate an element in an Array using Cypress dynamically

I have a question regarding the dynamic verification of Array elements. For my project, I need to suggest a price that will increase over time, and I require a script that can verify these elements dynamically. In the screenshot provided, you can see what ...

Issue with passing reactive property to component in Vue 3 application

I am currently working on a Vue 3 application and I am in the process of setting up a store for state management. Within this application, I have several important files that play different roles: app.vue component.vue main.js store.js These files contai ...

The total number of rows in each section is determined by the count specified in the "numberOfRowsInSection

When attempting to configure this segmented table to showcase different JSON arrays in separate sections, I encountered an issue where having a larger row count in any section following the first resulted in the error message: [__NSArrayM objectAtIndex:]: ...

What is the best way to implement Bootstrap in Coda2?

I am new to web design and I am trying to incorporate Bootstrap into my HTML code using Coda2. Since I don't have access to a server, I am referencing the directory address of CSS files on my hard drive instead of a web address. Currently, the beginni ...

Authentication failed due to Bcrypt.compare() returning invalid credentials

const express = require('express'); const router = express.Router(); const auth = require('../../middleware/auth'); const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const config = require(&apo ...

Discover how to apply unique styles to specific HTML elements using their index values in jQuery

In the process of creating a party game similar to Tambola or Housie, I am faced with the challenge of automatically detecting and highlighting the numbers called out on each ticket in the game. For instance, if number 86 is announced, I aim to visually di ...

Add a CSS file to the browser-sync with proxy functionality

Currently, I have a script that I utilize to proxy a live website in order to make CSS modifications. Instead of replacing the online CSS file with a local one using a rewrite, I am exploring the possibility of injecting an entirely new file below the exis ...

Attribute specified does not belong to type 'DetailedHTMLProps<ButtonHTMLAttributes

I am working on creating a reusable 'button' component and I would like to include a href attribute so that when the button is clicked, it navigates to another page. An Issue Occurred: The following error was encountered: 'The type '{ ...

Error in three.js: Attempting to access the 'rotation' property of an undefined object

As I try to animate a cube in my scene, I keep encountering an error that reads: "TypeError: Cannot read property 'rotation' of undefined." function animate() { requestAnimationFrame( animate ); render(); } ...

The surprising height discrepancy in the li element while using the jQuery sortable plugin

After implementing this plugin, everything was running smoothly except for one minor bug that I encountered - the li element was displaying an unexpected height. To replicate this issue, follow these steps: 1. Open the sortable demo in Internet Explorer an ...

What could be the reason for the HTML canvas not displaying anything after a new element is added?

How come the HTML canvas stops showing anything after adding a new element? Here is my HTML canvas, which works perfectly fine until I add a new element to the DOM: <canvas class="id-canvas", width="1025", height="600"> ...

Here's a tutorial on creating a dropdown menu containing a list of years. When a specific year is selected, the table will display the individuals who registered during that year using PHP

<table class="table table-striped table-bordered bootstrap-datatable datatable"> <thead> <tr> <th></th> <th>Business Name</th> <th>Commencement Da ...

Encountering an issue in a Next.js application while building it, where an error is triggered because the property 'protocol' of 'window.location' cannot be destructured due to being undefined

While building my nextjs application, I encountered the following error. My setup uses typescript for building purposes, although I am only using JavaScript. Build error occurred: TypeError: Cannot destructure property 'protocol' of 'window ...

jQuery toggle buttons to show or hide on radio button selection

I have a pair of buttons and a pair of radio buttons Buttons 1) btnErp 2) btngoogle Radio Buttons 1) rdiogoogle 2) rdioErp When I select 'rdiogoogle', 'btngoogle' should be visible while 'btnErp' should be hidden. Conve ...

Safari displaying incorrect sizing for table nested in inline-flex container

When a <table> is placed within an inline-flex container that has a flex-direction of column, Safari displays the table with a different cross-size compared to Chrome and Firefox. This discrepancy makes me question if Safari's default display fo ...

The use of a Bootstrap row is leading to incorrect dimensions for FullPageJS

Within the body tag, I have included the following code snippet: <div id="container"> <div class="section profile"> <div class="row"> <div class="col-sm-6"> A </div> ...

The JQuery duplication process did not function as anticipated

This question builds on a previous one related to jQuery cloning and incrementing IDs. The issue is that when adding new elements, the IDs are not being generated in the correct sequence. For example, if the initial ID is expy-1, clicking "add more" should ...