JS
$(document).ready(function () {
$(":input[data-watermark]").each(function () {
$(this).val($(this).attr("data-watermark"));
$(this).bind('focus', function () {
if ($(this).val() == $(this).attr("data-watermark")) $(this).val('');
});
$(this).bind('blur', function () {
if ($(this).val() == '') $(this).val($(this).attr("data-watermark"));
$(this).css('color','#a8a8a8');
});
});
});
HTML
<label>Name: </label>
<input class="input" type="text" name="name" maxlength="" data-watermark="My Name" />
CSS
.input{
width:190px;
height:16px;
padding-top:2px;
padding-left:6px;
color:#000;
font-size: 0.688em;
border:#8c9cad 1px solid;
}
What I would like to fix is that whenever the watermarks is in value of the input that the color of the text should be grey (#a8a8a8
). And when the value is something the user writes, then the color should be black.
This is the fiddle: http://jsfiddle.net/qGvAf/
<input type="text" placeholder="This is a watermark" />
is the HTML5 way to do placeholder text. – zzzzBov Sep 15 '11 at 15:45placeholder
polyfill for older browsers, use one of the many already existing ones. – thirtydot Sep 15 '11 at 15:50.data('watermark')
is the prefered way to access.attr("data-watermark")
. – Eric Sep 15 '11 at 16:21