Access hiddenfield using Jquery

keyboardP picture keyboardP · Jan 7, 2010 · Viewed 20.2k times · Source

I have a page that's derived from a master page. On this page, I have a hiddenfield ("hfUser"). How can I access this "hfUser" control and get/set its value using JQuery? I've tried variants of this:

$(document).ready(function() {
    var test = $("#hfUser").val();
    alert(test);

});

but test = undefined. I'm guessing I've got the selector wrong, but I don't know how to get an asp hiddenfield. Any ideas?

Thanks

Answer

Doug R picture Doug R · Jan 7, 2010

If you are using Asp.net controls, the server will mangle the control ids. It adds a bunch of extraneous control tree hierarchy information into the id. You need to reference what that acutal id is that's getting rendered, which is availble with the ClientID property on the control (hfUser.ClientID) or access your control in a different, more roundabout way, like find the controls parent, then search through its children to find your control.

If you don't have to use the asp.net HiddenField control, try just using a regular old html input.