Click here to Skip to main content
13,086,019 members (82,624 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello Friends,
Can anyone tell me, how I can get the value of hidden field using javascript. I'm using master pages.

Here is aspx code
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" CssClass="delbutton" CommandArgument='<%#Eval("noticeid") %>'>Delete
<asp:HiddenField ID="hfID" Value='<%# Eval("noticeid") %>' runat="server" />

Here what I'm trying, I'm binding value to hidden field. Using this value I'm going to delete record using Json.

I tried following JavaScript
var id = document.form1.item("hfID").value;

It gives error saying: document.form1 is undefined
Is this error due to use of master page, in master page the form has id='form1'

Any idea, how to get value??

Thanks in advance
Posted 1-Jul-11 19:27pm
dhage.prashant01 7-Jul-11 7:50am
I made use of Jquery
var id = $('#hfID').val();

But it gives error saying: undefined
Any idea, what is going wrong?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Got solution

check the id of hiddenfield by viewing page source.


This is what actual id I have given to hidden filed:hfID

This is added by

So if you try to use

it gives u error saying: document.getElementById("hfID").value is null

So, final solution is
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

While the answer you've been given is 'correct', a better way is to use the ClientID for your hidden control in code to emit the value, if you find the value by looking at your source, that value can change. Using ClientID will never break.
dhage.prashant01 2-Jul-11 1:59am
how to address it?

it gives me error saying: document.getElementById('hfID').value is null
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Like CG said use the ClientID.
var id = document.getElementById('<%= hfID.ClientID %>').value;

Further Reading
Control.ClientID Property[^]
How-to use ClientIDs in JavaScript without the ugliness[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Not sure my solution at this time is relevant, but just to let you know:).

if you are using ASP.NET v4.0 then it is quite simple. We can retain the id by applying the ClientIdMode = "Static" attribute to the HiddenField control. Of course, this attribute can be applied to any control be it child or parent.Applying to Page or Master directive will affect all the child controls whose ID value remains unchanged.

Then in your javascript,


or if you are using jquery,


Hope this helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100