Getting a jQuery alert on iOS devices: A step-by-step guide

The notification using

<span class="close">&times;</span>
functions perfectly on all desktop browsers, but unfortunately fails to trigger on mobile devices ... When attempting to view the page on an iPhone, the
<span class="close">&times;</span>
does not generate any alert.

Any suggestions on how to resolve this issue?

var closebtns = document.getElementsByClassName("close");
var i;

for (i = 0; i < closebtns.length; i++) {
  closebtns[i].addEventListener("click", function() {
alert("X was clicked");
  });
}
      body {font-family: sans-serif;}

input { font-size: 1em; } /* prevent zoom in mobile */

ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

ul li {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block;
  position: relative;
  min-height: 1em;
  z-index: 999;
  max-width: 360px;
}

ul li:hover {
  background-color: #eee;
}


/* sortable plugin styles when dragged */
.dragged {
position: absolute;
opacity: 0.5;
z-index: 2000;
}

li.placeholder {
position: relative;
background: purple; 
}

.close {
  cursor: pointer;
  position: absolute;
  top: 50%;
  right: 0%;
  padding: 12px 16px;
  transform: translate(0%, -50%);
  z-index: 99999;
}

.close:hover {background: #bbb;}
<div id="sort-it">
    <ul>
      <li>This is item #1<span class="close">&times;</span></li>       
      <li>This is item #2<span class="close">&times;</span></li>        
      <li>This is item #3<span class="close">&times;</span></li>        
      <li>This is item #4<span class="close">&times;</span></li>      
      <li>This is item #5<span class="close">&times;</span></li>
    </ul>
</div>

Answer №1

Learn how to create a mobile-friendly button click with this solution. Simply add touchstart to the click event.

$('.close').on('click touchstart', function(e)

$(document).ready(function() {
$('.close').on('click touchstart', function(e) {
e.stopPropagation(); e.preventDefault();
alert($(this).closest('li').attr('id'));
});
});
  body {font-family: sans-serif;}

input { font-size: 1em; } /* prevent zoom in mobile */

ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

ul li {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block;
  position: relative;
  min-height: 1em;
  max-width: 360px;
}

ul li:hover {
  background-color: #eee;
}


/* sortable plugin styles when dragged */
.dragged {
position: absolute;
opacity: 0.5;
z-index: 2000;
}

li.placeholder {
position: relative;
background: purple; 
}

.close {
  cursor: pointer;
  position: absolute;
  top: 50%;
  right: 0%;
  padding: 12px 16px;
  transform: translate(0%, -50%);
  background: #fff;
}

.close:hover {background: #bbb;}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="sort-it">
    <ul>
      <li id="ID_1">This is item #1<button class="close">&times;</button></li>       
      <li id="ID_2">This is item #2<button class="close">&times;</button></li>        
      <li id="ID_3">This is item #3<button class="close">&times;</button></li>        
      <li id="ID_4">This is item #4<button class="close">&times;</button></li>      
      <li id="ID_5">This is item #5<button class="close">&times</button></li>
    </ul>
</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

jquery is in motion while svg paths are at a standstill, waiting to

i have been attempting to incorporate a css-animated svg into my project. initially, the animation would start automatically, which was undesirable. after some research, i discovered that by declaring it as paused and then triggering it using jQuery with $ ...

How can I delete an individual HTML element that does not have a class or ID?

I am currently working with a theme that cannot be altered due to automatic update requirements. The theme includes the following HTML code snippet: <div class="dropdown-menu"> <a href="#">Profile</a> | <a href="#">Logout</a> ...

The output of jQuery('body').text() varies depending on the browser being used

Here is the setup of my HTML code: <html> <head> <title>Test</title> <script type="text/javascript" src="jQuery.js"></script> <script type="text/javascript"> function initialize() { var ...

Disabling the .hover function temporarily

I am currently exploring how to temporarily disable the .hover function in jQuery based on a specific event that occurs on the page. You can view my current jsfiddle here: http://jsfiddle.net/4vhajam3/3/ (please note that I have omitted some code for simp ...

Material-UI is having trouble resolving the module '@material-ui/core/styles/createMuiTheme'

Although I have searched for similar issues on StackOverflow, none of the solutions seem to work for me. The errors I am encountering are unique and so are the fixes required, hence I decided to create a new post. The company conducting my test provided m ...

Obtain the button's name value using Jquery

I currently have a button set up in the following manner: <button class="language-select selected" type="button" name="en-gb"></button> Does anyone know how I can retrieve the value "en-gb" from the name attribute using jQuery? My ultimate go ...

What is the best way to retrieve the directory path from a FileReader in Java

Hey there, check out these codes I have for reading the file that the user uploads: function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#myImg' ...

My goal is to use both jQuery and PHP to automatically populate the dropdown list

Here is my PHP code for executing a query: $host = "localhost"; $user = "root"; $pass = "abc123"; $databaseName = "class"; $con = mysql_connect($host,$user,$pass); $dbs = mysql_select_db($databaseName, $con); $result = mysql_qu ...

Tips for setting the style of child components in a ReactJS project with CSS modules, classNames, and JSX syntax

Currently in the process of setting up a project using React JS and I am interested in incorporating auth0 for authentication purposes, along with potentially integrating some serverless functions. auth0 conveniently offers a pre-made "seed" project at ht ...

Styling Your Website: Embrace CSS or Stick with Tables?

My goal is to create a layout with the following features, as shown in the screenshot: The main features include: The screen is divided into 3 regions (columns); The left and right columns have fixed widths; The middle column expands based on the browse ...

Changes to attributes of an HTML tag cannot be made by jQuery code during an AJAX call

Could someone help me figure out why this jQuery code is not functioning properly? I have already tested it outside the $(document).ready block with no success. Note: The unusual "{{ }}" and "{% %}" syntax is typically used in Django. jQuery $(document) ...

Struggling to locate the app icon when building with React Native for iOS

I set up react-native from the official website following these steps. (I installed node.js and openJDK separately) System Information Operating System: Mac OSX Mojave 10.14.6 XCode Version: 11.0 (11A420a) node(nvm) Version: 10.15 react Version: 16.9.0 ...

Implementing jQuery Validate along with Gritter Notification

I'm struggling with the jquery notification formReset() function along with "e.preventDefault();" and gritter notifications. In my code, I use the eventAdd() function in the submitHandler of a validator. Inside the eventAdd function, I trigger a gritt ...

Can you explain the significance of the jz element in HTML?

When visiting this particular page, it was noticed that the well-known ad blocking software successfully removed ads positioned near the top and right of the page. However, unexpectedly, this software added a brand new section of clickbait ads located just ...

What is causing the bullets for the unordered list to appear before the items are inserted into the list?

Can you explain why the bullets are showing up before the list items are added? <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>To Do List</title> </head> <body> ...

Update the index of each list object and then add them to the Angular controller

Currently, I am working on integrating Spring with AngularJS. In my setup, I have a Spring controller returning a List object to the Angular controller. When I print the List in the Spring controller console, it appears as follows: [org.com.pro.dto.MyDTO ...

Vue.js mobile app may show a loaded DOM that remains invisible until the screen is tapped

I am facing a peculiar issue that has me stumped. On my mobile device, my page initially loads like this homepage However, once I tap the screen, all the components suddenly appear. Is there a way to simulate a click on my mobile? I'm struggling to u ...

The value of ajax data at index 0 is not defined

When using AJAX to retrieve the number of rows from a SQL query in PHP, the JSON return shown in the Firefox Network tab is [{"number":2}], with the number 2 being without quotes. However, when trying to access the value (2) using data[0]["number"] or dat ...

Creating a dynamic HIIT program with an autoplay feature for videos and images, using a jQuery/JavaScript slider "playlist" with the backend support of

I am currently exploring the idea of developing a countdown timer that incorporates videos. In order for this timer to function effectively, it must be able to retrieve data such as countdown time, break time, number of sets, and video (or images if no vid ...

How can I use jQuery to access the parent node in an XML document?

I have been trying to extract the top-level 'label' attribute from the XML code below using jQuery, but I haven't had any luck so far. I have already converted it into a DOM object, but the results are not what I expected. Does anyone have a ...