Form Field Default Values with jQuery

2014-09-21 • Plugins, Snippets • Views: 1306

Form Field Default Values with jQuery

[php]

<!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>

[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Shares