简体   繁体   中英

Textbox value onclick onblur issue

<asp:TextBox ID="TxtBOX" 
        runat="server"              
        CssClass="text"
        Height="32px" 
        Width="360px"  
        TextMode=MultiLine         
        TextWrapping="Wrap"
        AcceptsReturn="True"
        VerticalScrollBarVisibility="Visible"
        onclick="if(this.value='<%=_data.Obsprevia.ToString()%>'){this.value=''}"             
        onblur="if(this.value=''){this.value='<%=TxtObservaciones.Text.ToString()%>'}">

     </asp:TextBox>

That's my code.

Default TxtBOX.Text comes from code behind (_data_obsprevia). What I want to achieve is, a clean up of that default text once the user clicks on the textbox. And if the txbox lose focus while the user didn't write anything, get back that default text.

I can't achieve that, and if the user DID write something, and lose-gain focus again, the textbox gets empty again.

Thanks.

EDIT

Fixed the == inside the IFs, Now it looks like this

<asp:TextBox ID="TxtBOX" 
        runat="server"              
        CssClass="text"
        Height="32px" 
        Width="360px"  
        TextMode=MultiLine         
        TextWrapping="Wrap"
        AcceptsReturn="True"
        VerticalScrollBarVisibility="Visible"
        onclick="if(this.value=='<%=_data.Obsprevia.ToString()%>'){this.value=''}"             
        onblur="if(this.value==''){this.value='<%=TxtObservaciones.Text.ToString()%>'}">

     </asp:TextBox>   

But when I first click on it, it's not cleaning the deafult data. And if I erase it, and go losing focus it shows <%=TxtObservaciones.Text.ToString()%> Not the value held inside of it.

It should be:

if (this.value == '') {}

As == compares values and = asigns a value.

Try this, a build onto insertusernamehere's suggestion..

onclick="if(this.value=='<%=_data.Obsprevia.ToString()%>'){this.value=''}"             
onblur="if(this.value==''){this.value=document.getElementById('<%= TxtObservaciones.ClientID %>').value}"

This gets what is set in the textbox from javascript, in case it is changed in the browser.

You also might need to break the code out into functions

     function TxtBOX_OnClick() {
         var TxtBOX = document.getElementById("<%= TxtBOX.ClientID %>");
         if (TxtBOX.value == "<%=_data.Obsprevia.ToString()%>") {
             TxtBOX.value = '';
         }
     }
     function TxtBOX_OnBlur() {
         var TxtBOX = document.getElementById("<%= TxtBOX.ClientID %>");
         if (TxtBOX.value == '') {
             TxtBOX.value = document.getElementById("<%= TxtObservaciones.ClientID %>").value;
         }
     }

and

onclick="TxtBOX_OnClick();"             
onblur="TxtBOX_OnBlur();"             

Something like this?

<input type="text" value="Search..." onclick="if(this.value=='Search...'){this.value=''}" onblur="if(this.value==''){this.value='Search...'}">

I'm not familiar with ASP, but hopefully this can help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM