Having trouble getting the jQuery click event to work on iPhone browsers? Despite already trying the cursor:pointer; method

I am facing an issue with setting a click event on anchor tags in my WordPress site. Surprisingly, the event works perfectly fine on all of my devices including PC, Android phone, and tablet except for my iPhone.

Despite trying to set the 'Cursor' property to 'Pointer' for the anchor tags, I have not been successful.

I have exhausted all my options by searching on SO (Stack Overflow) for solutions, but unfortunately, nothing seems to resolve the issue :/

If anyone has any suggestions or solutions, I would greatly appreciate it.

Answer №1

It seems like you can replace href with onclick in order to use a JavaScript function like this:

function ANYTHING() {
   $("a[href^=#]").on('click touchstart', function(e) {
   e.preventDefault(); var dest = $(this).attr('href');  
   console.log(dest);
   $('html,body').animate({ scrollTop: $(dest).offset().top }, 'slow');
  });
}
.scroller {
            cursor: pointer;
        }
<a class="scroller" onclick="void(0)" onclick='ANYTHING('>
   <div id="mobile-hero-book" class="book-now-btn">Book a Car</div>
</a>

If this solution works for you, feel free to keep the href attribute in your HTML as needed!

Answer №2

Here is a handy jQuery plugin I created specifically for the issue you're facing:

$(document).ready(function() {
  $("#special-button").press(function() {
    console.log("The special button was pressed!");
  });
});

