Skip to content

Instantly share code, notes, and snippets.

@ofca
Last active December 17, 2015 23:29
Show Gist options
  • Save ofca/5689674 to your computer and use it in GitHub Desktop.
Save ofca/5689674 to your computer and use it in GitHub Desktop.
Nicer version of HTML5 "placeholder" attribute.
$('[data-placeholder]').each(function() {
var $this = $(this),
text = $this.attr('data-placeholder');
if ($this[0].nodeName == 'TEXTAREA') {
$this.text(text);
} else {
$this.val(text);
}
$this
.on('focus', function() {
if (this.nodeName == 'TEXTAREA') {
if ($(this).text() == text) {
this.innerHTML = '';
}
} else {
if (this.value == text) {
this.value = '';
}
}
})
.on('blur', function() {
if (this.nodeName == 'TEXTAREA') {
if ($(this).text() == '') {
this.innerHTML = text;
}
} else {
if (this.value == '') {
this.value = text;
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment