Using preventDefault and stopPropagation will prevent altering the CSS properties of a div

I recently made the switch from using onclick on a div to touchstart in order to increase the speed of clicking on a touch screen. However, I have encountered an issue where the CSS property that changes the background color is no longer functioning. Below is the code snippet:

<div class="numpad" id="numpad" align="center">
    <div style="display:flex;">
        <kbd touchpad="call(1)">1
            <div class="background">&nbsp;</div>
        </kbd>
        <kbd touchpad="call(2)">2
            <div class="background">ABC</div>
        </kbd>
        <kbd touchpad="call(3)">3
            <div class="background">DEF</div>
        </kbd>
    </div>

Here's the Angular directive:

angular.module('myApp').directive('numpad', function($log) {
    return {
        restrict: 'E',
        templateUrl: 'html/directives/numpadOpenr.html',
        require: 'ngModel',
        link: function(scope, elem, attrs, ngModel) {

            scope.number = ngModel;

            scope.call = function(number) {
                var value = scope.number.$viewValue || '';
                scope.number.$setViewValue(value + number);
            };

            scope.remove = function() {
                var value = scope.number.$viewValue || '';
                if (value.length > 0) {
                    scope.number.$setViewValue(value.substring(0, value.length - 1));
                }
            };

        }
    };
});

angular.module('myApp').directive("touchpad", [function () {
    return function (scope, elem, attrs) {
        elem.bind("touchstart click", function (e) {
            e.preventDefault();
            e.stopPropagation();
            scope.$apply(attrs["touchpad"]);
        });
    }
}]);

And here is the CSS:

 kbd, .key {
        display: inline-block;
        min-width: 2.2em;
        font: normal .85em "Lucida Grande", Lucida, Arial, sans-serif;
        text-align: center;
        text-decoration: none;
        -webkit-border-radius: 3px;
        -moz-border-radius: 3px;
        -ms-border-radius: 3px;
        -o-border-radius: 3px;
        border-radius: 3px;
        cursor: pointer;
        color: #555;
    }

    kbd[title], .key[title] {
        cursor: help;
    }

    kbd {
        border: 1px solid #c4c6ca;
        padding: 10px 20px;
        background-color: transparent;
        flex:1;
        height: 2.2em;
    }

    kbd:active {
        background: #e6e7e8;
    }

    .numpad kbd {
        margin: 3px;
        font-size: xx-large;
        background-color: #ffffff;
    }

    kbd div.background {
        font-size:x-small;
    }

    .numpad kbd:hover, .numpad kbd:active, .numpad kbd:focus {
        background-color: #dceef7;
    }

Is there a way to ensure that the CSS properties for numpad kbd:active and numpad kbd:focus continue to work while using the same strategy with e.preventDefault() and e.stopPropagation()? Any insights would be greatly appreciated.

Answer №1

An unconventional, yet effective solution has been found to address this issue. By implementing a timeout function to prevent entry into the onclick function call, the functionality can be maintained successfully. Here is an example of the code:

 var doubleClickCheck = true;

            scope.call = function(number) {
                if (doubleClickCheck) {
                    var value = scope.number.$viewValue || '';
                    scope.number.$setViewValue(value + number);
                    reset();
                } 
            };

            scope.remove = function() {
                if (doubleClickCheck) {
                    var value = scope.number.$viewValue || '';
                    if (value.length > 0) {
                        scope.number.$setViewValue(value.substring(0, value.length - 1));
                    }
                    reset();
                }
            };

            function reset() {
                doubleClickCheck = false;
                $timeout(function () {
                    doubleClickCheck = true;
                }, 150)
            }

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

Can you identify the syntax of this LESS variable called @grid-breakpoints with breakpoints for xs at 0, sm at 576px, md at 768px, lg at 992px, and xl at

While exploring the bootstrap less port, I came across this interesting syntax: @grid-breakpoints: xs 0, sm 576px, md 768px, lg 992px, xl 1200px; What exactly is this? It doesn't seem to be a map or an array... How can I retriev ...

Searching for a specific value within a list that has been fetched from a database using AngularJs can be achieved by simply clicking on the search button

Seeking assistance on a search functionality issue. I am able to filter search results from a list retrieved from the database and displayed on an HTML page, but facing difficulty in getting complete search results from the controller. When clicking the se ...

Setting the Default Value for Dropdown Options in Angular 2

Back in the Angular 1 days, I had a trick up my sleeve for setting the default option on a dropdown menu: <select data-ng-model="carSelection" data-ng-options = "x.make for x in cars" data-ng-selected="$first"> </select> But now, in ...

How can I retrieve the HEX code of a color from an image when hovering or clicking?

