Skip to content Skip to sidebar Skip to footer

How To Clear An Label Value In Javascript

I have an label 'test' comimg from .cs [c# code] text='data saved successfully' . but once I click the save button i need to clear its text right now I have

Solution 1:

make a javascript function like:

<Scripttype="text/javascript">functionclearText(cntId) {
  var cnt = document.getElementById(cntId);
  cnt.value ="";
  returnfalse;
}
</script>

then on your submit button attach a client side event

<asp:Buttonid='btnSubmit'Text='Submit'onClientClick='clearText("<%this.lblLable.ClientId%>");'.... />

Solution 2:

On the client-side use a script like this

<scripttype="text/javascript">functionclearLabelValue(){
     var labelObj = document.getElementById("<%= myLabel.ClientID %>");
     labelObj.value = "";
  }
</script><asp:Labelid="myLabel"runat="server"Text="Some text"/><asp:Buttonid="myButton"runat="server"Text="Submit"OnClientClick="clearLabelValue();return false;"/>

Didn't test it in detail, but should work.

It is not really clear what you want to achieve, although I have the feeling there may be a "better" (more standard compliant) way of achieving what you want. Maybe you could describe more clearly what you want, so we may be able to help you.

Solution 3:

In these situations when a particular button has validation attached to it and also we need to fire some javascript what is done is to define a javascript function which is called on click of save button.

What this javascript function does:

This function will take your label and will set its value as blank so that the text is cleared.

Now in order to validate the page which happens internally (in case the javascript function is not written on the save button click) we need to explicitly call what asp.net call for client side validation.

There is a function page_ClientValidate which needs to be called from this javascript function so that validation is still done and we also do some other processing like clearing the label in this case.

Solution 4:

<!--for cleaning to label ; -->

document.getElementById("MyLabel").innerHTML = "";


<!--and label is like;--><asp:LabelID="MyLabel"runat="server" ></asp:Label>

Solution 5:

You can simply achieve this using below script:

<scripttype="text/javascript">functionclearLabelValue(){
      document.getElementById("<%= myLabel.ClientID %>").innerText=""

  }
</script><asp:LabelID="myLabel"runat="server" ></asp:Label><asp:Buttonid="myButton"runat="server"Text="Submit"OnClientClick="clearLabelValue();"/>

Post a Comment for "How To Clear An Label Value In Javascript"