Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
alert(id);


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
Comments
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?

Got solution

check the id of hiddenfield by viewing page source.

document.getElementById("ctl00_cphMain_gvJobs_ctl05_hfID").value;


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

This is added by asp.net:ctl00_cphMain_gvJobs_ctl05_

So if you try to use
document.getElementById("hfID").value;


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

So, final solution is
document.getElementById("ctl00_cphMain_gvJobs_ctl05_hfID").value;
 
Share this answer
 
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.
 
Share this answer
 
Comments
dhage.prashant01 2-Jul-11 1:59am    
how to address it?
document.getElementById('hfID').value;

it gives me error saying: document.getElementById('hfID').value is null
Like CG said use the ClientID.
var id = document.getElementById('<%= hfID.ClientID %>').value;
alert(id);


Further Reading
Control.ClientID Property[^]
How-to use ClientIDs in JavaScript without the ugliness[^]
 
Share this answer
 
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,

JavaScript
document.getElementById("hfID").value;


or if you are using jquery,

JavaScript
$("#hfID").val();


Hope this helps.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900