Display the keyboard and keep it visible when the password toggler is activated

Looking for help with my code that displays a password input with a toggler overlaying it. The ion-toggle directive is used to switch between the visible and hidden password inputs.

<label class="item item-input">
    <input placeholder="Password" ng-hide="showPassword" type="password">
    <input placeholder="Password" ng-if="showPassword" type="text">

    <ion-toggle ng-model="showPassword" toggle-class="toggle-energized">&nbsp;</ion-toggle>
</label>

Currently, when the toggler is in focus, the software keyboard disappears. This requires the user to tap back on the input to bring the keyboard back up.

Is there a way to automatically show/keep the keyboard visible when the toggler is in focus? I've attempted to create a directive to force focus back to the inputs, but it seems cumbersome, especially with two inputs involved.

Here's a simple demo. Keep in mind that a keyboard won't appear on a desktop browser.

Any advice or solutions would be greatly appreciated. Thank you.

Answer №1

I developed a custom directive that focuses on the password field when the toggle button is clicked. Additionally, I simplified the HTML code for the password field to only display one input field instead of two.

Code Snippet:

<ion-content>
  <label class="item item-input" ng-init="pwd">
    <input placeholder="Password" ng-attr-type="{{showPassword ? 'text' : 'password'}}" 
      ng-model="pwd" do-focus="showPassword"/>

    <ion-toggle ng-model="showPassword" toggle-class="toggle-energized">&nbsp;</ion-toggle>
   </label>
</ion-content>

Custom Directive:

.directive('doFocus', function(){
  return{
    link: function(scope, element, attrs){
      scope.$watch(attrs.doFocus, function(){
          element.focus();
      });
    }
  }
});

View Forked Codepen

Updated Details:

The directive is designed to accept the toggle flag name as an attribute, such as do-focus="showPassword", allowing the directive to watch for changes on the specified variable within the scope using attrs.doFocus. When the toggle button for showPassword is clicked, the watcher function triggers. The watcher function includes parameters for newValue and oldValue, although they are not utilized in this particular case. However, they could be useful if additional actions need to be taken based on the change in value.

Answer №2

One interesting issue with the Cordova keyboard plugin is that it can close the native keyboard but not open it, except on Android. This can be especially problematic when dealing with multiple input fields, particularly on iOS. Sometimes, you may find yourself having to close the keyboard only to have it reopen again. If you are only targeting Android, you can use this plugin to open the keyboard with a tap using on-tap="foo()". However, currently there is no other known way to interact with the native keyboard or keyboard events. One workaround could be to force focus back to the password input on a toggle tap, although this is not an ideal solution. It might be worth giving the plugin a try to see if you can make it work on both platforms. Keyboard Plug-in: https://github.com/driftyco/ionic-plugin-keyboard

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

Implementing a Unique Approach to Showcase the Initial PDF Page as Cover through Django and JS

I would like to enhance my script so that when a user hovers over an object on the template, a PDF cover page can be set for each object. Current process: Currently, I am able to upload files in the .pdf and .epub formats for each object with additional ...

"Encountering a bug with setting the pixel ratio in Three.js on IOS

I am currently working on a three.js website where I load a json file using ObjectLoader. Everything works perfectly on all platforms: Windows with all desktop browsers, and Android phones with all browsers. However, IOS (specifically iPad Air) is causing ...

Unable to sort nested JSON data in ngTable

I have successfully built an angularjs application using ngTable, however, I am facing an issue with sorting. The JSON structure is nested but the values are appearing correctly in the table. If anyone has a solution to this problem, please let me know. ...

Integrating dual Google Maps onto a single HTML page

I'm facing an issue with implementing two Google maps on a single page where the second map seems to be malfunctioning. Below is the code I am currently using: <style> #map-london { width: 500px; height: 400px; } #map-belgium { wi ...

Design a personalized .OBJ / .MTL file and showcase it using the power of three.js

