Watermark the textbox using javascript

Posted by Venkat | Labels: ,

Hi , Now we have to discuss about the watermark the textbox using javascript

for that you have to use onblur and onfocus property of your textbox


<script type = "text/javascript">

var defaultText = "Enter your text here";

function WaterMark(txt, evt)

{

if(txt.value.length == 0 && evt.type == "blur")

{

txt.style.color = "gray";

txt.value = defaultText;

}

if(txt.value == defaultText && evt.type == "focus")

{

txt.style.color = "black";

txt.value="";

}

}

</script>


Here is the HTML Code of the Textbox control

<asp:TextBox ID="TextBox1" runat="server" Text = "Enter your text here"

ForeColor = "Gray" onblur = "WaterMark(this, event);"

onfocus = "WaterMark(this, event);">

</asp:TextBox>

Now we have to write the attributes through code on your Page_load Event

TextBox1.Attributes.Add("onblur", "WaterMark(this, event);")

TextBox1.Attributes.Add("onfocus", "WaterMark(this, event);")

0 comments:

Post a Comment

Thanks for the Comments.

PayOffers.in