$.fn.press = function(callback) {
  var press = false;
  $(this).on("click", function(e) {
    if (!press) {
      console.log("Click detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    } else {
      press = true;
    }
    press = false;
  });
  $(this).on("touchstart", function(e) {
    $(this).blur();
    if (typeof e.touches != typeof undefined) {
      e.preventDefault();
      press = true;
      console.log("Touch detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="special-button">Try Me</button>

EDIT 1:

Removed

EDIT 2:

I misinterpreted the question. The code above still works well with iOS devices, but here's how to handle anchor tags. Remove the #id href:

$(document).ready(function() {
  $(".anchor-tag").press(function() {
    var targetElem = $("#" + $(this).data('target-elem'));
    $('html,body').animate({
      scrollTop: targetElem.offset().top
    }, 'slow');
  });
});

$.fn.press = function(callback) {
  var press = false;
  $(this).on("click", function(e) {
    if (!press) {
      console.log("Click detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    } else {
      press = true;
    }
    press = false;
  });
  $(this).on("touchstart", function(e) {
    $(this).blur();
    if (typeof e.touches != typeof undefined) {
      e.preventDefault();
      press = true;
      console.log("Touch detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    }
  });
}
#target-element {
  margin-top: 1200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="javascript:void(0);" class="anchor-tag" id="link-1" data-target-elem="target-element">Try Me</a>

<div id="target-element">
  <p>This is the target container</p>
</div>

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

What is the best way to include a string in formData when making an ajax request

Is there a way to include the id when sending formdata? I need help modifying my code for better understanding. $(document).ready(function() { $("#profile_photo_form").on('submit', (function(e) { e.preventDefault(); var id = $("#update ...

Unusual animation glitches encountered while coding with Swift 4 and UIView

I'm at a loss trying to identify the issue with my code. Allow me to elaborate on what I was aiming for: In my UIViewController, I have two views called "redSquare" and "greenSquare". When the UIViewController is displayed, I intend to animate the r ...

Creating a CSS3 web design inspired by the Facebook-image style - here's how!

Looking to create a design using CSS that mimics the facebook-image-container, similar to this example: . Seeking advice on how to achieve this effect. The concept is to have a centered box on the page, with one half displaying an image and the other conta ...

Using jQuery to inject quotation marks

After writing a simple jQuery code: $("p").html("A basic <strong>sample</strong>"); Upon inspecting in Chrome, the output displayed is: <p> "A basic " <strong>sample</strong> </p> I am curious about the double qu ...

Obtain the contenteditable span's value

I'm facing an issue with a content editable span - I want to extract its value when the post button is clicked, but it's not working as expected. Please note that I am unable to convert the span to a textbox or div, I specifically need the value ...

Shifting a div from side to side

I don't have much experience with coding, so I was hoping for some assistance. My goal was to create a div that moves back and forth using toggleClass; $('div').on('click', function() { $(this).toggleClass('active') ...

Tips for dynamically populating select options with objects

After executing a query using ajax to retrieve data from a database, I found that the response is in the format shown below. array(206) { [0]=> object(stdClass)#6707 (8) { ["id"]=> string(1) "8" ["year"]=> string(4) "1947" ["make"]=> string(7) ...

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 ...

Navigate within a single component on the Stack by scrolling

I'm a huge fan of CSS! Although it may seem like a simple task, I am completely stumped on how to tackle it. Mission: My goal is to enable scrolling only within the List section without affecting the entire page. Here's the structure of my ...

Changing the background color with a switch function in ReactJS

After clicking a link, a view is rendered based on the "id" from a JSON. To enhance the user experience, I want to add a background color when a particular view renders and also toggle the style. This code snippet illustrates how the crawl is displaye ...

What causes the alignment of text to be overridden by the presence of a floating element?

https://jsfiddle.net/u0Ltgh3e/68/ .text { width: 50%; text-align: justify; } I am currently developing a custom formatting tool for a specific purpose and I am attempting to create a two-column layout similar to the one demonstrated in this Jsfiddle l ...

Insert information into the #myModal window of Twitter Bootstrap

I'm struggling with this seemingly simple issue and can't seem to find a solution. Is there a way to insert a PHP variable into the link for this Twitter Bootstrap popup? <a href="#myModal" title="Learn more" value="12345" data-toggle="modal ...

Internet Explorer and Edge browsers are concealing box shadow behind the parent div

When using an input slider range, the box-shadow is being applied to the active thumb in all browsers except for IE & EDGE. In these browsers, the shadow is cutting or hiding behind the parent div #c. I have already tried setting overflow:visible on the p ...

Navigating Swift's optionals behavior within an if-statement

I'm facing an issue with an if statement that is supposed to compare the type of a room (String), which may be nil, to a list of room types for a UIPicker. If the room name doesn't exist, we want to automatically set the picker to "Other". The p ...

Leveraging NSUserDefaults

I'm in the early stages of learning cocos2d and I've encountered some challenges when it comes to using NSUserDefaults for storing and retrieving user-specific data. The game I am developing features a fisherman who can upgrade his equipment by ...

Mastering Ajax techniques for uploading multiple files

I'm in need of assistance - I have devised a straightforward form that empowers users to upload comment text and files onto the server. The 'upload.php' file handles the file upload process flawlessly but only for one file at a time. My goa ...

Using jquery to dynamically change audio source on click

Is there a way to dynamically change the audio src using jquery? <audio id="audio" controls="" > <source src="" type="audio/mpeg" /> </audio> <ul id="playlist"> <?php if($lists) { foreach ($lists as $list) { ?> ...

The Jquery.ajax function is passing null values to the controller, but it functions properly when using pre-defined

I have been working on a simple app in VS Code where I need to save values into a database. This is the code snippet I have written so far. var name = $("#TextName").val(); var price = $("#TextPrice").val(); var descrip ...

Struggling to make a basic ajax request function correctly

I've been experimenting with this code snippet in my browser's console. $.ajax({ type: 'GET', url: 'http://stackoverflow.com/', dataType: 'html', success: function() { console.log("Yes, t ...

Having issues with C# ASP.Net autocomplete not functioning properly when using Javascript/Json post

I have been working on a c# asp.net usercontrol that requires a functional autocomplete feature. However, I am encountering an ongoing issue where the script appears to be running – with the progress bar spinning – but it consistently returns an ' ...