ASP.NET - how to get and set value to the hidden field using javascript?

Asked By Eswaran Radhakrishnan on 21-Oct-09 07:51 AM
hi all,
how to set and get the value from hidden field using javascript/

thanks
r.e

get and set value to the hidden field using javascript

DL M replied to Eswaran Radhakrishnan on 21-Oct-09 07:56 AM
set the value from hidden field using javascript

http://www.javascript-coder.com/javascript-form/javascript-set-form-field.htm

get the value from hidden field using javascript

function showPopUp()
{
var hdnValue= document.getElementById('hdnHideUsers').value// Hidden Field vale
var formname="frmGuestUser.aspx?hdnvalue="+hdnValue; //Name of the Page & Query string value
uploadtqpwin=window.open(formname, "GuestUser","top=50px,left=50px,height=500px,width=800px,resizable=no,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
if(uploadtqpwin)
uploadtqpwin.focus();
}

Re

Huggy Bear replied to Eswaran Radhakrishnan on 21-Oct-09 07:56 AM
If I am not wrong, you will still be able to access the value through document.getElementById("<control id>") for a hidden field.

reply

Deepak Sonawane replied to Eswaran Radhakrishnan on 21-Oct-09 08:37 AM
Hi,
You have to write just this code for set and get the value from hidden field using javascript:
Check this code:

 You are going to need to use an ID with your hidden input field. In the example below I added a value to the hidden input field and then called it again and put it into an alert to display it.

<head runat="server">

<title>Untitled Page</title>

 

<script type="text/javascript">


function SetValue()

{

document.getElementById(
'Hidden1').value = "testing !!!!!";
alert(document.getElementById(
'Hidden1').value);

}

 

</script>


</head>

<body onload="SetValue();">

<form id="form1" runat="server">

<div>

<input id="Hidden1" type="hidden" value="" />

</div>

</form>


Here's all that you need.
[)ia6l0 iii replied to Eswaran Radhakrishnan on 21-Oct-09 12:47 PM
To set a value to a hidden field by ID: "hdnSampleFld",
document.getElementById('hdnSampleFld').value = "new value";

To get value from a hidden field by ID: "hdnSampleFld",
var valueOfHiddenField = document.getElementById('hdnSampleFld').value

Note that value starts with lower case "v" in all cases. 

And additionally note that, you would need to use the HiddenField's ClientID when you are using master pages. Like,

To set a value to a hidden field by ID: "hdnSampleFld",
document.getElementById("<%= hdnSampleFld.ClientID%>").value = "new value";

And to get value from a hidden field by ID: "hdnSampleFld",
var valueOfHiddenField = document.getElementById('<%= hdnSampleFld.ClientID%>').value