Skip to content

Instantly share code, notes, and snippets.

@jackbrown
Last active December 23, 2015 03:58
Show Gist options
  • Select an option

  • Save jackbrown/6576689 to your computer and use it in GitHub Desktop.

Select an option

Save jackbrown/6576689 to your computer and use it in GitHub Desktop.
jQuery input placeholders. A basic way of handling input placeholder text.
<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>
/**
* 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]');
@jackbrown

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment