El siguiente snippet nos permite añadir un valor por defecto a un campo de texto y que cuando el usuario se disponga a introducir datos en él, este valor desaparezca. Esta técnica se suele utilizar para dar indicaciones sobre como rellenar el campo dentro del propio campo.
$("#caja").focus(function() {
if( this.value == this.defaultValue ) {
this.value = "";
}
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
}
});
Un ejemplo podría ser:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Menu Salto jQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".caja").focus(function() {
if( this.value == this.defaultValue ) {
this.value = "";
}
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
}
});
});
</script>
<style type="text/css">
.caja{
padding:5px;
font-size:11px;
color:#666;
background:#E8E8E8;
border:1px solid #CCC;
}
</style>
</head>
<body>
<p><label> Ejemplo 1:</label><input name="caja" class="caja" width="60" maxlength="200" value="valor por defecto" /></p>
<p><label> Usuario:</label><input name="caja" class="caja" width="60" maxlength="200" value="Al menos 5 caracteres" /></p>
</body>
</html>




