I have a hidden field in my form, I'm setting the hidden field's value on the server and trying to access this value from javascript,
I get the error: Unable to get value of the property 'value': object is null or undefined
If I view the source the hidden field value is set and the ID of the hidden field is the same as the ID I'm calling.
ASPX
var v = document.getElementById('hxValue').value;
<asp:HiddenField ID="hxValue" runat="server"/>
VB
hxValue.Value = "Value1"
I recall doing this before and it should be relatively simple but for some reason, i'm not getting it right.
You can use
innerText
notvalue
to retrieve the value of hxValue.If you were using
jQuery
you could also doTry this
Problem is that Hidden Field is server side control and it's ID that you have given is a server side ID, you will have to get client side ID of that control to refer it in your client side JavaScript or Jquery.
Update
put this script at the end of your page, just before
</body>
something like thisTry
<asp:HiddenField ID="hxValue" runat="server" Value=""/>
Then call it by id and set value
Ok it appears my hidden field's value had not been set before the script had run, therefore receiving a null value. I had assumed that placing breakpoints on the server page load and the script would establish if the control was being set before the script ran, appears not.
Fixed as below:
Thanks for all the assistance.
Your code will work. For simple forms, just add
OR
you need to find the client id using