The jQuery .on('click') event is only functioning properly the first time it is clicked

Recently, I encountered a peculiar issue with an on-click event that seems to work only once. The intended functionality is to toggle the .p-active class, but instead, it only adds it.

The problem arises from dynamically added content post-click using lighttooltip.js. Strangely, this dynamic content seems to interfere with the toggleClass function within my click event, causing it to malfunction. I've attempted different delegation methods such as using document, "body", and ".hotspots", all to no avail. While using "div" and ".hotspot" for delegation allows the class to be toggled on, it fails to toggle off.

An interesting observation was made when I removed all .LiteTooltip jQuery code (found below the click function) - this restoration allowed the on-click function to behave as expected.

I seek assistance in resolving this matter. I've simplified the code by reducing the number of "hotspots" to 3 for enhanced readability.

Note: The hotspot number itself isn't being toggled properly - its color should change from pink to white upon toggling.

/**** look inside tooltip CSS ****/
...
}
});
</script>

Answer №1

Update: Make sure to review the latest jQuery code provided below. This should address your needs.

/**** Update tooltip CSS ****/

div#look-inside .et_pb_row {
  width: 100% !important;
  max-width: 1500px !important;
}

#tooltip-clickoutside {
  /*z-index: 10;*/
}

.litetooltip-wrapper {
  font: inherit;
}

... (CSS styles continue exponentially) ...

body {
  margin-bottom: 100px;
  min-height: 800px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://paynefhstage.wpengine.com/wp-content/themes/Divi-child-01/look-inside/litetooltip.min.css" rel="stylesheet" />
<script src="https://paynefhstage.wpengine.com/wp-content/themes/Divi-child-01/look-inside/litetooltip.min.js"></script>


<div class="look-inside">
  <div class="img-wrap">
    <img alt="Look Inside" src="https://paynefhstage.wpengine.com/wp-content/uploads/Breckenridge.jpg">
  </div>
  <div class="hotspots">
    ... (Code for hotspots omitted for brevity) ...
</div>

<script>
  jQuery(document).ready(function($) {
    // JavaScript functionality for hotspot interactions
    var int = false;

   $(".redhotspot").click(function (e) {
     $(this).toggleClass("p-active");
     $(this).parents().siblings().children().removeClass("p-active");
   });
  });


  // Tooltip content setup and initialization
  jQuery('#ihotspot1').LiteTooltip({
    ... (Content for first hotspot tooltip) ...
  });
  jQuery('#ihotspot2').LiteTooltip({
    ... (Content for second hotspot tooltip) ...
  });
  jQuery('#ihotspot3').LiteTooltip({
    ... (Content for third hotspot tooltip) ...
  });
</script>

Answer №2

To prevent the click event from propagating up the DOM and triggering a click on the parent div, make sure to include e.stopPropagation(); in your code:

/**** look inside tooltip CSS ****/

div#look-inside .et_pb_row {
  width: 100% !important;
  max-width: 1500px !important;
}

#tooltip-clickoutside {
  /*z-index: 10;*/
}

.litetooltip-wrapper {
  font: inherit;
}

.litetooltip-wrapper .tooltip-arrow {
  border: none !important;
}

...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://paynefhstage.wpengine.com/wp-content/themes/Divi-child-01/look-inside/litetooltip.min.css" rel="stylesheet" />
<script src="https://paynefhstage.wpengine.com/wp-content/themes/Divi-child-01/look-inside/litetooltip.min.js"></script>


<div class="look-inside">
  <div class="img-wrap">
    <img alt="Look Inside" src="https://paynefhstage.wpengine.com/wp-content/uploads/Breckenridge.jpg">
  </div>
  <div class="hotspots">
    <div class="hotspot" id="ihotspot1">
      <div class="redhotspot" style="top: 80%; left: 44.5%; cursor: pointer;">1</div>
    </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

Which JQuery plugins can transform regular <select> dropdowns into more stylish options?

After an extensive search, I was only able to locate one solution at this link. I scoured the entire internet for alternatives. ...

Ensure that the floated div and its non-floated counterpart are both set to 100% height

I want to design 3 colored divs with specific dimensions and placements. The left div should have a fixed width and take up 100% of the height. The right div should fill up the remaining space (100% height and width minus the left div). The last div sho ...

Conceal an HTML video player on the webpage

Currently, I am utilizing the webcam easy js to capture a picture for our clients. My objective is to conceal the camera from the client's view, as they should not be aware that they are being recorded. I attempted to hide the element using jQuery wit ...

Hide Address with Jquery Dialog Box

