Identify all inputs that have been dynamically modified using AngularJS

I am currently working on a form that allows users to edit various fields. Some of these fields will automatically update with suggested values until the user makes changes ($dirty).

To ensure users can see which fields have been modified, I would like to visually indicate this change (such as flashing the border of the input).

My challenge is how to apply this visual indicator to all 50 inputs in the form.

For example, I have text inputs:

  • X
  • Y
  • Area
  • Perimeter

Typically, users will input values for X and Y. I have set up a $watch function on these fields that triggers a web service call (/calculate?x=3&y=2) returning {perimeter: 12; area: 6}. These values then autofill in the Area and Perimeter inputs. At this point, I want to add a CSS class to these fields to notify the user that they have been updated automatically.

While I could add the CSS class within the $watch function, I have multiple watchers and wish to avoid complicating the existing form further. Instead, I am considering implementing an "onchange" functionality that can identify inputs changed programmatically and be applied across multiple inputs (either through a directive on the form or individual inputs, or by adding a watcher specifically for the form fields, not just the model).

Answer №1

To enhance the appearance of an input field in HTML when it becomes dirty, you can assign a CSS class to it:

HTML:

<form name="myForm">
    ...
    ...    
    <input name="fieldName" type="text" ng-model="object.name" ng-class="{'dirty-field': myForm.fieldName.$dirty}">
    ...
    ...
</form>

CSS:

.dirty-field{
  border: 1px solid yellow;
}

UPDATE:

If you want to track changes in the input fields using a directive, you can implement this solution:

DIRECTIVE:

.directive('changedField', function(){
  return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, element, attrs, ctrl){

        var originalValue;

        scope.$watch(function(){ 
          return ctrl.$modelValue; 
        }, function(value){
          originalValue = !originalValue ? value : originalValue;
          element.toggleClass('changed-field', originalValue !== value);
        });

      }
  };  
}) 

HTML:

<input changed-field name="fieldName" type="text" ng-model="object.name">

CSS:

.changed-field{
  border: 1px solid yellow;
}

Have fun experimenting with these techniques!

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

Which is more effective: coding with just plain JavaScript and CSS, or utilizing frameworks?

As a student, is it more beneficial to focus on utilizing pure JavaScript & CSS or frameworks? And which approach is best suited for the professional field? ...

Tips on obtaining a dynamically generated document identifier before uploading a file to Firebase storage, and implementing it as a reference for web storage

Before sending a group of writes to Firestore with an automatically generated ID, I have a function that uploads an image file to Firebase storage. The code is set up to wait for the file to be uploaded before getting the download URL and then uploading th ...

The Angular controller that has been injected into the app is not defined

In an effort to enhance my simple Angular app, I decided to modularize it. I separated the controller into its own file within a dedicated directory. By employing dependency injection, I ensured the controller's availability in the app module. Combini ...

Can you please explain the function of this JavaScript code for the Isotope filter?

I am struggling to grasp the functionality of a section of vanilla JS code related to the Isotope filter. You can find the original code here. var buttonGroups = document.querySelectorAll('.button-group'); for (var i = 0; i < buttonGroups.le ...

Altering the playback of a flash object using HTML or JavaScript

Can the playback speed of a flash object be adjusted without recompiling the object, like through HTML attributes or JavaScript? Appreciate any help in advance ...

How can I set a value using document.getElementById(idPopUPImage).innerHTML to create a static popup in Leaflet?

I added a leaflet map to my project which you can find on Codpen In the map, I've included a button key that displays an image in a popup when clicked. However, after closing the popup and reopening it, the image doesn't show unless I click the ...

What is the solution for repairing a slideshow?

I would like to create a carousel of cards in my index.html file. The current code I have displays all five cards stacked vertically with non-functional clickable arrows. Is there a way to show one card at a time and allow users to click through them? < ...

AngularJS's ScrollTo function allows users to scroll to a specific

Trying to implement a quick nav that smoothly scrolls to different sections on the page when a link is clicked. Currently using a guide provided by Treehouse for reference. $("#quickNav a").click(function(){ var quickNavId = $(this).attr("href"); ...

I utilized the ng-option feature to automatically select a specific option in a dropdown menu based on data fetched from a MySQL

Can someone help me with automatically selecting an option in a dropdown list based on data retrieved from a database? This is for editing purposes, so when you are editing, the data will be retrieved automatically for you to edit. Right now, I am able to ...

Automatically trigger a Bootstrap 5.2 modal when the page loads

Currently, I've been utilizing Bootstrap in conjunction with JQuery and have a specific code snippet that displays a Modal when a page is loaded. However, with the latest version 5.2 of Bootstrap, there is a push to move away from using JQuery (which ...

Having trouble executing node commands in the terminal

After launching the terminal on my Mac, I made sure to confirm that Node was installed by running the command: node -v v14.17.5 Next, when attempting to open a file I had created called index.html from Visual Studio Code, I encountered an error message in ...

Guide on verifying the ng-valid status of all inputs within an AngularJS table

I'm working with a table where each row contains an input field. Here's the code snippet: When the save button is clicked, I need to go through each input and determine if it's ng-valid. <div class="line" data-ng-repeat="data in datas"& ...

Creating random numbers in HTML using JavaScript

Currently, I am attempting to develop an HTML random number generator using JavaScript to produce a randomized number and then notify the user of the generated number. However, I consistently encounter an error when obtaining my number, which is different ...

What is your strategy for managing errors when utilizing xui's xhr functionality?

Recently, I've been utilizing the code below to extract data from an external source. <script type="text/javascript"> xui.ready(function() { var url = 'http://www.domain.com/getData.aspx'; x$().xhr(url, ...

Revamping an npm package on GitHub

Currently, I am managing a project that has gained popularity among users and has received contributions from multiple individuals. The next step I want to take is to convert the entire library into TypeScript, but I am unsure of the best approach to ach ...

Pattern for money with two decimal points and a comma as the separator

I have currently implemented regex in the convertNumberToString property, where a comma appears every 3 digits entered. The convertStringToNumber property simply removes the comma to convert it back to a number type. Now, I want to update the regex for t ...

What are the steps to generate interactive hotspots in a 3D setting with threejs?

I'm new to working with three.js and I'm attempting to create a project similar to the example found at . I have everything set up using three.js, but I'm struggling to figure out how to implement a hotspot (like the red dot in the provided ...

Using NodeJS to Send JSON Data via HTTP POST Request

Issue with Sending JSON Data via Http Post in NodeJS const http = require('http'); const fs = require('fs'); var options = { hostname: 'www.postcatcher.in', port: 80, path: '/catchers/5531b7faacde130300002495&apo ...

The use of conditional statements in AngularJS, such as if-else statements

Recently, I've been experimenting with some AngularJS code and encountered a minor challenge that I need assistance with. My goal is to allow users to choose from three different options. Below is the HTML snippet for this: <label class="item item ...

Halt the CSS transition on the preceding element

I tried to pause a CSS transition and came across a question with a solution that seems similar: Is there a way to pause CSS transition mid-way? However, I couldn't make it work for my code. I suspect the issue lies with the before element. What cou ...