gustavopaes
11/24/2015 - 6:22 PM

simple placeholder polyfill

simple placeholder polyfill

;(function() {
  var testInput = document.createElement('input');
  if('placeholder' in testInput || 'placeHolder' in testInput) {
    return false;
  }

  var inputs = document.querySelectorAll('input');
  for(var i = 0, m = inputs.length; i < m; i++) {
    var input = inputs[i];
    var inputPlaceholder = input.getAttribute('placeholder');

    if(input.type == 'text' && inputPlaceholder !== '') {
      input.value = inputPlaceholder;

      input.onclick = function() {
        if(input.value == inputPlaceholder) {
          input.value = '';
        }
      }

      input.onblur = function() {
        if(input.value == '') {
          input.value = inputPlaceholder;
        }
      }
    }
  }
})();