Ozkan Koylu Ozkan Koylu - 6 months ago 80
Javascript Question

ASP.NET set hiddenfield a value in Javascript

I don't know how to set the value of a hiddenField in Javascript. Can somebody show me how to do this?

Javascript:

document.getElementById('hdntxtbxTaksit').value = "";


HTML:

<asp:HiddenField ID="hdntxtbxTaksit" runat="server" Value="" Visible="false"> </asp:HiddenField>



error : "Unable to get value of the property \'value\': object is null or undefined"

Answer

Try below code with help of ClientID.

  • Asp.net server control id will be vary if you use Master page.

Note: You can avoid ID issue by setting ClientIDMode="static" page property (ASP.Net 4.0 onwards only)

 var myHidden=  document.getElementById('<%= hdntxtbxTaksit.ClientID %>');

 if(myHidden)//checking whether it is found on DOM, but not necessary
{
myHidden.value='mynewvalue';
}