Last active
December 23, 2015 03:58
-
-
Save jackbrown/6576689 to your computer and use it in GitHub Desktop.
jQuery input placeholders. A basic way of handling input placeholder text.
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
| <form> | |
| <input type="text" id="name" name="name" data-placeholder="Full name*" value="" /> | |
| <input type="text" id="email" name="email" data-placeholder="Email Address*" value="" /> | |
| <input type="submit" name="submit" value="Submit" /> | |
| </form> |
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
| /** | |
| * Handle the input placeholder text similarly to how HTML5 placeholder attributes work. | |
| */ | |
| $(document).on({ | |
| focus: function(){ | |
| $(this).css('color', 'grey'); | |
| }, | |
| keydown:function(){ | |
| if($(this).val() === $(this).data('placeholder')){ | |
| $(this).val(''); | |
| } | |
| $(this).css('color', '#fff'); | |
| }, | |
| blur: function(){ | |
| if($(this).val() === ""){ | |
| $(this).val($(this).data('placeholder')); | |
| } | |
| } | |
| }, 'input[type=text]'); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added a non-jQuery version https://gist.github.com/jackbrown/d11aba1f6ac8a3e979e3