Having trouble with the backspace key on mobile devices?


        function createAdditionalDiv() {
            let innerBox = document.createElement('div')
            innerBox.contentEditable = "true"
            innerBox.id = totalBoxes++;
            innerBox.className = "mainBox"
            let mainBox = document.getElementById('mainContainer')
            innerBox.addEventListener('keyup', checkTrigger)
            mainBox.appendChild(innerBox);
            $(".mainBox").on("keydown", function (event) {
                if (event.key === 'Backspace' || event.key === 'Delete') {
                    let deletionMarker = document.createElement('span');
                    deletionMarker.hidden = true
                    deletionMarker.id = "deletionMarker"
                    putDataToCarentPos(deletionMarker.outerHTML.toString(), false);
                    let previousTag = document.querySelector('#deletionMarker').previousSibling
                    while (!previousTag.textContent) {
                        previousTag = previousTag.previousSibling
                    }
                    if (previousTag.tagName === "SPAN") {
                        if (previousTag.className === "toDelete") {
                            event.preventDefault();
                            document.getElementById('deletionMarker').remove();
                            document.querySelector('.toDelete').remove()
                        }
                        else {
                            event.preventDefault();
                            document.getElementById('deletionMarker').remove();
                            previousTag.className = 'toDelete';
                            event.stopImmediatePropagation();
                        }
                    }
                    else {
                        document.getElementById('deletionMarker').remove();
                    }
                }
            });
        }

Hey there! I'm facing an issue with a specific function in my mobile deployed app. You can find the app hosted here. The application is designed for notekeeping and has a feature where typing "<>" allows you to autocomplete from other divs. However, the backspace functionality is not working as expected on mobile devices. When selecting an item from a dropdown and pressing backspace, it selects the entire div instead of deleting characters. This seems to be functioning properly on emulators and web apps, but not on mobile devices. Can anyone suggest what modifications need to be made to resolve this issue?

Specifically, the double backspace to delete feature is not working correctly on mobile devices, even though it works fine on emulators and web apps.

Answer №1

It appears that the issue you are facing is quite intricate and may necessitate further examination. Nonetheless, I have revised your code with explanatory comments to elucidate its functionality. Additionally, I have implemented some potential adjustments that could potentially address the problem, although there's no certainty that this will completely resolve the issue.

function createAdditionalDiv() {
    let innerBox = document.createElement('div');
    innerBox.contentEditable = "true";
    innerBox.id = `box_${totalBoxes++}`; // Creating a unique identifier using backticks
    innerBox.className = "mainBox";

    let mainBox = document.getElementById('mainContainer');
    innerBox.addEventListener('input', checkTrigger); // Substituting 'keyup' event with 'input'

    mainBox.appendChild(innerBox);

    // Using 'input' event instead of 'keydown' with jQuery
    $(".mainBox").on("input", function (event) {
        if (event.inputType === 'deleteContentBackward') {
            let deletionMarker = document.createElement('span');
            deletionMarker.hidden = true;
            deletionMarker.id = "deletionMarker";

            putDataToCarentPos(deletionMarker.outerHTML.toString(), false);

            let previousTag = document.querySelector('#deletionMarker').previousSibling;

            while (previousTag && !previousTag.textContent) {
                previousTag = previousTag.previousSibling;
            }

            if (previousTag && previousTag.tagName === "SPAN") {
                if (previousTag.className === "toDelete") {
                    event.preventDefault();
                    document.getElementById('deletionMarker').remove();
                    previousTag.remove(); // Eliminate the preceding element
                } else {
                    event.preventDefault();
                    document.getElementById('deletionMarker').remove();
                    previousTag.className = 'toDelete';
                    event.stopImmediatePropagation();
                }
            } else {
                document.getElementById('deletionMarker').remove();
            }
        }
    });
}

Furthermore, it is advisable to :

  • Verify Keyboard Event Handling: Confirm the functionality of the "keydown" event on mobile devices by debugging console logs or alerts to ensure the checkTrigger function is triggered upon pressing "Backspace"

  • Explore Alternative Events: Experiment with different events like "keyup" or "input" to detect text deletion in case the handling of "Backspace" is not as expected on mobile devices

  • Adopt jQuery Event Handling: Mitigate conflicts between native JavaScript (addEventListener) and jQuery ($(".mainBox").on(...)) event handling. Validate proper management of "Backspace" events with jQuery on mobile devices

  • Test Across Various Mobile Browsers: Assess whether the issue is browser-specific or even examine it on diverse devices to ascertain if the problem is device-specific

  • Leverage Mobile Debugging Tools: Utilize tools such as Chrome DevTools for Android to inspect events and DOM elements, enabling insights into the behavior upon pressing "Backspace" on a mobile device

I have made an effort to assist you, but it might not provide a definitive solution 😅

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

sending a file to a controller without the use of a form

