Click here to Skip to main content
15,667,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I set default text for asp.net textbox during page load and clear them when focused on? I have the Text property set to "0" but it will not clear when focus is set and I would like to keep the default value if there is nothing to input into the textbox.
Posted

1 solution

try this..


HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(function () {

            var txtbox = $('#<%= txtBoxControl.ClientID %>');
            var defaultText = txtbox.val();

            txtbox.focus(function () {
                if (txtbox.val() == defaultText)
                    txtbox.val("");
            });

            txtbox.blur(function () {
                if (txtbox.val().length == 0)
                    txtbox.val(defaultText);
            });

        });

        
        
    </script>
</head>
<body>
    <form id="form1"  runat="server">
    <asp:TextBox ID="txtBoxControl" runat="server" Text="some default value">
    </form>
</body>
</html>
 
Share this answer
 
v3
Comments
Computer Wiz99 6-Jan-14 22:51pm    
Thanks Karthik Bangalore. I will test it.
Karthik_Mahalingam 6-Jan-14 22:52pm    
welcome dude :)
sure you can test.
Add jquery reference to it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900