Reputation: 1402
I have a hidden input field on the page and I wanted to get the ID of it using jQuery.I am using the below statement but it is coming out as null.Can someone tell me what I am doing wrong here.
$(document).ready(function()
{
var hiddenID = $('input[id~="HiddenCounter"]').attr('id');
});
In the source view of the page I can see my element
<input type="hidden" name="longstringgeneratedbyASP.Net_HiddenCounter" id="longstringgeneratedbyASP.Net_HiddenCounter"/>
Upvotes: 0
Views: 2373
Reputation: 626
You can use the .NET-generated ID with the control's ClientID property. Your javascript would look like this:
$(document).ready(function()
{
var hiddenID = '<%=HiddenCounter.ClientID%>';
});
Upvotes: 0
Reputation: 388436
you have a syntax error, there is an extra "
at the beginning of the id
attribute id=""long...
<input type="hidden" name="longstringgeneratedbyASP.Net_HiddenCounter" id="longstringgeneratedbyASP.Net_HiddenCounter"/>
also change the selector to
var hiddenID = $('input:hidden[id$="HiddenCounter"]').attr('id');
Upvotes: 0
Reputation: 12353
Try below code :
var hiddenID = $('input[name$="HiddenCounter"]').attr('id');
Upvotes: 2
Reputation: 11532
you can use :hidden
selector. eg
$(document).ready(function() {
var hiddenID = $('input:hidden').attr('id');
});
your id attribute is null
<input ... id="" longstringgeneratedbyASP.Net_HiddenCounter"/>
Upvotes: 0