TextBox Watermark using javascript
Thursday, January 15, 2009
TextBox Watermark using javascript
This is how you can add a watermark to a textbox in asp page using javascript. Suppose we have a textbox like this
Also paste this code on body's onLoad eventThis is how you can add a watermark to a textbox in asp page using javascript. Suppose we have a textbox like this
To add attributes to a textbox paste the following code in the page load event of the page.
<asp:TextBox ID="Text2" runat="server"></asp:TextBox>
Text2.Attributes.Add("onfocus", "chTextin();");
Text2.Attributes.Add("onblur", "chTextout();");
add this functions inside the<body onload="initialize();">
<script>
to </script>
tags.
function chTextin()
{
var txt=document.getElementById('Text2').value;
if(txt=='Enter a Value')
{
document.getElementById('Text2').value='';
document.getElementById('Text2').style.color='';
}
}
function chTextout()
{
var txt=document.getElementById('Text2').value;
if(txt=='')
{
document.getElementById('Text2').value='Enter a Value';
document.getElementById('Text2').style.color='DarkGray';
}
}
function initialize()
{
document.getElementById('Text2').value='Enter a Value';
document.getElementById('Text2').style.color='DarkGray';
}
Labels: ASP.NET, C#, Code, DOTNET, Javascript