I attempted to create a basic blender model, export it to .obj/.mtl, and then render it with three.js. However, I am experiencing an issue. I have downloaded and uploaded the official three.js demo, and the objmtl-loader is functioning properly with the or ...

How can I utilize VeeValidate 3's locale message JSON files without the need for Node.js or an HTTP server?

With VeeValidate 2, the locale message files are in javascript format, making it possible to use them by including <script src='./vee-validate/dist/locale/ja.js'> without needing Node.js or an Http Server. However, with VeeValidate 3, the ...

When utilizing Angular 2, this message is triggered when a function is invoked from the Observable

One of my services is set up like this: @Injectable() export class DataService { constructor(protected url: string) { } private handleError(error: Response) { console.log(this.url); return Observable.throw(new AppError(error)); ...

What causes the website to malfunction when I refresh the page?

I utilized a Fuse template to construct my angular project. However, upon reloading the page, I encountered broken website elements. The error message displayed is as follows: Server Error 404 - File or directory not found. The resource you are looking fo ...

Send a complex Json object in a POST request

Hey guys, I'm new to the world of web development and I've encountered a challenging issue. I have a complex object with numerous fields filled by a JavaScript function that needs to be passed to a C# HttpPost Call. I attempted to use JSON.Strin ...

From Angular JS to Node Js with the help of the Express Js framework

I've been attempting to run an AngularJs front-end with a NodeJs server using ExpressJs. The main purpose of this program is to take user input and display it on the server console. With my limited JavaScript knowledge, I've put together the foll ...

Access in-depth data by clicking on a map to get detailed information

Recently, I took on the challenge of managing a golf club website after the original creator had to step away. One urgent issue I need to address is fixing a malfunctioning flash animation that provides information about each hole on the course. My plan is ...

How can I troubleshoot Ajax not loading my additional external JavaScript files?

$(document).ready(function () { $("#livesearch").on("keyup",function(){ var search_term = $(this).val(); $.ajax({ url:"ajax-live-search.php", type:"POST", d ...

Issue with useEffect EventListener in REACT HOOKS

Recently, I attempted to create a simple Snake-Game using REACT. Everything was going smoothly until I encountered an issue with using useEffect for moving the "snake" when a keydown event is triggered. The challenge arose when trying to implement moveSnak ...

Tool tips not displaying when the mouse is moved or hovered over

I have customized the code below to create a multi-line chart with tooltips and make the x-axis ordinal. However, the tooltips are not displaying when I hover over the data points, and I want to show the tooltips only for the data in the variable. https:/ ...

Using JavaScript to extract variables from parsed JSON data

Could someone please help me understand how to run this code smoothly without encountering any errors? var test = 'Something'; JSON.parse('{"xxx": test}'); I am inquiring about this because I have a JSON object containing variables th ...

Assigning a one-of-a-kind identifier to a cell within a table that was dynamically created using JavaScript and

When using JavaScript to create table cells and rows and populate them with information from a JSON file, I encounter an issue where the unique ids assigned to the table cells in my code are undefined. This causes problems when trying to reference these id ...

What steps should I follow to ensure that the processData function waits for the data returned by the getData function in Angular?

After calling the getData function, each object is stored in an array and returned. Now I need the processData function to await these results from getData and then further process them. However, when I try to console.log(cleaningData), I don't see an ...

Recursively apply a custom condition to filter a tree

Within this tree structure, I need to extract items with a release version of 0.0.1 to use in building my navigation based on that specific release number. { title: '', items: [ { title: '', path: '', ...

When using requirejs and vue.js together, the error message "Vue is not defined" may be encountered

My code snippet looks like this: <script type="text/javascript" src="../../node_modules/requirejs/require.js"></script> <script type="text/javascript" src="../../node_modules/vue/dist/vue.js"></script> <script>console.log(Vue ...

Difficulty encountered when attempting to utilize keyup functionality on input-groups that are added dynamically

I've exhausted all available questions on this topic and attempted every solution, but my keyup function remains unresponsive. $(document).ready(function() { $(document).on('keyup', '.pollOption', function() { var empty = ...