0

On page reloads I have floating labels overlapping the input text if the form has been filled out before. To avoid this problem I want to alter the input text's style if there is data in the form.

Here is my code:

https://codepen.io/holly-williford/pen/pONYYM

document.getElementsByClassName("floating").addEventListener("load", hideLabel);

function hideLabel() {
    if(!$('input').val() ) {
        $('floating').addClass('warning');
    } else {

    }
}


<label class="floating">Test</label>
<input></input> 

warning {
    color: red; 
}
  • 1
    Possible duplicate of [What do querySelectorAll, getElementsByClassName and other getElementsBy\* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-getelementsbyclassname-and-other-getelementsby-method) – Andreas Aug 30 '18 at 17:30
  • I don't understand the problem. – AmmoPT Aug 30 '18 at 17:30
  • 1
    Why are you using `.getElementsByTagName()` and `.addEventListener()` when you have jQuery? – Andreas Aug 30 '18 at 17:30
  • 1
    A ` – Sebastian Simon Aug 30 '18 at 17:34

1 Answers1

1

There are many things from the codepen example that need changes.

  1. HTML

<label class="floating">Test</label>
    <input> <!-- You should have a proper html tag -->
  1. CSS

 .warning { //Notice the '.'
    color: red; 
  }
  1. JS

    window.onload = hideLabel;
    function hideLabel() {
        if(!$('input').val() ) {
              $('.floating').addClass('warning'); // Notice the '.'
        } else {
         
        }
    }

These modifications should help you get started.

But, you have mentioned jQuery and you don't seem to make the most of it.