There's a sneaky distraction lurking in the cordova, angular, and ionic trio that constantly grabs

When striving for Accessibility in my Cordova app, I encountered an issue where focusing on elements upon entering screens was proving to be a challenge. Despite using some techniques to set focus on an element successfully, the focus is quickly lost (confirmed by the "blur" event) even before Apple's VoiceOver or Android's Talkback can activate, or before the user can interact with anything.

I attempted wrapping the focus-setting code within the $timeout function to ensure it occurs after all rendering is complete, but unfortunately, this did not resolve the issue. It seems like there might be some interference from Cordova, angular 1, or Ionic that is causing this unexpected behavior.

Has anyone else faced a similar situation and found a workaround to maintain focus on elements?

Answer №1

I implemented the following directive specifically for Cordova-Ionic:

app.directive('focusableInput', function($timeout,$log) {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            focusState: "=",
            onGo: "&"
        },
        link: function(scope, element, attr, ngModel) {

            var moveCursorToEnd = function(el) {
                if (typeof el.selectionStart == "number") {
                    el.selectionStart = el.selectionEnd = el.value.length;
                } else if (typeof el.createTextRange != "undefined") {
                    el.focus();
//                    var range = el.createTextRange();
//                    range.collapse(false);
//                    range.select();
                }
            }

            scope.$watch("focusState", function(state) {
                $log.debug("focusableInput - focus state change: " + state,element);
                if (state) {
                    //cordova.plugins.Keyboard.show();
                    $log.debug("focusableInput - do focus!!!",element);
                    $timeout(function() {
                        element[0].focus();
                        moveCursorToEnd(element[0]);
                    }, 300);

                    //element[0].focus();
                } else {
                    element[0].blur();
                }
            });

            element.on("keydown", function(event) {
                console.log("pressed: " + event.which, event);
                //$log.debug(attr);

                if (event.which == 13) {
                    $timeout(function() {
                        scope.onGo();
                    });
                }
            });



            scope.$watch(
                    function() {
                        return ngModel.$modelValue;
                    },
                    function(newValue) {
                        $log.debug("focus input model changed: ", newValue);

                        if (newValue) {
                            var endChar = newValue[newValue.length - 1];
                            $log.debug("endChar",endChar);

                            if (endChar == " " || endChar == ",") {
                                $timeout(function() {
                                    scope.addItem();
                                });
                            }
                        }
                    }
            );
        }
    };
});

Here is how you can use it:

<input type="email" placeholder="Email" 
               class="login-custom-input"
               focusable-input 
               focus-state="textAreaFocusState"
               ng-model="meeterAccount.email"
               ng-focus="onSignUpLoginEmailFocus()"
               ng-blur="onSignUpLoginEmailFocusLost()"
               >

The focusable-input directive requires the focus-state attribute.


Hopefully this will be beneficial for your needs.

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

How can I retrieve the OptionID value upon click?

How can I retrieve the value of OptionID when the Add button (.plus-link) is clicked? Each list item may contain a dropdown select menu or not. <ul> <li> <div class="menux"> <div class="text-block"> ...

Utilizing titanium to develop a functionality that listens for button presses on any area of the screen

I am trying to simplify the action listener for 9 buttons on a screen. Currently, I have individual event handlers set up for each button, which seems inefficient. Is there a way to create an array of buttons and manipulate them collectively? For example ...

How can you connect templates to actions and models in AngularJS directives?

I am currently working on building a custom directive that has presented me with two challenges. Take a look at the template provided below... restrict: 'A', require: '?ngModel', template: '<div>' + ' ...

How can I display a limited number of items from this loop in Node.js using EJS?

If the loop contains more than 3 items, a "show more" button will be added to display the remaining items. <% include ./partials/header %> <div class='container'> <p><a class='btn btn-primary btn-large'href=&a ...

Creating a jQuery datatable with unique sorting and filtering functionality

