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

How to position preloader at the center in materializeCSS

Given this situation: I attempted to implement this Preloader with position:fixed and centered. My approach was as follows: .loader { position:fixed; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); } <link rel="stylesheet" href="h ...

The accuracy of getBoundingClientRect in calculating the width of table cells (td)

Currently, I am tackling a feature that necessitates me to specify the CSS width in pixels for each td element of a table upon clicking a button. My approach involves using getBoundingClientRect to compute the td width and retrieving the value in pixels (e ...

Utilize a jQuery button to horizontally align text with ellipsis

I am currently struggling to align some text with ellipsis horizontally alongside a jquery button and I cannot seem to determine the reason for this issue. You can view my problem on jsfiddle: http://jsfiddle.net/5bVDu/ <span> <span id=&apo ...

Issue with database connection on iOS device seems to be causing trouble

Working on an Ionic Phonegap project for iOS has been a challenge. One specific method in Appdelegate.m caught my attention as it makes an AJAX request to fetch a text file from a server, containing a crucial URL necessary for the app's functionality. ...

Develop a navigation system with forward and backward buttons to easily navigate between

I'm currently delving into angular and striving to implement a next/back button for showcasing various views. Within my repository, I have a template named example.html <h1>{{ message }}</h1> which is utilized by multiple controllers: ...

Sending an HTTP GET request to an API can result in different outcomes depending on whether it is sent to localhost or to a remote server. In some cases, the

My setup involves Angular for the front end and Slim for the back end. Interestingly, when I send a request locally, everything functions smoothly, and I receive the JSON response as expected. http://localhost/domain/api/jobs However, the situation chan ...

Trouble with highlighting the chosen menu item

I have been attempting to implement this code snippet from JSFiddle onto my website. Although I directly copied the HTML, used the CSS inline, and placed the Javascript in an external file, the functionality does not seem to be working properly. Feel free ...

Encountered a "no login transports available" error while trying to use Firebase

Currently utilizing Firebase's AngularFire for Facebook logins and encountering an issue with the provided code. Below is my HTML code (including ng-click to initiate login): <a ng-click="loginWithFacebook()" class="btn btn-block btn-social btn- ...

Arranging cards in a stack using VueJS

I am currently working with a small vue snippet. Originally, I had v-for and v-if conditions in the snippet, but due to issues reading the array, it is now hardcoded. The current setup produces three cards stacked on top of each other. I am exploring opti ...

Display an assortment of various categories once every other time

As a novice AngularJS user, I am looking to showcase a collection in my HTML code using a specific directive: displaying items alternately on the left and right side. For example, the first item on the left, the second on the right, the third on the left, ...

Position two div elements so that the second div expands to occupy the entire available space

I am struggling with aligning two divs within a container div, one on the left and one on the right. I am using flexbox for alignment purposes, as shown in the code snippet below. .box { display: flex; align-items: center; just ...

updating the HTML DOM elements using JavaScript is not yielding any response

One way that I am trying to change the background of a div is by using a function. Below is an example of the html code I am working with: $scope.Background = 'img/seg5en.png'; document.getElementById("Bstyle").style.background = "url("+$scope.B ...

What is the best way to display breadcrumb text on a new line within a pop up when the width exceeds without resorting to a scroll bar

Presently, my breadcrumb has a scrollable wrap with text overflow I want to make the overflowed text continue on the next line. How can I achieve this? The CSS code I am using for the image attached is as follows: .breadcrumb-css { text-overflow: ellip ...

What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in t ...

Unable to access image from JSON within Mobile Angular Ui utilizing Angular Js

Currently, I am able to retrieve various data from JSON such as Price and Display Order, but I am facing difficulty in fetching the image. HTML: <div ng-controller="chooseProductDescription"> <div ng-repeat="cat in productDescrip ...

Increasing the maximum width of an image in CSS is necessary to see the max-height take effect

Within my HTML structure: <Col md="6"> <div className="hero-section"> <div className={`flipper ${isFlipping ? "isFlipping" : ""}`}> <div className="front"> <div className="hero-section-content"> ...

Synchronous Http calls in AngularJS Promise Handling

I'm experiencing an issue with the then function not getting called in one of my controllers when making multiple synchronous http calls. In the code snippet below, the console.log(data) statement in my controller is not being triggered as the flow d ...

Creating boxes with equal heights in HTML and CSS

I have created this menu using a combination of html and css. The layout consists of 3 boxes, each with different content. My goal is to ensure that all boxes are the same height within the design. However, due to specific responsive requirements, I cannot ...

Subdomain Dilemma: Trouble with Images in Internet Explorer

Currently, I am working on creating a basic website on a subdomain. Everything appears to be running smoothly in Google Chrome except for one issue with the header image not displaying in IE8. The image link is . It seems that the problem may stem from IE ...

Conceal or reveal buttons using JavaScript

I am struggling with a function that is supposed to hide certain buttons and create a new button. When I click on the newly created button, it should make the previously hidden buttons visible again. However, the function does not seem to work properly. ...