0

I would like to check if the input has any character in it, then I sould add an active class to it and when it's empty then I should remove the active class. It's working only if the input only has the active class. Could you help me to improve my code to be able to remove ONLY the active class even if it has more?

<div class="input-group">
    <input type="text" id="lastname" onblur="checkInput(this)" />
    <label for="lastname" class="required">Lastname</label>
</div>

<div class="input-group">
    <input type="text" id="invalid" class="invalid" onblur="checkInput(this)" />
    <label for="invalid">Firstname</label>
</div>

<script>
    var checkInput = function(input) {
        if (input.value.length > 0) {
            input.className = 'active';
        } else {
            input.className = '';
        }
    };
</script>
Patrik Alexits
  • 987
  • 9
  • 24

1 Answers1

1

Use onChange event handler instead of onblur Check the link use

input.classList.add('active')
input.classList.remove('active')
shakogele
  • 409
  • 4
  • 14