How can I securely redirect a user to another page within the same website? In my specific situation, I have a form where users input information. For cancellations, they should have options available. When a user clicks the cancellation button, a dialog ...

Tips for Validating Radio Buttons in Angular Reactive Forms and Displaying Error Messages upon Submission

Objective: Ensure that the radio buttons are mandatory. Challenge: The element mat-error and its content are being displayed immediately, even before the form is submitted. It should only show up when the user tries to submit the form. I attempted to use ...

Adjust the background color dynamically as you scroll, given that I am using a linear-gradient

Is it possible to change the color background using JavaScript while scrolling, especially if I have a linear gradient like this: background: linear-gradient(115deg, #42c344 0%,#42c344 40%,#c4efc4 40%,#ffffff 40%,#ffffff 100%); background-attachment: fi ...

jQuery scroll to id is a function that allows you to smoothly

I have been creating onepage websites for quite some time now, and one issue that always bothers me is the repetitive navigation functions I need to write for each button and id. Here's what it typically looks like: $('#homeB').click(funct ...

Creating a Custom Bookmark Title for Apple Touch Icon on Mobile Homescreen

Can the title of a touch icon on the homescreen of an iOS device be customized? https://i.sstatic.net/9YL0t.jpg While it's currently acceptable, I am interested in creating a custom title instead of using a default one. ...

Is it necessary to specify the server-side script within the "routes" directory?

I'm currently developing a NodeJS Express application and from what I gather, the communication between the server and client is achieved by incorporating an AJAX script in a JavaScript file (on the client-side) and implementing a listener function (f ...

Is it possible to extract a string from a file and import the extracted string into another file using require() in NODE.js?

My file structure looks like this: collegesapp ├── node_modules │ ├── express │ ├── connect │ ├── jade │ └── passport ├── routes │ └── routes.js ├── views │ ├── index.jade │ ...

Looking to create an Ajax Control Toolkit AutoCompleteExtender with results that are "similar"?

The Ajax AutoCompleteExtender is all set up and functioning properly, linked to a webservice that fetches results from SQL. Now, I want to enhance the user experience by providing similar results in case they can't recall the exact name of what they& ...

What is the best way to solve the problem of Chrome auto-complete overlapping with labels in Vuetify?

When attempting to make a login form using outlined text fields in Vutify, there is an issue with Chrome autocomplete overlapping the labels. <v-text-field v-model="email" label="e-mail" name="email" outlined prep ...

Is there a bug with the CSS Safari selector for elements focused together?

Looking for a solution using the element+element-Selector to modify CSS for an element following a button. This code snippet functions in Chrome, Edge, and Firefox but not Safari on MacOS: .button:focus+.change{ color: red; } <p>When you focus ...

Tips for logging out a user from Auth0 in Next.js when token refresh fails with the help of Axios interceptor

I integrated the Auth0 SDK into my Next.js application to handle authentication, and I implemented an Axios interceptor for automatically refreshing the access token upon expiration. Below is the setup for the Axios interceptor: async function renewAccessT ...

Completing a Promise without invoking the 'then' method

As I work on developing a small API for the NPM module Poolio, one common dilemma arises regarding error-first callbacks and promises. The challenge lies in how to cater to both types of asynchronous functions while maintaining consistency in APIs and retu ...

Achieving the display of font-weight 100 for Google web fonts successful

I have been experiencing difficulties in achieving the ultra-thin display of the Lato font through Google web fonts. Despite appearing correctly on Google's website, the font weight does not change when implemented on my own page if it is below 400 (I ...

Ways to parse the data from a response received from an Axios POST request

After sending the same POST request using a cURL command, the response I receive is: {"allowed":[],"error":null} However, when I incorporate the POST request in my code and print it using either console.log("response: ", resp ...

Generate a new core element featuring the top 10 users

In my app, I have a function that sorts users in the mobile_user table based on their earned points and assigns them a rank. This function is triggered every time an http request is made. Now, what I want to achieve is creating a new root node called lead ...

Create a stunning visual on the canvas using the high-resolution Retina display

Working on a web App in phonegap and trying to use a 320 by 480 image for drawing, but it appears fuzzy. html <canvas id="canvas" height=480 width=320> Your browser does not support the HTML5 canvas tag.</canvas> javascript var canvas = doc ...

Tips on modifying responsive design with jQuery

I have implemented a responsive design approach with the following CSS code: @media all and (max-width:799px){ .bigFont{ font-size: 28px; } } @media all and (min-width:800px){ .bigFont{ font-size: 48px; } } If I want to modify the font size using ...