Last active
August 29, 2015 13:56
-
-
Save davidpede/9274029 to your computer and use it in GitHub Desktop.
Clear the content of a text input field when on focus
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
# 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