Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve ID of server control using jQuery

How do I get the ID of a server control with jQuery?

E.g. I have

<asp:Label ID="label1" runat="server""></asp:Label>

and now I want to get "label1",

var id = ??
like image 701
AGuyCalledGerald Avatar asked Apr 14 '11 15:04

AGuyCalledGerald


People also ask

How to get id of control in JavaScript?

var rcno = document. getElementById('<%=recno. ClientID %>'); But it return null value..

How to get server side control id in JavaScript?

The Button control has an OnClientClick event which you could use to call some js-code for all buttons. e.g. <asp:Button id="button1" OnClientClick="test(this.id)" ... var fu1 = document.

How to get Client id of ASP.Net control in JavaScript?

Getting Client ID of ASP.Net control using jQuery Once the file is added copy the following code into the JS file. Finally you need to reference the JS file in your project where you need to get the ClientID. return $("[id$=" + asp_net_id + "]"). attr("id");


2 Answers

If you use ASP.NET 4.0 you can set attribute ClientIDMode="Static" and your code will looks following way:

<asp:Label ID="label1" runat="server" ClientIDMode="Static"></asp:Label>

js:

var id = 'label1';
like image 87
pstarkov Avatar answered Oct 20 '22 13:10

pstarkov


var labelID = $('#<%= label1.ClientID %>');

You need to get the client ID.

If you just need the ID, and not the actual value of the control, then you don't even need jQuery.

var labelID  = '<%= label1.ClientID %>';
like image 45
Jack Marchetti Avatar answered Oct 20 '22 15:10

Jack Marchetti