I have implemented jquery datatables in my MVC4 application using a simple configuration. In fact, I included a small jquery snippet in my layout.cshtml file that automatically styles all tables in the application without any customization needed. $(".dat ...

Creating custom mesh Morph Target Animations using THREE.js

I'm currently working on creating an animation that showcases the revolution of a 2D function around an axis to produce a 3D surface. Successfully rendering the surface, my next task is to implement the animation section using MorphTargets. Despite m ...

Can an image map be utilized within an <a> element?

I am attempting to implement an image map within an <a> tag. It seems to be functioning correctly in Chrome, but I am encountering issues with it not working in Internet Explorer. Is it acceptable to use an image map in an <a> tag? Below is th ...

Maintain the fancybox open even in case of ajax errors

I'm having an issue with my code where the fancybox closes automatically after displaying the error message briefly. I want it to remain open so that users have more time to fix their errors. What could be causing this problem? $(document).ready(func ...

Can Sequelize be used to perform queries based on associations?

I have designed a data structure that includes an n:m relationship between "Rooms" and "Users." My current goal is to remove or delete a room. To accomplish this, I need to verify if the user initiating the deletion process is actually present in the room ...

HTML dropdown menu to trigger an action with the submitted URL

I need assistance creating a menu of options with corresponding URL actions. Here is the structure I have in mind: <form> <select> <option value="1">1</option> <option value="2">2</option> <option value="3 ...

Difficulty with parsing JSON in JavaScript and retrieving values by key

I'm encountering an error response within the ajax error function; $.ajax(Request).error(function (Response) { var content = JSON.stringify(Response.responseText); var obj = JSON.parse(content); console.log("Response.r ...

What is preventing me from installing socket.io?

I keep seeing an error in the console, what could be causing this? npm ERR! code 1 npm ERR! path E:\full-stack\proshop-2\socket\node_modules\utf-8-validate npm ERR! command failed npm ERR! command C:\WINDOWS\system32&bso ...

What is the process for including parameters in a javascript/jquery function?

This unique script enables you to click on a specific element without any consequences, but as soon as you click anywhere else, it triggers a fade-out effect on something else. Within the following code snippet, you can interact with elements inside $(&apo ...

Error handling with JSON in Parse.com causes compatibility issues in Express js 4

I need help figuring out why the data I send in a POST call to an Express JS server hosted on Parse.com is not being received properly. This is how I send the data: var data = new Array(); data["firstName"] = firstName; data["lastName"] = las ...

Utilizing TIMING=1 with eslint to evaluate rule efficiency on Windows operating system

On the official ESLint website, there is a section titled Per-rule Performance. It explains that "setting the TIMING environment variable will prompt the display of the ten longest-running rules upon linting completion, showing their individual runn ...

div or paragraph element nested within a flex container

How can I make the logo text div tag, called Title, take up its content space inside the parent flexbox without wrapping? I prefer not to set the Title div to 100% or use white-space: nowrap. I simply want it to behave like a regular div where it fills it ...

The issue of nested tabs in Bootstrap 3 causing problems with sliders within the main tab has

Hello, I am utilizing Bootstrap 3 tabs. Within the nav-tabs, there are three tab-panes: the first tab contains text only, the second tab includes a slider, and the third tab has nested tabs. However, I encountered an issue where the slider in the nested t ...

Display or conceal a div based on checkbox selection

I am trying to implement functionality to show/hide a div when a single checkbox is selected. Currently, it works with "Select all" but I am struggling to make it work with a single checkbox. Below is the code for "Select All": JS: <script language=&a ...

Retrieve all the items listed in the markdown file under specific headings

Below is an example of a markdown file: # Test ## First List * Hello World * Lorem Ipsum * Foo ## Second List - Item 1 ## Third List + Item A Part of Item A + Item B ## Not a List Blah blah blah ## Empty ## Another List Blah blah blah * ITEM # ...

Setting up a spotlight in three.js

After following the initial tutorial for three.js, I hit a roadblock when attempting to incorporate a point light into the scene. Despite numerous attempts to tweak my code, the cube remains unlit by the point light. var scene = new THREE.Scene(); var cam ...