Alter the class when $dirty occurs in Angular

I've recently started working with angular js and am attempting to incorporate animations into my login page.

I have created a controller that will modify the class of the input field when clicked and when blurred.

app.controller("con", function($scope,$rootScope,$http,$location){
  $scope.OnClick = function(event){
      $scope.username1 = "active";
      $scope.password1 = "active";
      };

  $scope.OnBlurUserName = function(event){
      if(this.username)
          $scope.username1 = "active";
      else{
          $scope.username1 = "input"; 
      }
      };
      $scope.OnBlurPassword = function(event){
          if(this.password)
              $scope.password1 = "active";
          else{
              $scope.password1 = "input"; 
          }
          };
});


        <div class = "input" ng-class="username1" ng-controller="con">
        <label class="label">Username</label>
        <input class="field" name="username" type="text" ng-model="username" 
        ng-click="OnClick()" ng-blur="OnBlurUserName(username)" required>

        <div class="input" ng-class="password1" ng-controller="con"> 
        <label class="label">Password</label>
        <input name="password" class="field" type="password" ng-    model="password"
        ng-click="OnClick()" ng-touched="OnClick()" ng-blur="OnBlurPassword(password)" required>

When I click on the username and password fields, the classes are successfully changed. However, if I enter text in the username field, press a tab button, then enter text in the password field, the class doesn't change.

How can I determine if the user has begun entering input and apply the same class that is applied on click?

Thank you in advance!

Answer №1

If you want to customize the appearance of your input based on its state, consider using the ng-class directive like this:

ng-class="{'your-class': yourForm.yourInputsName.$dirty}"

For more information, check out this helpful answer on Stack Overflow: How can I add a class to an input if the input is changed with AngularJS?

You may also find this article about form validation and manipulation with AngularJS useful: https://scotch.io/tutorials/angularjs-form-validation

Answer №2

The issue was resolved by including both ng-focus and ng-click in the 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

Is there a way for me to update the button text and class to make it toggle-like

Is there a way to switch the button text and class when toggling? Currently, the default settings show a blue button with "Show" text, but upon click it should change to "Hide" with a white button background. <button class="btn exam-int-btn">Show< ...

`Accessing information within a nested JSON array``

I'm currently parsing through the JSON data below. While I can extract the id and name fields without any issues, I am encountering a problem when trying to access json.templates[i].dailyemails.length, as it always returns 0. Below is the structure o ...

Problems with Google Maps Event Tracker

I recently inherited a section of code that utilizes the Google Maps API to place markers on a map along with information windows. Below is an excerpt from the code responsible for creating the markers and setting up event listeners: if(markers.length > ...

Can the swap operation be carried out using web3.js and a forked HardHat implementation?

Embarking on my journey into ethereum development, I am currently engrossed in crafting a basic script that facilitates swaps using web3.js. To begin with, my web3 is establishing a connection to the HardHat forked server. The first step involves setting ...

The implementation of a custom event for jQuery rows is not functioning as expected

I need assistance with jQuery code to add click events only to columns in a row that have text in the first column. Specifically, I want to hide rows containing "1/" in the first column when clicked on for the first row. <table class="results"> < ...

The Mongoose connection pool has been shut down

I am currently working on a web application that retrieves data from a Mongo database. I have set up 2 cron jobs using Heroku scheduler to run daily and perform tasks on a remote database. The issue arises when these jobs need to conclude and close the con ...

Tips for creating a window closing event handler in Angular 2

Can someone guide me on how to create a window closing event handler in Angular 2, specifically for closing and not refreshing the page? I am unable to use window.onBeforeunLoad(); method. ...

Updating the content of a window without the need to refresh the page using JavaScript

Is there a way to navigate back to the previous window in chat_user without refreshing the entire page when the back button is clicked? Below is the code I have tried: <a href="" onclick="window.history.go(-1); return false;">back</a> ...

How to showcase base64 encoded images in pug (jade) with node.js

Can anyone help with decoding this mysterious data and displaying the image? I'm using pug as my template engine. Below is the questionable data that needs to be shown as an image: /9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQ ...and so f ...

Is it possible to use a server action in Next.js to both retrieve data and refresh the page?

I am currently working on developing a web application using Next.js (13.4.9) and I intend to utilize server actions. These server actions will involve sending data to the server, which will process the data and return a string back to me. The challenge I& ...

What could be causing these cards to not show up correctly?

I am currently incorporating Angular Material's cards in a material grid. Here is the code snippet that I am working with: http://codepen.io/anon/pen/YWwwvZ The issue at hand is that the top row of images extends off the screen at the top, and the b ...

Need help triggering Ajax code upon clicking a link?

Can someone help me find the issue with my script? Below is the code snippet: <script> $(".icross").click(function(e){ e.preventDefault(); var obj = $(this); $.ajax({ type: "GET", url: "supprimer.php", data: 'id=&a ...

What is the best way to organize a massive file over 10Gb filled with words?

I've been presented with this interview query: You have an input file containing words (which could be a jumble of letters) separated by commas, and the file size is 10 GB or more. Can you devise an algorithm to sort all these words? Keep in min ...

Slide the next section over the current section using full-page JavaScript

I'm currently developing a website utilizing the FullPage.JS script found at this link . My goal is to have the next section slide over the previous one, similar to what is demonstrated in this example. I've attempted setting the position to fix ...

Using jQuery to access a server-side SQL database through PHP

After searching for an example on connecting a client to a server's SQL database using JQuery, AJAX, and PHP, I came across this seemingly well-executed guide: Example Link. All my PHP files and the jQuery library (javascript-1.10.2.min.js) are contai ...

Saving URI from HTTP request in Javascript using Google Drive API: A step-by-step guide

I'm struggling to understand and implement the instructions provided at https://developers.google.com/drive/v3/web/manage-uploads#save-session-uri While I've successfully used javascript to upload files to Google Drive, I am now faced with a cha ...

Achieve automated zooming out using highcharts-ng through code

Currently, I am using Highcharts-ng as seen on https://github.com/pablojim/highcharts-ng Upon inspecting the source code, I have noticed some interesting functionalities in the directive utilizing scope.$on which I can leverage for broadcasting. One examp ...

Manipulating Arrays in JavaScript: Techniques for Extracting Values Buried in Nested Objects

I am working with an array of objects that contain multiple "Book" objects with dynamic keys. My goal is to filter the objects in the array to only include those that have at least one new "Book" object. For example: const arr = [ { id: '123&ap ...

Filtering data in AngularJS controller file allows you to easily manipulate and

Searching for a solution in the following function where the 'data' element contains multiple record sets. My goal is to filter it so that only the row with an id matching 'selectedModelDrv' is returned. I've attempted this but ke ...

Posting data using the form method="post" in Vue does not seem to be working

My goal is to create a website that allows users to reserve a seat using a specific password and time slot. I've set up a form where users can input the password and their name, then submit the form to make the reservation. However, I'm facing an ...