Skip to content

Instantly share code, notes, and snippets.

@AdamMadrzejewski
Created January 3, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamMadrzejewski/c630fac47367d72dae27 to your computer and use it in GitHub Desktop.
Save AdamMadrzejewski/c630fac47367d72dae27 to your computer and use it in GitHub Desktop.
Sprawdzanie, za pomocą Javascript, czy pole jest wypełnione
(function () {
// pobieramy wszystkie formularze na stronie
var forms = document.querySelectorAll('form');
// pobieramy wszystkie pola formularzy na stronie
var inputs = document.querySelectorAll('.form__content__input');
var isFilledClass = 'is-filled';
// konwertujemy obiekt typu HTML NodeList do obiektu typu tablicowego
forms = Array.prototype.slice.call(forms);
inputs = Array.prototype.slice.call(inputs);
// dla każdego formularza
forms.forEach(function (form) {
// przypisujemy obsługę zdarzenia `change`
form.addEventListener('change', function (event) {
var target = event.target;
// jeśli element na którym miało miejsce zdarzenie `change`
// nie jest polem formularza, to zakończ przetwarzanie
if (!target.classList.contains('form__content__input')) {
return;
}
// jeśli pole nie jest puste
if (target.value !== '') {
// dodaj klasę
target.classList.add(isFilledClass);
} else { // jeśli jest puste
// usuń klasę
target.classList.remove(isFilledClass);
}
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment