How can I use ontouchstart and ontouchend events in jQuery?

Currently, I am using the following code to change the class of elements on touch:

ontouchstart="$(this).addClass('select');" ontouchend="$(this).removeClass('select');"

I was wondering if there is a more efficient way to achieve this functionality:

$("#element").touchstart(function(){
    $(this).addClass('select');
},
function(){
   $(this).removeClass('select');
});

This would allow me to specify multiple elements that should have this behavior. I've attempted different approaches but haven't been successful in making it work.

Answer №1

In the end, I simply employed the following code:

$('.menu li').on('touchstart', function(){
    $(this).toggleClass('highlight');
}).on('touchend', function(){
    $(this).removeClass('highlight');
});

Answer №2

When using vanilla JavaScript :

element.addEventListener('touchstart', function(e) {
    e.currentTarget.className = "selected";
});

However, with the use of JQUERY :

$('#navbar ul li a').on('touchstart', function(){
    $(this).addClass('selected');
});

In fact, the on() method provides better performance compared to bind(). More details can be found in the official documentation

Answer №3

Consider incorporating jQuery Mobile into your project. It provides normalized events that may align with your requirements. Refer to jQuery Mobile's API reference for a comprehensive list of special events: checkout this link.

Pay attention to the following events:

vmousedown

Standardized event for managing touchstart or mousedown events

vmousemove

Standardized event for managing touchmove or mousemove events

vmouseup

Standardized event for managing touchend or mouseup events

Answer №4

Have you considered implementing the :hover pseudo-class instead? It appears that this modification to the CSS regulations of the nav_li component is only temporary.

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 a persistent left border / divider only on the even-numbered items in the list?

I have a layout of list items arranged in 2 columns, separated by a bottom margin after every 'row' of 2 items. While it's simple to add a left border to every even list item... I am curious if it's achievable to extend the border so ...

Transform in-line elements using specific HTML attributes using CSS styling

Can you achieve this with links (a[href="/"]{_/*CSS declarations.*/_})? For instance: #example > .meow span[style="color:red;"] { text-transform: lowercase; } #example > .woof span[title="I'm a dog."] { color: blue; text-decorat ...

Unable to retrieve the td value for the current row when the button inside the DIV is clicked

I want to extract the value 3 instead of the entire HTML. <div id="ExtraDt" style="overflow: auto; height:500px; width:100%; background-color: #FFFFFF; top: 0px;"> <table id="MasterList1" class="navigateable ...

Disable the off-canvas sidebar when a link is clicked in Jasny Bootstrap

Working on a website located at When viewing the site on mobile devices ( < 768px ), I am struggling with getting the off-canvas navigation to automatically close after clicking on one of its links. Any assistance would be greatly appreciated as I&apos ...

Guide on creating a stationary side navigation bar in HTML with Bootstrap 5 and CSS

I am developing a website with a fixed side navigation bar that stays in place when scrolling through the content section. I experimented with both the fix-position in Bootstrap 5 and CSS methods, but no matter how I set it up, the side bar remains transpa ...

What is the best approach to updating multiple rows instead of just the first row using the update button located on both sides using PHP and AJAX?

Starting fresh, so I might sound like a beginner with this question. How can I make use of the update button to update specific rows instead of just the top row? Every time I try to update, it only works for the top row. Here's the code snippet from ...

Enhancing the user experience by providing auto-complete suggestions while still maintaining the original form functionality

Encountering a curious bug that appears to be affecting only IE and Webkit browsers. I've set up a simple form as follows: <div id="output">Form output is shown here</div> <form id="myForm" action="#" method="post"> <input type= ...

Notify when the SVG object is clicked based on its identifier

I'm interested in adding more complexity to this project, but before I can proceed, I need to be able to detect when a specific object element containing an SVG image with an ID is clicked. Each object is nested within a div. <div class="grid 18"&g ...

Optimizing User Addition in Python Using Selenium

Currently, I am developing test cases to populate a new web app account with multiple users simultaneously. Here is the script I am using: driver.find_element_by_css_selector("input.ss-med.small").clear() driver.find_element_by_css_selector("input ...

Safari Mobile not rendering absolute positioning correctly

Our website www.anahatainternational.org has a functioning search section that is displaying correctly on Firefox, Chrome, and Safari browsers. However, we have encountered an issue with the mobile version of Safari where the search section appears in the ...

Adjusting Chart.js line graph alignment to the left

I'm curious about why my chart is not aligned to the left and how I can fix this issue. Here is the code snippet I am working with: var data = { labels: ["ID"] , datasets: [ { label: "Sensor 1" , data: [{ ...

Ways to showcase multiple images that have been pre-tagged

I have implemented image tagging where I store the x and y positions of tags in a database. When displaying single images with tagging, the correct position is shown as follows: foreach($images_data as $img) { <img src="<?=$img['img'] ...

The elements within the HTML, CSS, and Bootstrap are all clashing and

Website Output https://i.sstatic.net/5JVyg.jpg I've been working on a small website with a bootstrap carousel, and in this specific section of the program, the Bootstrap and HTML/CSS elements overlap by default. I'm not sure how to separate the ...

Tips for keeping the scroll position of a bootstrap table when reloading the page

Displayed below is a bootstrap table with specific features. <div class="row"> <table id="question-selection" class="table table-sm table-hover table-wrapper"> <tbody> {% for placer in chapter_questions %} ...

Updating the style of different input elements using Angular's dynamic CSS properties

I am seeking guidance on the proper method for achieving a specific functionality. I have a set of buttons, and I would like the opacity of a button to increase when it is pressed. Here is the approach I have taken so far, but I have doubts about its eff ...

Implementing pagination in Webgrid using AJAX post method

I've developed this JavaScript code: function PartialViewLoad() { $.ajaxSetup({ cache: false }); $.ajax({ url: "/ControllerAlpha/MethodBeta", type: "GET", dataType: "html", data: { s ...

Having no capability to manipulate CSS using jquery

In one of my divs, the CSS is set to have display: none .cookie_policy { color: white; text-align: justify; width: 50%; margin: 2% 25%; line-height: 20px; display: block; font-family: Arial,Helvetica,sans-serif; font-style: italic; display:none; } <div ...

Automatically integrating Nivo Lightbox into your website

I incorporated Nivo Lightbox into my website, seamlessly integrating all images with the plugin using the following code: <div class="portfolio-item"> <div class="portfolio-thumb "> <a class="lightbox" data-lightbox-type="ajax" title="Strat ...

The nestedSortable plugin fails to detect elements once the HTML has finished rendering

If I add multiple elements to a nested sortable list, they automatically go to the root and can be dragged around normally. When I drag a parent element, the child moves along with it as shown in the image below. After saving the list using .nestedSortabl ...

Combining Python strings

I'm trying to understand the distinction between two statements. I need to display a variable in a <p> HTML tag. Both statements achieve the same outcome, but one is causing an error. The first statement that functions correctly: out += "</ ...