Created
July 30, 2016 21:23
-
-
Save bassplayer7/1e38e8a1116d0d8173c985fbcaaab695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var InputHandler = function InputHandler() { | |
var inputs = document.querySelectorAll('input'), | |
i = 0; | |
for (i; i < inputs.length; i++) { | |
inputs[i].addEventListener('focus', this.handleLabelState); | |
inputs[i].addEventListener('blur', this.handleLabelState); | |
} | |
}; | |
InputHandler.prototype.handleLabelState = function(event) { | |
var input = event.target, | |
label = document.querySelector('label[for="' + input.getAttribute('id') + '"]'); | |
if (!label) { | |
return false; | |
} | |
if (event.type === 'focus' || input.value) { | |
label.classList.add('is-active'); | |
} else { | |
label.classList.remove('is-active'); | |
} | |
}; | |
new InputHandler(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment