Placeholder polyfill

by laustdeleuran

JavaScript

function supportInputPlaceholder() {
  var i = document.createElement('input');
  return 'placeholder' in i;
}
  
// Placeholder support
if (!supportInputPlaceholder()) {
$('input[placeholder], textarea[placeholder]').each(function(){
  var t = $(this);
  var ph = $(this).attr('placeholder');
  var phi = $('<span class="ph"></span>').insertAfter(t);
  t.removeAttr('placeholder');
  phi
    .text(ph)
    .css({
      'position':'absolute',
      'display':'block',
      'top':t.position().top,
      'left':t.position().left
    })
    .addClass('placeholder')
    .hide()
    .click(function(){
      t.focus();
    });
  t.blur(function() {
    if (t[0].value == '') {
      phi.show()
    }
  });
  t.focus(function() {
    phi.hide();
  });
  if (t[0].value == '') {
    phi.show()
  }
});
}