Monday, October 17, 2011

Watermark TextBox using JavaScript

<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>


---------------------------
<asp:TextBox ID="TextBox1" runat="server" Text = "Enter your text here"
    ForeColor = "Gray" onblur = "WaterMark(this, event);"
    onfocus = "WaterMark(this, event);">
</asp:TextBox>



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

No comments:

Post a Comment