I am trying to write some watermark text on textbox and textarea. I can successfully add the watermark on the textbox. But on the textarea, it does not work. Anybody has seen this kind of thing before? how to solve this?
<%:Html.TextAreaFor(m => m.InvoiceDetails, new {Value="Invoice detailes",@class = "water", Title = "Invoice detailes" })%>
This is the jquery for watermark. I found this somewhere on the net.:)
<script type="text/javascript">
$(function () {
$(".water").each(function () {
$tb = $(this);
if ($tb.val() != this.title) {
$tb.removeClass("water");
}
});
$(".water").focus(function () {
$tb = $(this);
if ($tb.val() == this.title) {
$tb.val("");
$tb.removeClass("water");
}
});
$(".water").blur(function () {
$tb = $(this);
if ($.trim($tb.val()) == "") {
$tb.val(this.title);
$tb.addClass("water");
}
});
});
The problem is that your textarea doesn’t have a
titleattribute which is what you are trying to use in your javascript. It has aTitleattribute which obviously is not the same. Also there’s noValueattribute defined. The value of a textarea is to be set on by using theInvoiceDetailsproperty on your view model. So start by fixing your markup:Then you may use the code provided in this answer:
And here’s an live demo.