What is the method for ending the mouseleave effect?

Clicking on the box will trigger a change in the text on mouseleave. Clicking on the button will also cause another change in the text. How can we revert the text back to its original position after removing the effects triggered by clicking the button and mouseleave on the box?

Check out the code here: https://jsfiddle.net/jcg8e0yL/

const test = document.getElementById('test');

['pointerdown', 'mousedown', 'touchstart'].forEach(function(item) {
        document.body.addEventListener(item, function(e) {
            if(e.target.classList.contains('ignore')) {
                
                test.innerHTML = 'How to undo the effect triggered by the button click?';
                
            } else {
      
        }
        });
    });

['pointerup', 'mouseleave', 'touchend'].forEach(function(item) {
        document.body.addEventListener(item, function(e) {
            if(e.target.classList.contains('container')) {
                
                test.innerHTML = 'Mouseleave';
                
            } else {
      
        }
        });
    });
    
<h2 id="test" class="test">
  Previous
</h2>

<div class="container">
  <button style="cursor: pointer;" class="ignore">Click me</button>
</div>

Answer №1

If you're looking to undo the changes triggered by clicking a button when the cursor moves away from the red box, one simple solution is to include the useCapture parameter while setting up the event listener:

['pointerup', 'mouseleave', 'touchend'].forEach(function(item) {
        document.body.addEventListener(item, function(e) {
            if(e.target.classList.contains('container')) {
                
                test.innerHTML = 'Mouseleave';
                
            } else {
      
        }
        }, true); //useCapture
    });

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

Javascript continues to execute even after the designated element has been eliminated or substituted

I'm currently working on a WordPress auction site using WooCommerce that needs a countdown timer to display when a specific product auction is ending. Below is the JavaScript code for the countdown timer: const getElem = elem => document.q ...

Problem encountered with JavaScript getter on iOS 5

While implementing JavaScript getters in my iPad-optimized website, everything was working perfectly until I updated to iOS 5. Suddenly, the site stopped functioning properly. After thorough investigation, I discovered the root cause of the issue. I have ...

Ways to induce scrolling in an overflow-y container

Is there a way to create an offset scroll within a div that contains a list generated by ngFor? I attempted the following on the div with overflow-y: @ViewChild('list') listRef: ElementRef; Then, upon clicking, I tried implementing this with s ...

What is the best way to enforce a required bindings property in an AngularJS component?

Suppose I have the following component defined: angular.module('myApp').component('myComponent', { templateUrl: 'myComponent.html', bindings: { myPropOne: '<', myPropTwo: '<' ...

Using Vanilla Javascript to implement a dark mode toggle button

I've been struggling with getting a button to properly switch a website to dark mode. I already have a night mode that works based on the user's location and time, so I know the issue lies within my JavaScript... Initially, I tried adding ' ...

Ways to Ensure a Property of an Object Remains Synced with Another

I'm dealing with the following Object structure: { a: "123" b: "$a" } In this setup, b should always be equal to the value of a. Any suggestions on how I can achieve this using JavaScript? ...

Confirming Deletion with a Jquery and Ajax Popup Box

I am seeking assistance with a specific task. Although my function is functioning well with ajax, I am in need of an "ok" or "cancel" delete confirmation box before submitting the ajax request to remove an item from the database. Here is the code I am cu ...

How to eliminate all special characters from a text in Angular

Suppose I receive a string containing special characters that needs to be transformed using filter/pipe, with the additional requirement of capitalizing the first letter of each word. For instance, transforming "@!₪ test stri&!ng₪" into "Test Stri ...

Making an asynchronous call from Index.html to PHP Script

I am currently working on implementing an AJAX call to my PHP Script. While I can successfully echo the results from my data.php file, I am now facing a challenge regarding how to initiate the call from index.html in order to retrieve the table results s ...

"Implementing a feature in React JS react-table to dynamically add a new column when a

I'm struggling to add a new column to react-table when a button is clicked. Even after re-rendering the table with a flag, I can't seem to add the new column. Can anyone suggest where I might be going wrong? Here's the link to the executable ...

What is the best way to update properties of an array object using a map in pure JavaScript for maximum efficiency?

Looking for an efficient solution to replace property keys in an array of objects using a map? Here's an example scenario: // Consider an array of objects: var records = [ {"Id": "0035w000036m j7oAAA", "Business Phone": ...

Attempt to create a truncated text that spans two lines, with the truncation occurring at the beginning of the text

How can I truncate text on two lines with truncation at the beginning of the text? I want it to appear like this: ... to long for this div I haven't been able to find a solution. Does anyone have any suggestions? Thanks in advance! ...

Looking for a way to efficiently retrieve results by matching multiple string keywords as you go through each line of a file (fs)?

Essentially, I have multiple search strings provided by the client that need to be matched with each line in a file. If a line matches all of the inputted strings, I should add that line to the results array. However, when I run the code below, it only ret ...

The React application is unable to communicate with my Express application in a production environment, despite functioning properly during development

Currently, I am attempting to make a basic get request to my express backend located at mywebsite.com/test. The expected response from the server should be {"test": "test"}. While this is working perfectly fine in development on localho ...

Creating a seamless integration between a multi-step form in React and React Router

I've been learning how to use React + ReactRouter in order to create a multi-step form. After getting the example working from this link: , I encountered an issue. The problem with the example is that it doesn't utilize ReactRouter, causing the ...

How can I extract certain key-value pairs from an array and add them to a different array?

collection [{ "title":"Completed", "details":[[dateA,timeA],[dateB,timeB]] },{ "title":"Unfinished", "details":[[dateC,timeC],[dateD,timeD]] }] If I need to transfer the title "Unfinished" and details [dateC,timeC] to a separate array, wh ...

What is the best way to update objects in different scopes within AngularJS?

Exploring AngularJS for the first time, I find myself struggling with understanding scopes. My current dilemma involves modifying an object or variable from multiple scopes. Here's the scenario: I'm looking to centralize the user notification Co ...

Transforming an array of HashMaps into a JSON object within a servlet and showcasing it on a JSP page

Looking to extract all table rows and store them in an array of hashmaps, then convert them into a JSON object to be sent to a JSP page using Ajax and jQuery. Struggling with displaying the content on the JSP page. I've written the code below but need ...

Utilizing JavaScript's Array.sort() method for sorting objects with varying properties

Currently, I am dealing with these specific Objects: let obj1 = { from: Date, to: Date } let obj2 = { date: Date } These Objects have been placed in an Array: let arr = [ obj1, obj2 ] My goal is to organize the objects within the array by using arr.sort( ...

Particle JS - Taking over the entire screen

I have incorporated Particle JS into the banner using the link provided. It should be confined within the banner, with a white background underneath displaying the header text "hello there." However, the Particle.JS effect is currently taking over the enti ...