Clickable label for checkbox in Android browser is unresponsive

I've been experimenting with creating a responsive CSS menu using the checkbox hack

Here's the HTML:

<input type="checkbox" id="button" />
<label for="button" onclick>click / touch</label>

<div>
 Change my color!
</div>

And here's the CSS:

/* Advanced Checkbox Hack */

body { -webkit-animation: bugfix infinite 1s; }
@-webkit-keyframes bugfix { from {padding:0;} to {padding:0;} }

input[type=checkbox] {
 position:absolute;
 top:-9999px;
 left:-9999px;
}

label { 
 cursor: pointer;
 user-select: none;
}

div {
 background-color: #00F;
}

/* checked */
input[type=checkbox]:checked ~ div {
 background-color: #F00;
}

My setup worked well, passed valid html5 tests and was successfully tested on various desktop browsers. However, when testing on an Android 4.1.1 tablet, I encountered some issues where the menu wouldn't open. The problem wasn't with the pseudo-class :checked, but instead due to:

  1. The associated checkbox label not toggling the checkbox state upon tapping, and
  2. A JavaScript workaround failing to simulate the click due to the browser detecting it as two clicks.

For example, the following JS didn't work on Android:

$('label').click(function(e) {
 e.preventDefault();
 $("input#button").trigger('click'); 
});

An alternate approach setting the checkbox always to 'checked' also had limitations. Despite attempts with a timestamp-based workaround, I couldn't resolve the issue.

What perplexes me is that while the checkbox hack example functions on the tablet, my menu (unpublished) or even a simple form with checkboxes fail to change the checkbox upon label click.

If you have any insights on where I may have erred or possible solutions for enabling checkbox set/unset functionality on Android 4.1.1 without nesting within labels, please share. Even though my aim was a CSS-only solution, I'm open to a JS resolution. Thanks!

Answer №1

After some experimentation, I implemented this solution:

$('.touch .menu label').on('tap', function(e) { 
  e.stopPropagation(); 
  e.preventDefault();
  var hID = $(this).prop('for');
  $("input#"+hID).trigger('click'); /* not tap */
});

The tap event is derived from tocca.js, but it successfully triggers a click as well.

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

When sorting in AngularJS using the orderBy filter, remember that string values should come before numeric values: for example, sort as follows (n/a, 0,

While running an AngularJS filter sorting on a table, I encountered an issue where if the value is 'n/a' (which is a numeric string), the sorting is incorrect. I expected the order to be n/a, 0, 1, 2, 5, 100 since strings should be considered l ...

Centering Multiple Lines of Items in an Unordered List with Horizontal Alignment

Looking for a solution to center an unordered list horizontally within the containing UL, especially when the list wraps onto a second line. Each LI has a set width and height. I've tried various methods that work for a single line but have had no luc ...

What is the best way to divide the dev-server.js file in Vue into multiple separate files?

Currently, I am utilizing vue.js to develop an application and have created a mock login api on localhost in dev-server.js. Now, I am looking to refactor the code related to the login api into a separate file. Do you have any suggestions on how I can ach ...

Tips for creating a form that adapts to different devices, ensuring it looks great no matter the screen size

<div class="modal" id="myModal" style="width:50%; top:15%;" role="dialog" > <div class=""> <!-- Modal content--> <div class="modal-content"> ...

Enhancing dynamic text boxes with jQuery by incorporating additional information

I've recently developed an interactive app that dynamically creates tables based on user input. The app includes a feature where you can specify the number of tables to generate and track the total count of tables added. Currently, I'm exploring ...

Issue with Google Maps: undesired change in map size

Welcome to my app! Check out the link to my app here: http://jsbin.com/axeWOwAN/1/edit If you prefer a full-screen view, click here: http://jsbin.com/axeWOwAN/1 I'm encountering an issue with the map on the second page of my app. The map works perf ...

Error: The connection with mssql in nodejs has been unexpectedly terminated due to a read ECONNRESET issue

I am currently using the node-mssql module which can be found at this link Whenever I try to insert a large amount of data into an MSSQL table, the connection is lost If I attempt to insert more than 4 rows at a time, an error is thrown import * as SQL ...

Exploring the differences between arrays and objects provided by users

I am working on a functionality that involves comparing user input with predefined usernames and passwords. Here is what I have so far: var sys = { users: [ {user: 'user1', pass: 'qwerty'}, {user: 'Ragnar&apos ...

Having trouble with object initialization in a jQuery GET request?

Looking to create an HTML button using jQuery that, upon clicking the chart button, will retrieve values from specified text inputs. These values will then be used to construct a JSON object which will subsequently be included in a GET request. $(".chart" ...

Shifting the navigation bar to the right to accommodate a logo on the left side of the bar

I'm currently working on adjusting the position of the navigation bar to the right in order to accommodate a logo on the left side of the navigation text. Despite trying to use float-right, it doesn't seem to be working as expected. Below is the ...

Tips for presenting a returned list of Objects from the AdminController to the Admin interface in asp.net MVC5

Two methods have been configured in the AdministrationController to handle locked accounts. One method will collect all locked accounts, and the other method will unlock an account when the user id is passed to it. public bool unlockUserById(String userId ...

Leveraging em units in jQuery Script

I'm currently in the process of building a website and I'm facing an issue with converting measurements to em's. The script that I have reused from a previous project only seems to work with pixels, despite my efforts to make it compatible w ...

Vanilla JavaScript // Conceal a div when the class of another div is modified

Note: I am unable to utilize jQuery, only vanilla JavaScript I am not very proficient in pure JS. Additionally, this time around, I cannot rely on any external resources such as jQuery. What I am looking for: If the div1 class is active, I want to hide ...

React Native encounters issues with removing the reference to the callback attribute upon unmounting

Utilizing a component that I place in an array (of various sizes) and controlling individual components through refs, while adding refs to an object to keep track of each separately. constructor(props){ super(props); this.stamps = []; this.get ...

Safari browser experiencing issues with JQuery Slider functionality

I am facing an issue with a slider that works perfectly in all browsers except Safari. I have been struggling to figure out the root cause of this problem. You can view the fiddler code here: http://jsfiddle.net/sjramsay/UyvvL/ It appears that when the co ...

Is all of the app fetched by Next.js when the initial request is sent?

After doing some research online, I learned that Next.js utilizes client-side routing. This means that when you make the first request, all pages are fetched from the server. Subsequent requests will render those pages in the browser without needing to com ...

What is the best method for disseminating data to multiple stores with just a single action in the React flux architecture?

Is there a way to efficiently update multiple stores with data in one action? Imagine receiving post data from a server as a user action. Below is a simple pseudo code for this action: class UserActions { getPosts() { asyncFetch(apiEndPoint, ...

Empty vertical space on the right side in iPad view

I am currently working on the development of this website and facing an issue. https://i.sstatic.net/Y7B7Q.png When switching to iPad view in Chrome's developer mode, I noticed a blank column on the extreme right side that spans the entire page. I a ...

Struggling with the elimination of bullet points in CSS navigation bars

I'm having trouble removing the bullet points from my navigation bar. I've tried using list-style-type: none and even adding !important, but it doesn't seem to work. Strangely enough, everything looks fine in Chrome, but I get an error when ...

Transferring information using express

Currently, my Express server is up and running and it's set to send an HTML file from the public folder of my project. The issue arises when I try to initiate a request from a client script linked in this HTML file to send data back to the server. Des ...