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

Using VueJS to Bind an Array of Integer Values to Checkbox Models

I encountered a discrepancy between VueJS 1.0 and VueJS 2.0 regarding checkbox behavior. In VueJS 1.0, when binding checkboxes to a list of integers in v-model, the integers do not register as checked attributes. Below is the HTML code snippet: <div i ...

Having trouble receiving a JSON response during unit testing with Karma and Jasmine in AngularJS?

describe('getEmployeeDetails', function () { var getEmployeeDetails, httpBackend; //2. beforeEach(function () { //3. load the module. module('nodeApp'); // 4. obtain your service and $httpBackend for mocking. inject(fun ...

Implementing the ui-tinymce Directive Within a Different Directive

I am attempting to implement the ui-tinymce directive within another directive: angular.module("risevision.widget.common.font-setting", ["ui.tinymce"]) .directive("fontSetting", ["$templateCache", function ($templateCache) { return { restrict: ...

I keep encountering a 404 error page not found whenever I try to use the useRouter function. What could

Once the form is submitted by the user, I want them to be redirected to a thank you page. However, when the backend logic is executed, it redirects me to a 404 page. I have checked the URL path and everything seems to be correct. The structure of my proje ...

Exploring the wonders of Lightbox when dealing with content loaded via AJAX

Using Bootstrap 5 along with the Lightbox library from , I encountered an issue. The Lightbox feature functions properly on regular page loads, but fails to load on content loaded via AJAX. I attempted to resolve this by implementing the following code: ...

"Unlocking the potential of AngularJS: A guide to accessing multiple controllers

I am trying to set a variable in one instance of a controller and read it in another. The variable I need to set is within the object vm (so $scope cannot be used). This is the code for the controller: app.controller("AppController", function(){ var ...

A more efficient method for tallying values within an object (JavaScript, React)

Is there a more efficient way to determine the count of items with a specific key/value pair in a React state? When the list is large, this method may become a bottleneck. To illustrate the question, consider the following simplified example: class App ...

Tips for accessing hidden field values in a second jsp page

I have a webpage called page1.jsp where I am including hidden fields. Here is the code snippet: <form action = "page2.jsp" method = "post" id = "hiddenValuesForm"> <input type = "hidden" name = "userData" value="" id = "useDataID"> <input t ...

Angular 5's external JavaScript library

After thoroughly researching this subject, I find myself still lacking comprehension. An example may be the key to understanding. As a newcomer to Angular, my goal is to create a mathematical application for mobile using Ionic. Despite investing two weeks ...

The `getScript` function in jQuery is not working as expected, even though the path is accurate and the script was successfully downloaded

Recently, I created a website using Mustache.js and incorporated templates loaded via AJAX with jQuery. During the testing phase on my local environment, everything worked perfectly. However, upon uploading the site to my school server, an issue arose whe ...

Uncover and control nested objects in JSON data

Having a dynamic foo object with the possibility of nested parent objects raises the question of how to effectively: 1) Determine the last object that has a parent? 2) Create an array containing all nested parent objects along with the initial obj (foo.o ...

Scroll down only if the user is not actively scrolling

Struggling with JavaScript once again and feeling frustrated! Can someone please assist me? I am currently working on a basic chat application that retrieves content from a text file using an AJAX request. The content refreshes every few seconds, scrollin ...

Tips for resizing user-uploaded images to fit the required dimensions outlined in the design draft using CSS or JavaScript

Hey everyone! I'm facing an issue but my English isn't great. I'll do my best to explain it thoroughly, and if anything is unclear, please feel free to let me know! So here's the problem: today there's a block for users to upload p ...

Deliver data in batches of ten when the endpoint is accessed

I am currently in the process of developing a web application using Next.JS and Node. As part of this project, I have created my own API with Node that is being requested by Next.JS. One particular endpoint within my API sends data to the front end as an ...

The property 'push' cannot be read because it is undefined

$scope.AddTask = function () { $scope.tasks.push([{ "taskName": $scope.taskName, "priority": $scope.selectedP }]); }; $scope.tasks = [{ "taskId": 1, "taskName": "task 1", "priority": 1 }, { "taskId": 2, "taskName ...

Ways to transfer ID to a different HTML page

I have some Javascript code in a file named nextPage.js. When this code is executed by clicking on something, it should transfer the value of category_id to another page called reportlist.html. Can you please provide assistance with this? var base_url = " ...

Is there a way to display errors during jQuery validation?

Is there a specific function that can provide all error messages generated during form validation? I attempted to use the defaultshowerrors() function, but it only shows errors for the current element being validated. Is there a way to retrieve all error ...

Rails application experiencing difficulties in displaying the Raty JS plugin

Encountering issues with the raty js plugin while working on Rails version 5.0. jquery.raty.self-628421be04f36f7a8fa8b9b884c6d7824d6f8bdeba4f172b131f15aa63f713e8.js?body=1:761 Uncaught ReferenceError: jQuery is not defined at jquery.raty.self-628421be ...

Retrieve data by sorting based on the count column in a joined table with Sequelize

I've been struggling to make this work for some time and was hoping for some guidance. OBJECTIVE: I'm attempting to sort the posts by the number of likes they currently have. CURRENT: const posts = await db.post.findAll({ include: [ db.user ...

Angular confirmation, confirmation after tweaking

Currently, I am using angular along with angular-confirm. I have created an interface containing a list of buttons. To view the interface, you can visit this link: Each button in the list has a confirmation prompt attached to it like so: <button ng- ...