user505210
user505210

Reputation: 1402

getting hidden input element ID using jquery

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

Answers (4)

Chuck Spohr
Chuck Spohr

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

Arun P Johny
Arun P Johny

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

Santosh
Santosh

Reputation: 12353

Try below code :

var hiddenID = $('input[name$="HiddenCounter"]').attr('id');

Upvotes: 2

novalagung
novalagung

Reputation: 11532

you can use :hidden selector. eg

$(document).ready(function() {
    var hiddenID = $('input:hidden').attr('id');
});

EDITED

your id attribute is null

<input ... id="" longstringgeneratedbyASP.Net_HiddenCounter"/>

Upvotes: 0

Related Questions