Form Field Default Values with jQuery

[javascript]
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$(‘.someClass’).each(function() {
var $this = $(this);
var defaultVal = $this.attr(‘title’);

$this.focus(function() {
if ($this.val() === defaultVal) {
$this.val(”);
}
});

$this.blur(function() {
if ($this.val().length === 0) {
$this.val(defaultVal);
}
});
});
});
</script>
<style>
input {
display:block;
margin-bottom:5px;
}
</style>
</head>
<body>
<input class="someClass" type="text" title="Name" value="Name" />
<input class="someClass" type="text" title="Email Address" value="Email Address" />
<input class="someClass" type="text" title="Default Value Here" value="Insert form refill here" />
</body>
</html>
[/javascript]

Leave a Reply