Discover the steps to modify the input field type with just a keypress in angularjs

I need help changing an input field type from text to password.

Currently, I am able to change the input field type from text to password when clicking on it. However, I also want the type to change from text to password when tab is pressed.

Any assistance would be appreciated.

The code snippet where I am currently changing the type on click function is as follows:

<input type="text" name="password" class="form-control5 floating-input" ng-model="vm.password" required autocomplete="off" onclick="(this.type='password')" value="secure"/>
<span class="highlight"></span>
<span class="bar"></span>
<label for="password" class="field-name">Password</label>

label {
  font-size:16px;
  font-weight:normal;
  position:absolute;
  pointer-events:none;
  left:5px;
  top:10px;
  transition:0.2s ease all;
}

.highlight {
  position:absolute;
  height:60%;
  top:25%;
  left:0;
  pointer-events:none;
  opacity:0.5;
}

.floating-label {
  position:relative;
  margin-bottom:20px;
}

.floating-input , .floating-select {
  font-size:14px;
  padding:4px 4px;
  display:block;
  width:100%;
  height:30px;
  background-color: transparent;
  border:none;
  border-bottom:1px solid #ccc;
}

.floating-input:focus , .floating-select:focus {
     outline:none;
}

.floating-input:focus ~ label, .floating-input:not([value=""]):valid ~ label {
      top:-18px;
      font-size:14px;
      color:#333;
      font-weight: 600;
}

Answer №1

Utilize the onfocus function just like you would with onclick. Here's an example:

<input type="text" onfocus="this.type='password'" value="Password"/>

You can also switch the type back on focus out by using the onblur event.

<input type="text" onblur="this.type='text'" value="Password"/>

Answer №2

If you're looking for a solution, give this a try.

