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


<asp:TextBox ID="Text2" runat="server"></asp:TextBox>

To add attributes to a textbox paste the following code in the page load event of the page.

Text2.Attributes.Add("onfocus", "chTextin();");
Text2.Attributes.Add("onblur", "chTextout();");

Also paste this code on body's onLoad event
<body onload="initialize();">
add this functions inside the <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: , , , ,

 
posted by Abhijeet at 2:45 AM | Permalink | 0 comments