Skip to content

Instantly share code, notes, and snippets.

@davidpede
Last active August 29, 2015 13:56
Show Gist options
  • Save davidpede/9274029 to your computer and use it in GitHub Desktop.
Save davidpede/9274029 to your computer and use it in GitHub Desktop.
Clear the content of a text input field when on focus
# Option One
<script type="text/javascript">
$(document).ready(function() {
$("input:text").each(function () {
var v = this.value; // store default value
$(this).blur(function () {
if (this.value.length == 0) this.value = v; // if input is empty, reset value to default
})
.focus(function () {
this.value = ""; // when input is focused, clear its contents
});
});
});
</script>
# Option Two
<input type="text" onfocus="if(this.value == 'value') { this.value = ''; }" value="value" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment