Created
February 10, 2012 19:23
-
-
Save brianmcallister/1791936 to your computer and use it in GitHub Desktop.
HTML5 Placeholder polyfill
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
/*jslint devel: true, browser: true, indent: 2, nomen: true */ | |
/*global jQuery, Modernizr */ | |
// Load a polyfil for placeholder support | |
(function ($, Modernizr) { | |
'use strict'; | |
if (!Modernizr.input.placeholder) { | |
var Actions = { | |
focusInput: function () { | |
var $this = $(this); | |
if ($this.val() === $this.data('placeholder-polyfill')) { | |
$this.val(''); | |
} | |
}, | |
blurInput: function () { | |
var $this = $(this), | |
text = $this.data('placeholder-polyfill'); | |
if ($this.val() === '') { | |
$this.val(text); | |
} | |
} | |
}; | |
$('input[placeholder], textarea[placeholder]').each(function (index, el) { | |
var $el = $(el), | |
text = $el.attr('placeholder'); | |
$el.val(text).data('placeholder-polyfill', text); | |
if ($.browser.msie && parseInt($.browser.version, 10) < 9) { | |
$el.on('focus blur', function (event) { | |
var func = event.type + 'Input'; | |
Actions[func].call(this); | |
}); | |
} else { | |
$(window).on('input[placeholder], textarea[placeholder]', 'focus blur', function (event) { | |
if (event.type === 'focusin') { | |
event.type = 'focus'; | |
} else if (event.type === 'focusout') { | |
event.type = 'blur'; | |
} | |
var func = event.type + 'Input'; | |
Actions[func].call(this); | |
}); | |
} | |
}); | |
} | |
}(jQuery, Modernizr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was using jQuery 1.6 on the project I wrote this for, hence delegate().