Is there a way to retrieve the hexadecimal code of a pixel within an image displayed on a webpage using jQuery, without relying on the canvas element? I am specifically interested in obtaining the actual color code of the image itself, not the background c ...

Strange Interaction with Nested Divs in CSS Layouts

I am working on a webpage layout and currently it looks like this: https://i.sstatic.net/s4TOb.jpg I am trying to align the purple box inline with the pink box inside the yellow box, which is inside the green box. However, when I change the display proper ...

Tips for properly formatting quotes in JSON to activate a link

I am working with a JSON configuration file that looks like this: "type": "script", "label": "coreapps.archivesRoom.printSelected", "script": "emr.fragmentActionLink(${project.parent.artifactId},\"downloadPatientHistory\", &bs ...

Encountering a Syntax Error within the ng-click function of a button in AngularJS

I'm currently developing a mobile-based web application using angular js along with onsen ui. I've created a follow button on the user's profile page, allowing users to follow each other. However, when the user's profile loads and the a ...

Reversed Sink CSS3 Animation moving to the left side

Currently, I am working on creating an animation that gives the illusion of a div sinking backward and then being pushed to the left once it has finished falling back. Although I am using CSS3 for this project, I am not very familiar with the animation pr ...

Size your grids in HTML5

I have designed an HTML5 grid for a website that consists of "big" squares containing 10 smaller squares each. However, I actually need there to be only 5 squares within each "big" square. Unfortunately, my skills in HTML5 are limited and I am struggling t ...

What is the best way to ensure Font-Face loads fonts successfully on iOS devices?

In the process of constructing a website with Gatsby.js, Styled-Components, and a custom font named 'Montserrat', I encountered an issue regarding font loading. The font appears as expected on desktop browsers during both build and hot-reloading, ...

Is there a way to make a glossy `selectInput()` extend beyond a `bslib::navset_card_pill()`?

The dilemma: My issue is that the selectInput() in my code gets cut off by the lower border of the card: library(shiny) # Version 1.7.4 library(bslib) # Version 0.5.0 ui <- page_fluid( navset_card_pill(nav_panel( title = "No overflow :(&quo ...

Problem encountered while attempting to sort a table with JavaScript and jQuery

Having trouble sorting my table by the content in the first column. I've tried implementing code similar to the one found in this ANSWER, but unfortunately, it's not working as expected for me. When you run the snippet, you can see that the table ...

Include a sharing option within the magiczoomplus feature

My current project involves creating a WordPress site using Artisteer along with several plugins to showcase photo galleries. To enhance the user experience, I purchased an e-commerce WordPress theme which features a share button that I really like. Now, I ...

Is it possible to apply a PNG icon collection using CSS?

Throughout various examples, jQuery plugins, and CSS-styled HTMLElements, I have observed the setting of icons on an image with multiple options. I am eager to implement this practice in my own apps. https://i.sstatic.net/WDCH3.png For instance, in the ...

pressing a button unrelated to the 'close' button still triggers the close event

I have a notification bar that features a button in the center that links to another website. There is also a 'close' button on the far right. However, whenever I click the center button, it also triggers the close button. I tried moving the #cl ...

Issue with $watch in Angular Modal not triggering

Having trouble using $watch with a radio button located inside an AngularJS Modal. The $watch doesn't seem to work when the value changes within the modal. If I move the radio buttons outside the modal, the $watch functions as expected. You can see ...

Implement Conditional Enabling of Forms in JavaScript or jQuery to Support Single Form Usage

I currently have four forms labeled as form-1, form-2, form-3, and form-4, along with three buttons named Button-1, Button-2, and Button-3. Upon loading the first JSP page, it displays form elements on the screen. My query pertains to clicking on button- ...

React Material-UI select component malfunctions when I enclose the menu item within a Tooltip wrapper

Here is a snippet of my code: return( <FormControl sx={{ m: 1, minWidth: 80 }}> <InputLabel id="demo-simple-select-autowidth-label">Age</InputLabel> <Select labelId="demo-simple-select-autowidt ...

Creating a div that is positioned below an input form when it is focused can be achieved using

I am aiming to achieve the following: https://i.sstatic.net/AxIXs.png My goal is to generate a new div positioned directly below the input form that has been selected (each of the 3 input fields displayed will have a distinct div underneath). The selectio ...

Looking to retrieve object values in JavaScript in a dynamic manner?

Imagine having an array of objects with nested objects. The data is always changing, meaning the keys may vary each time. For instance: [ { "uuid": "53e7202c-28c8-4083-b910-a92c946a7626", "extraIdentifiers": { "National ID": "NAT2804" }, ...