Hey there, I'm exploring a way to transfer a file from a view to a controller without using a form. My goal is to enable the user to browse for a file and have it sent to the controller via Ajax. Do you think this is achievable? <td>Import ...

Guide on retrieving the value of "form" from a select using jQuery in a Ruby on Rails application

I am struggling to figure out how to use jQuery to pass the value of the form attribute from the select tag. I have been trying different ways, but so far haven't been successful. When using simple_form_for, the input statement looks like this: < ...

Having trouble with ejs.filters?

I'm having trouble grasping ejs filters and getting them to work correctly: Server.js var ejs = require('ejs'); ejs.filters.example = function() { //placeholder for example }; Routes.js app.get('/home', function(req, res) { ...

Error encountered in MySQL and NodeJS: Unable to add new query after invoking quit with transactions

While working on implementing MySQL for NodeJS and Restify, I encountered a flawless experience with queries. However, when attempting to utilize data updating functionality through transactions, I faced the error message: Error: Cannot enqueue Query after ...

A step-by-step guide on how to manually link the npm package 'react native reanimated'

I need some assistance with manually linking the library react-native-reanimated since the automatic cli linking is not functioning properly and causing crashes in my react native app ...

Kotlin object cloning implementation

I currently have 3 different classes that I am working with: Company.kt Class: data class Company(var comCode:String= "",var comName:String=""):Cloneable{ fun copy() : Company { //uses the fields name and property defined in the constructor return Co ...

The network activity displays the Header() redirect, but the actual redirection is not taking place

I have been working on a process that involves checking for a specific cookie when a user lands on the home page. If the cookie is not found, the user gets redirected to login.php. To authenticate the user, I make a POST request to a third-party API using ...

Having trouble initiating the webpack development server

As a newcomer to ES6, I decided to set up my development environment by following a guide for beginners. After completing all the steps as instructed, I reached the point of installing the webpack development server. Upon entering the command npm run bui ...

Is it possible for an android button to trigger the execution of a different button before running its own code

I'm trying to figure out if it's possible for button B to run the code of button A first, before running its own defined code. Right now both buttons A and B are working in my program, but I've come to realize that button A's code shoul ...

What sets Express.js apart from koa2.js in handling asynchronous functions?

I've encountered a situation where I had to set up the router using Express, and it was functioning correctly with the following code: router.get('/',(req,res)=>{ queries.getAll().then(stickers=>{ res.json(stickers) }) ...

What is the most effective way to assign multiple functions to the onClick event of a button, based on a specific property,

I have a special button that generates a specific type of code when rendered: <button>{this.props.text}</button> This button is named ButtonCustom. In the render method of the main container, I am trying to achieve the following: function my ...

Utilize React to showcase all stored items in localStorage in a Material UI List

I have a storage system with multiple items stored in it. I am looking to retrieve all of them and showcase them using a <ListItem> from Material UI. This is my current code snippet: function saveItem(key, value) { localStorage.setItem(key, value) ...

Can you recommend a resource that provides a comprehensive breakdown of which CSS elements are compatible with each internet

Can anyone recommend a comprehensive resource, such as a book or website, that provides a complete list of CSS elements and their compatibility with the major web browsers? Specifically, I am interested in information regarding compatibility with IE8 and F ...

How can I leverage the new linewidth feature in Three.js r91 to make lines appear thicker or wider?

As I was working on my project, I encountered a situation where I needed to create wireframe-style lines with transparency inside to give the appearance of outlining shapes in a comic or cartoon style without actually creating solid objects. These outline ...

What is the best way to reset the size of an element in webkit back to its original dimensions?

I am having an issue with an element that changes to display absolute and covers its parent element on focus via javascript. However, when it goes back to its default state on blur, it does not return to its original position. This problem seems to only o ...

Generate a rubbery effect when hovering over an element

Currently working on my website and I want each letter in the title to have a rubbery effect when hovered over. A great example of this can be found on the home section of . I was considering turning each letter into a span with a specific class that, upo ...

Tips for verifying the text input in an EditText within an Android application development process

As a beginner in Android app development, I have encountered an issue with my code in the .java file. My goal is to display a Toast message if either EditText field 1 or 2 is empty, otherwise, show the result using result.setText(). However, when testing ...

Tips for sending multiple forms and having PHP interpret it as a single form using $.ajax

I am working on an email function using $.ajax with 3 different forms that are loaded through an ajax request. When a user clicks on a button, the current form will disappear and a new form will appear. The challenge I am facing is how to send data from al ...

How to use the route.navigate() method in Angular 9 to open a URL in a new tab with a query string

When a button is clicked within a table in our application, I have to open a new tab with details of a specific record from the table. Currently, the code I am using navigates to a new URL and uses resolvers to fetch data from the backend on the new page. ...

Issue with VueJS where input placeholder text is not being shown

I'm trying to make the placeholder text in my input field dynamic within my Vue app. Here's what I have so far: <template> <div> <input type="text" v-model="text" :placeholder="placeholder" /> </div> </template ...