Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display watermark instead of value in textbox in ASP.Net MVC?

In my view I have

<%= Html.TextBoxFor(m => m.Patient.Weight, new { title = "Weight" , style = "width: 3em", @class = "fieldRequired watermark" }) %> pounds</div>

when I run the application I got enter image description here in my text box. When I delete the value from the textbox it will show watermark. So,How do I make like enter image description here ?

like image 228
Lakhae Avatar asked Sep 16 '25 04:09

Lakhae


1 Answers

The term you're looking for is placeholder.

There's an option with HTML 5 to specify a placeholder.

<input name="name" type="text" placeholder="Your name...">
@Html.TextBoxFor(m => m.Name, new { placeholder="Your Name..." })

However, it's not supported on all browsers. So you're off to JavaScript land.

Here's a popular option on NuGet - jQuery.placeholder. It'll read the HTML 5 attributes and handle fall-back for browsers that don't support it.

Just add the script at this in the document onload. And some css classes, check their docs for further info.

$('input, textarea').placeholder();

Edit: placeholder is supported in all major modern browsers

like image 142
Leniency Avatar answered Sep 19 '25 17:09

Leniency



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!