var app = angular.module('cft', []);
app.controller('changeproperty', function($scope) {
  $scope.vm = {};
  $scope.fieldType = 'text';
  $scope.changeField = function(){
    $scope.fieldType = 'password';
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="cft">
  <div ng-controller="changeproperty">
    <input type="{{fieldType}}" name="password" class="form-control5 floating-input" ng-blur="changeField()" ng-model="vm.password" required autocomplete="off" value="secure"/>
    <span class="highlight"></span>
    <span class="bar"></span>
    <label for="password" class="field-name">{{fieldType}}</label>
  </div>
</div>

Answer №3

To connect the type attribute of the input with a controller model, follow these steps:

<input type="{{inputType}}" />

Additionally, include the ng-focus and ng-blur attributes:

<input ng-focus="updateType('email')" ng-blur="updateType('text')" />

The controller method should be defined as follows:

 $scope.updateType = function(type) {
    $scope.inputType = type;
 }

Demo

Answer №4

To successfully tackle this issue, I incorporated a boolean variable along with a function that alters the state of this variable within the controller. Here is an example:

$scope.showPassword = false;
$scope.togglePassword = function() {
    $scope.showPassword = !$scope.showPassword

}

Subsequently, in the HTML layout, I integrated this feature using a ternary operator:

<input type='{{(showPassword)? text : password }}' />
<i class='{{(showPassword)? icon-eye-open : icon-eye-close }}' ng-click='togglePassword()' ><i/>

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

Ensuring radio button validation prior to redirecting to contact form

I created a survey form using HTML and CSS. I am looking to add validation to the questions before redirecting to the Contact form page. Currently, when a user clicks on the submit button in the Survey form, the page redirects to the contact form. Howeve ...

What could be causing the movement of my main navigation links when hovering over another top-level element?

I am struggling to pinpoint the origin of a particular issue I am encountering with my menu. The problem arises when I hover over a top-level element, causing all elements below it to shift to the right: (Take note of the top-level elements being affected ...

A Guide to Displaying HTTP Error Messages on the Angular Login Page

When encountering a form validation failure on my login page, I utilize ngMessage to display an error message. However, I now want this div to be displayed in the event of an HTTP 500 error. While I can retrieve the corresponding error message when an HTTP ...

The ng-click function ceases to work properly once a toaster is defined within a

When I disable the noty service, the "Show Toast" button ng-click events are enabled. However, when I try to use the noty service, it stops all my ng-click events. You can find the implementation of the noty service in "tempService.js," which is essential ...

Organize various base arrangements within Angular version 2

One thing I can accomplish in my angularjs application using ui.router is: $stateProvider .state('app', { url: '', abstract: true, template: '<div data-ui-view></div>' ...

Collapse or display div elements with jQuery - First close all other elements before opening the selected item

The Problem at Hand Currently, the script is expected to hide all elements with the "gallery-collapse" class and reveal the specific content based on the clicked link. However, sometimes multiple divs might appear simultaneously when switching between ite ...

The functionality of AngularJS UI.router multiple views on a single page is not functioning properly

.state("home",{ url:'/home', templateUrl:'./resources/js/baseapp/ContactHome/views/ContactHome.html' }) .state("home.contacts",{ url:'/home', views:{ "contactByName":{ templateUrl : ' ...

:Incorporating active hyperlinks through javascript

Hey there, I've encountered a little conundrum. I have a header.php file that contains all the header information - navigation and logo. It's super convenient because I can include this file on all my pages where needed, making editing a breeze. ...

Establish the height of the root to be the same as the height

As a newcomer to HTML and CSS, I am struggling with setting the background color of my root div on a webpage. My code is quite complex, but let me simplify the issue for you by providing a sample problem below. <style> #background{ background-c ...

Child div height exceeds parent container bounds

Take a look at this example here: http://fiddle.jshell.net/dwuZp/15/ Can you determine why the "icons" div is overflowing the height of its parent div, "logoTimeAndIcons"? To visualize the issue, try changing the height to 50%. Your assistance is great ...

Generating a registration key for a Laravel application using a distinct frontend application

I am in the process of developing an Angular application that will be connected to a separate Laravel backend. My focus at the moment is on user authentication, specifically user registration. However, I have encountered some errors along the way: Error ...

Updating a $scope variable within a loop in AngularJS

Attempting to modify a $scope variable: Example: $scope.variable_1 $scope.variable_2 ... Desired way of updating it: for (i=0; i<2; i++) { $scope.variable_$i = 1; } Wanting to access "$scope.variable_1" using the "i" index in each loop. Any ...

Having challenges retrieving information from MySQL in AngularJS

As a beginner in angularJS, I am trying to display all customers from MySQL. Here is the code I have written in the controller and service: app.controller('CustomersController', function ($scope, customersService, $http) { init(); function ini ...

Could it be a typical issue that arises when utilizing select elements and including both the $options.placeholder and id attributes for custom validation, resulting in $invalid always being

This is a sample snippet from my HTML document: <form name="form" ng-submit="submit()" novalidate> <label> Gender</label> <select name="gender" ng-model="gender" placeholder="Date of Birth - Gender" ng-required="true"> ...

Superimpose two actionlinks and prioritize the one appearing above

I have implemented a menu similar to http://tympanus.net/Tutorials/SlideDownBoxMenu/. Below this menu, I have placed a webgrid. The issue I am facing is that when I hover over the menu, the slidedownbox with two action links pops up directly over the heade ...

Front Page Luma Design for Magento Platform

As a newcomer to Magento, I am currently using the Luma Home Page which appears blank. My goal is to incorporate an image that spans the entire browser viewport. Is it possible to achieve this through the admin panel by adjusting the CSS within the home pa ...

retrieving values from an array in ng-model and displaying them within <input

I am seeking assistance with retrieving values from an array and formatting them into a comma-separated list. Visit this link for reference Within my input tag: <input type="number" ng-model="data.price1[$index]" ng-change="pricecalc1($index)" placeh ...

Struggling to align nested grids in the center

I'm currently following a mobile-first approach in designing my website. The entire website is contained within one main grid, and I also want to include smaller grids that will serve as links to other HTML pages. I created a sample nested grid but am ...

Arranging Bootstrap divs vertically and then horizontally in mobile view

My website layout is relatively straightforward. It consists of a header at the top, a navigation bar with an image inside a column on the left, and the main content displayed on the right side. https://i.stack.imgur.com/yYzaG.png For mobile responsive vi ...

Hide additional tabs on the page when the width of the tabs exceeds the page width

I am currently working on a Google module tab application. I have successfully added the tab control with drag and drop functionality within a specific area. However, I now need to limit the number of tabs displayed on the page. Regardless of the total cou ...