Angular JS is having some issues with rendering the style correctly

How can I make input fields turn pink before values are entered and green after values are entered? Currently, the styling only applies to the first row. What could be causing this issue?

<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <style>
input.ng-invalid {
    background-color:pink;
}
input.ng-valid {
    background-color:lightgreen;
}
</style>
</head>
<body>

    <div ng-app="myapp" ng-controller="mycont">
    <form name="myform">

        <table>
            <tr>
                <td>Faculty ID</td>
                <td><input type="text" name="facultyid" ng-model="faculty.id" required /><br><span style="color:red" ng-show="myform.facultyid.$touched && myform.facultyid.$invalid">Id is required</span></td>

            </tr>
            <tr>
                <td>Faculty Name:</td>
                <td><input type="text" ng-model="faculty.name" /></td>
            </tr>
            <tr>
                <td>Faculty Salary:</td>
                <td><input type="text" ng-model="faculty.salary" /></td>
            </tr>
            <tr><td><input type="checkbox" ng-model="agree" required>Agree terms and conditions</td></tr>
            <tr>
                <td colspan="2"><button ng-disabled="myform.$invalid" ng-click="addfaculty(faculty)">ADD</button></td>
            </tr>
        </table>
        </form>

Any help on this matter would be greatly appreciated.

Answer №1

You have the option to accomplish this task in the following manner

  angular.module('formExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.submit = function() {
        return false;
      };      
  }]);
input.ng-invalid {
background-color:pink;
}
input.ng-valid {
background-color:lightgreen;
}
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="formExample">
<div ng-controller="ExampleController">
  <form novalidate>
   <table>
            <tr>
                <td>Faculty ID</td>
                <td><input type="text" name="facultyid" ng-model="faculty.id" required /><br><span style="color:red" ng-show="myform.facultyid.$touched && myform.facultyid.$invalid">Id is required</span></td>

            </tr>
            <tr>
                <td>Faculty Name:</td>
                <td><input type="text" ng-model="faculty.name" required /></td>
            </tr>
            <tr>
                <td>Faculty Salary:</td>
                <td><input type="text" ng-model="faculty.salary" required /></td>
            </tr>
            <tr><td><input type="checkbox" ng-model="agree" required>Agree terms and conditions</td></tr>
            <tr>
              <td colspan="2"><button ng-disabled="myform.$invalid" ng-click="addfaculty(faculty)">ADD</button></td>
            </tr>
        </table>
        
  </form>

</div>
  </body>
</html>

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

What is the best way to create a sliding menu that appears from the right side on a mobile device using HTML, CSS, and JQuery

Is there a way to have the menu slide in smoothly from the right side after clicking on the menu bar icon, including sliding the menu bar icon itself? The current code displays the menu directly upon clicking on the menu bar, but I would like it to slide i ...

What steps are involved in creating a mobile web app using Visual Studio 2012, jQuery, and ASP.NET MVC?

As a beginner in asp.net, I am tasked with creating a mobile web app project. Before diving into development, I would like to first focus on designing my app. Can anyone provide guidance on how I can achieve this? Any help is appreciated. ...

Transforming AngularJS $http POST requests into GET requests

I came across a scenario where I needed to update the logo path in my application. So, I defined a route like this: Route::post('/updateLogo', 'CaptivePortalController@updateLogo'); After defining the route, I made a POST request as s ...

Issues with special characters in @font-face rules

For some reason, I'm encountering issues with the CSS3 property @font-face in certain browsers when trying to use my own fonts. Chrome, Safari, and IE10 work fine, but other browsers present several problems: The code I used looks like this: (fonts G ...

"Enhance your client-side PDF capabilities with the pdfMake plugin, offering support for multiple languages

I am in search of a plugin that can convert HTML content into a PDF format and is compatible with javascript/jQuery/AngularJS. It must also provide multilingual support. I have experimented with the following two plugins, but encountered limitations with ...

Exploring the power of $promise in Angular and its interaction with

