What is the method for creating a variable that is dynamic in nature?

I have encountered a small issue that I am struggling to resolve.

There is one div that should execute a jQuery code on onTouchEnd, and it does so perfectly. The function sends the ID of the specific div.

<div id="tapme1" class="subject hold" onTouchEnd="goes(this.id)"></div>

The function being executed is as follows:

function goes(e) {
   var now = new Date().getTime();
   var lastTouch = $(this).data('lastTouch') || now + 1 /** the first time this will make delta a negative number */;
   var element = e;
   var delta = now - lastTouch;
   if(delta < 300 && delta > 0) {
      if($('#tapme1').hasClass("hold")) {
          $('#tapme1').removeClass("hold");
      } else {
          $('#tapme1').addClass("hold");
      }
   } else {
       // A click/touch action could be invoked here but we need to wait half a second before doing so.
   }
   $(this).data('lastTouch', now);
}

The above code functions correctly. However, the problem arises when trying to change the clicked div. Replacing $('#tapme1') with $(element) did not produce any results despite trying various options.

If anyone can assist me with this, it would be greatly appreciated!

Answer №1

Ensure to include the # before the id selector.

Modify $(lement) to $("#" + lement)

Answer №2

Make sure to utilize: $('#' + element)

Answer №3

The hashtag '#' is crucial

Update to:

$('#' + element)

Instead of:

$(element)

Answer №4

Is it possible to just send this rather than this.id as the parameter?

<div id="clickhere1" class="subject hold" onClick="execute(this)"></div>

After that, you could utilize

$(object)

Answer №5

When passing the id, such as tapme1, remember that jQuery requires a CSS selector format like #tapme1. To make this adjustment, modify your code to search for $("#"+lement), or alternatively use var lement = "#" + e;.

Answer №6

When you have the id already, there is a need for adjustment:

$('#tapme1')

Should become:

$('#' + element)

The variable element refers to the function argument that represents the id being passed.

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

Utilize JavaScript to apply the CSS -moz-transition

Creating a web application and using CSS3 to transform a div, but encountering a challenge with Firefox. Able to make Chrome, Opera, and IE work properly, except for Firefox. Here's how I'm setting up the working browsers: obj.style.WebkitTrans ...

How can you temporarily delay the original ajax request until a certain condition is satisfied before proceeding?

I'm looking to set up a recaptcha process that intercepts all ajax requests before they are executed - here's how I envision the process unfolding: A user performs an action that triggers an ajax request. If the user has already completed the r ...

Executing memory function with JQuery replaceWith() and event handlers

Imagine you have the following HTML code: <button id="click-me" type="button">Click Me!</button> Now, picture this jQuery snippet being executed: var button = $('#click-me'); button.on('click', function() { console.l ...

Fill a form with jQuery and ajax data after it has been submitted

I'm working on a simple HTML form and I want to use Ajax to perform a lookup using a PHP file after entering data into the first field. The goal is to fetch information from an external source for the two remaining fields. <form method="post" acti ...

Discover local points of interest with the Google Places API integration on your WordPress site through PHP programming

$(document).ready(function() { var image_src = "images/"; var map; var infowindow; var bounds = new google.maps.LatLngBounds(); var PlaceArray = ["restaurant", "cafe", "bar", "grocery_or_supermarket", "parks", "school", "shopping_mall", "movie_t ...

Bespoke Video Player using HTML5 with "Nerdy Stats" Feature

I have been given the task of creating a streaming video testing framework for internal metrics and measurement purposes. Essentially, I am looking to develop an HTML5 player that can handle streams and provide performance data related to: - Average Bitr ...

Challenges with basic contact form

Apologies in advance for any beginner mistakes, as I am venturing into creating an HTML/PHP contact form for the first time. Despite researching similar problems faced by others, I have not come across a solution. The main issue is that the email is not b ...

Is there a way for us to determine the time at which the user last took a screenshot or photo?

I am currently developing a website using Django and I have a unique requirement. I need to access the last image that a user has taken on their phone, without the image being shared by anyone else. The photo must be captured by the user's device itse ...

Uncover the hidden href link that has been obscured using the code javascript:void(0)

Currently, I am in the process of developing a straightforward PHP script to extract the actual link from a website that conceals the real link/key using the javascript:void(0) approach. Upon inspecting the source code, I noticed that the href link is empt ...

Data binding functions properly only when utilizing the syntax within the select ng-options

In the book AngularJS in Action, I came across this Angular controller: angular.module('Angello.Storyboard') .controller('StoryboardCtrl', function() { var storyboard = this; storyboard.currentStory = null; ...

Choosing the middle child of an element without using the :nth pseudo-classes

As I gear up for a regional championship in web development for high school students, one of the preparation tasks involves solving challenges on the championship website. The HTML code provided is as follows: <h2>Task 6</h2> <article id=& ...

What is a method to display a list in javascript without using a rendering engine?

Seeking to avoid any render engine or framework except for express. Our goal is to display any list retrieved from a database query without relying on a render engine. What code can be used as a substitute for a render engine when working with data lists ...

The Jquery watermark extension in IE9 is capable of transmitting the watermark as the primary value of the form

I have integrated a plugin into my project. The plugin can be downloaded from this link. However, I am facing an issue where the watermark value is being submitted as the textbox's value in Internet Explorer 9. How can I resolve this problem? Intere ...

Alert box in Vue.js

Embarking on my journey with VueJS. I previously implemented an alert box using jQuery. Now, I am attempting to recreate that in VueJS. Here is my current progress: 1- Developed a component named NovinAlert.vue which includes: <template> & ...

The fadeOut() function is not functioning properly as the div keeps reappearing even after attempting to

My navigation bar has a unique feature - the subnav expands to fullscreen on hover, while all other subnavs close. However, I encountered an issue with the close button functionality. When clicked, the subnav fades out but then immediately fades back in. ...

Tips for adjusting the size of a background image using zoom

Currently, I am in the process of creating my first website. At this stage, it consists of a background image, an image, and a non-functional password input section. Everything seems to be going well so far. The background image fits perfectly on the scree ...

Encountering a POST 405 error message stating "Method not allowed" while attempting to send an AJAX request in Laravel

I've been attempting to create a simple AJAX request to populate a menu in Laravel, but I've been running into some difficulties getting it to function correctly. Despite spending several hours searching for a solution, I'm still unsure of ...

JavaScript struggles to obtain the timezone information when Daylight Saving Time is

Can someone help me with setting a user's timezone offset for PHP through ajax? When a page is loaded with session data, if there is no pre-existing data, the following script is inserted into the page: <script type="text/javascript"> $(doc ...

Error: Trouble encountered when sending a request to a webservice through ajax due to an unexpected token <

$(document).ready(function () { $("#submit").click(function (e) { e.preventDefault(); var userName = $("#username").val(); var password = $("#password").val(); authenticateUser(userName, password); }); }); functio ...

Sencha Touch sends a beacon to jQuery

I am looking to incorporate a jQuery plugin into my Sencha Touch application. The challenge is making sure that the jQuery plugin is only initialized after Sencha Touch has finished populating the DOM structure. This is necessary because the plugin needs ...