Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

onclick event help

Back to Forum: Tech Off
  • User profile image
    venkatzeus

    Hi..
    I have a web application, in which i have a textbox for the user to enter the time.

    <asp:TextBox ID="txtEndTime" runat="server" Width="104px" TabIndex="15" MaxLength="8" Text="hh:mm:ss"></asp:TextBox>

    When the user clicks on the textbox, i want to vanish the "hh:mm:ss". How to do it?

  • User profile image
    Maddus Mattus

    Add a javascript that cleares the text of the control on click if it contains "hh:mm:ss". 

  • User profile image
    Ion Todirel

    something like this:

    <script type="text/javascript">   
        function onload()
        {      
            var txtEndTime = document.getElementById('txtEndTime')
            txtEndTime.onclick = function() { txtEndTime.value = '' }
        }
    </script>
    <body onload="onload()">
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="txtEndTime" runat="server" Width="104px" TabIndex="15" MaxLength="8"
                    Text="hh:mm:ss" />
            </div>
        </form>
    </body>

    it's not the best method, but is working

  • User profile image
    stevo_

    Ion Todirel wrote:
    something like this:

    <script type="text/javascript">   
        function onload()
        {      
            var txtEndTime = document.getElementById('txtEndTime')
            txtEndTime.onclick = function() { txtEndTime.value = '' }
        }
    </script>
    <body onload="onload()">
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="txtEndTime" runat="server" Width="104px" TabIndex="15" MaxLength="8"
                    Text="hh:mm:ss" />
            </div>
        </form>
    </body>

    it's not the best method, but is working


    Well, thats probably the worst solution.

    Since you cannot set onclick attribute yourself (as this will map to the server side event). You want to bob in to your code behind file and put (on the page init probably) something like:

    txtEndTime.Attributes.Add("onclick", "if (this.value == 'hh:mm:ss') this.value = '';")

    Edit: you may also want to restore the hh:mm:ss in the event the control loses focus with no value, again similar, something like:

    txtEndTime.Attributes.Add("onblur", "if (this.value == '') this.value = 'hh:mm:ss';")

    Can't be sure if onblur is fully standard across browsers off the top of my head.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.