JavaScript Add class to input parent

I need to add class to input parent if input is not empty or focus because I want to change label position.

<div class="input-field">
    <input type="email" name="email" ng-model="email" required />
    <label for="email">Email</label>
</div>

I can do this:

<div class="input-field" ng-class="{ active: signIn.email.$viewValue.length > 0 || isFocus }">
    <input type="email" name="email" ng-model="email" ng-focus="isFocus=true" ng-blur="isFocus=false" required />
    <label for="email">Email</label>
</div>

But I have many inputs and my code will be very large and ugly.

How can I do it better?

Thank you.

Answer:1

Go with attribute directive where you can have logic which class you need to add to input-filed.

angular.directive('myClass', function() {
  return {
    restrict: 'A',   
    link: function (scope, element,attr) {
      // your logic
    }
  };

or 
$scope.myClass = "";
<div class="input-field" ng-class="myClass" />

angularjs : setting class attribute in a directive template

Answer:2

I am trying to change custom icon of video, when video is toggled(Play/Pause). ngAfterViewInit() { const vdoCont = document.querySelector('.video-player'); const vdo = vdoCont.querySelector('video')...

I am trying to change custom icon of video, when video is toggled(Play/Pause). ngAfterViewInit() { const vdoCont = document.querySelector('.video-player'); const vdo = vdoCont.querySelector('video')...

  1. javascript snake
  2. javascript foreach
  3. javascript map
  4. javascript for loop
  5. javascript array
  6. javascript substring
  7. javascript switch
  8. javascript reduce
  9. javascript date
  10. javascript regex
  11. javascript filter
  12. javascript string contains
  13. javascript function
  14. javascript replace
  15. javascript slice
  16. javascript split
  17. javascript settimeout
  18. javascript download
  19. javascript interview questions
  20. javascript promise

I'm trying to implement an image preview. It works fine until i came to a problem where i need to prevent the input file to not change. JSFIDDLE When trying to change to image its fine. After ...

I'm trying to implement an image preview. It works fine until i came to a problem where i need to prevent the input file to not change. JSFIDDLE When trying to change to image its fine. After ...

  1. jquery preventdefault return false
  2. preventdefault jquery return
  3. jquery preventdefault vs return false
  4. jquery event.preventdefault vs return false
  5. jquery event.preventdefault return false

This is my data with 5 arrays. What I wish to achieve is to combine id and name and the new array should have 5 different playname values. It can be in either an array or new key like playername1. [ ...

This is my data with 5 arrays. What I wish to achieve is to combine id and name and the new array should have 5 different playname values. It can be in either an array or new key like playername1. [ ...

  1. combine array objects javascript
  2. concat array objects javascript
  3. concat array objects
  4. combine array of objects
  5. combine array of objects js
  6. combine two array objects
  7. combine arrays into objects
  8. lodash combine array of objects
  9. combine two array of objects javascript
  10. combine 2 array of objects javascript
  11. es6 combine array of objects
  12. underscore combine array of objects
  13. powershell combine array of objects
  14. php combine array of objects
  15. jq combine array of objects

I have an fuctions in JS like below. manager.addDocument('en', 'bye bye take care', 'greetings.bye'); manager.addDocument('en', 'okay see you later', 'greetings.bye'); manager.addDocument('en', ...

I have an fuctions in JS like below. manager.addDocument('en', 'bye bye take care', 'greetings.bye'); manager.addDocument('en', 'okay see you later', 'greetings.bye'); manager.addDocument('en', ...

  1. make each sentence negative
  2. make each sentence true
  3. make each sentence
  4. make a sentence each with the words you wrote
  5. make each number sentence true
  6. how to make each sentence appear in powerpoint
  7. make a sentence with each other
  8. make a sentence using each
  9. make a sentence with each word
  10. lo contrario make each sentence negative
  11. write numbers to make each sentence true
  12. use parentheses to make each sentence true
  13. make a sentence using word each
  14. make a sentence from each of the following irregular verbs
  15. make a sentence from each of the following irregular verbs arise-arose
  16. make a sentence using each idiom above
  17. make a sentence from each of the following irreguler verbs
  18. make three questions to each sentence