Created
August 3, 2010 14:26
-
-
Save riddle/506462 to your computer and use it in GitHub Desktop.
html5placeholder.js
This file contains 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
enablePlaceholders: function() { | |
if (!('placeholder' in document.createElement('input'))) { | |
// enable <input placeholder> for browsers not drinking HTML5 kool-aid | |
$('input[placeholder]').each(function() { | |
var $this = $(this); | |
var placeholder = $this.attr('placeholder'); | |
var insert_placeholder = function() { | |
if ($this.val() === '') { | |
$this.val(placeholder); | |
$this.addClass('placeholder'); | |
} | |
}; | |
var remove_placeholder = function() { | |
if ($this.val() === placeholder) { | |
$this.val(''); | |
$this.removeClass('placeholder'); | |
} | |
}; | |
$this.bind('focus', remove_placeholder); | |
$this.bind('blur', insert_placeholder); | |
// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Function/Apply | |
insert_placeholder.apply(this); | |
// prevent submission with default placeholder | |
$this.parents('form').bind('submit', function() { | |
remove_placeholder(); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's mine: http://gist.github.com/506492