Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access hiddenfield using Jquery

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

like image 262
keyboardP Avatar asked Dec 23 '22 06:12

keyboardP


1 Answers

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.

like image 122
Doug R Avatar answered Jan 02 '23 11:01

Doug R