After delving into promises in Angular and mastering their usage, including deferring, rejecting/resolving, and using .then and .finally, I find myself faced with a challenge that may seem nonsensical at first glance. However, I am determined to figure out ...

Looking for a drag-and-drop solution using Vanilla Javascript, no need for jQuery

Looking for assistance with creating a click-and-drag solution for a background image using Vanilla Javascript. I came across a jQuery solution on Codepen, but I need help translating it into Vanilla Javascript. Draggable.create(".box", { ...

Attempting to achieve a carousel animation using jQuery

Upon loading the page, only one character out of four is displayed. Two arrows are provided - one on the left and one on the right. Clicking on the left arrow causes the current character to fade out and the previous character to fade in. Clicking on the r ...

The Angular Material table does not adapt to different screen sizes

I developed an Angular application using Angular Material that features a table with 22 columns. However, when I expand the browser window, some columns are hidden. Additionally, on mobile browsers, not all columns are displayed. I have attempted the follo ...

Card Alignment with Bootstrap

I need help aligning the last two elements in the card. I want the first one (a button) to be on the left, and another element (a Div with multiple social icon images) on the right side. Can someone provide guidance on how to achieve this layout? <!D ...

"Troubleshooting: Why isn't AngularJS $http.post sending my data

Feeling frustrated with my experience using AngularJS and Laravel 5.2. I'm trying to make a standard $http post request: APIservice.saveArticle = function ( oArticle, callback ) { var message = 'Hello World!'; $http({ metho ...

Center a sans-serif font vertically with exact precision while adjusting the font size

My issue is an expansion of a previous problem I mentioned in another question, which can be found here Vertically align sans-serif font precisely using jquery/css. To summarize: I am aiming to align two divs containing text, with one positioned above the ...

Only the initial external CSS ID is functional, while the second one is inactive

I am facing an issue with my table where I have applied an external CSS that uses alternating shaded rows. My aim is to have specific table cells with different background and font colors - one cell with a green background and red font, and another with a ...

Expand the accordion to reveal all the content

I'm facing an issue with my accordion where a scrollbar appears in every content section. To fix this, I tried setting overflow: hidden in the CSS: .ui-accordion .ui-accordion-content { padding: 1em 2.2em; border-top: 0; overflow: hidd ...

CSS3 :target and display:none inconsistency problem in Internet Explorer and Firefox

Creating a one-page site with navigation tabs that toggle between hidden divs using :target CSS3 selector is my current project. To ensure the first div is visible upon page load, I implemented this line of code: document.location.href = "#div1"; The HTM ...

ng-dialog: the selected value of the input radio button is not defined

While working with ng-dialog, I encountered an issue where the radio button value always ended up being undefined. Below is the dialog template: <script type="text/ng-template" id="flag-reasons.html"> <div style="padding:10px;"> & ...

Enhance Your Navbar in Bootstrap 3 by Creating Vertically Expanding Links Relative to Brand Image Dimensions

To best illustrate my issue, I will begin with a screenshot of the problem and then explain what I aim to achieve. Here is the current state of my navbar: Currently, the text of the links on the right side is positioned at the top. Additionally, this is h ...

What is the best way to begin the main page content below the stationary header in order to prevent any overlap with the hyperlink section?

I have created this HTML page, but I am facing an issue where the hyperlinked sections overlap with the static header when clicked on. I want all linked sections to appear below the header and each section to have different dimensions within the iframe. I ...

Issue in Jhipster v2.20.0: Production mode causing static pictures to fail loading

I successfully launched a JHipster (v2.20.0) application in production mode. Within the AngularJS code, I have included the following line: img.src = assets/.../mypicture.png. When testing locally, the picture loads correctly. However, in production mode ...

Determining the Size and Color of Squares in a Treemap Based on Content with D3.js

I encountered an issue with a treemap visualization. I came across an example that is similar to my situation, which can be viewed here: In the demo, you can see that the size of each square in the treemap is determined by the content size. However, all s ...