How to get control server id using jQuery?
eg. I have
<asp:Label ID="label1" runat="server""></asp:Label>
and now I want to get "label1",
var id = ??
If you are using ASP.NET 4.0, you can set the ClientIDMode = "Static" attribute, and your code will look like this:
<asp:Label ID="label1" runat="server" ClientIDMode="Static"></asp:Label>
JS:
var id = 'label1';
var labelID = $('#<%= label1.ClientID %>');
You need to get the client ID.
If you only need the identifier and not the actual value of the control, you don't even need jQuery.
var labelID = '<%= label1.ClientID %>';
var $lblObj = $("label[id$='label1']:first")
jQuery runs on the client side, so it can only access the identifier of the html element, and not the asp control on the server.
Do you use the main page. If so, give the ContentPlaceHolderID along with the ID of the control.
For example:
jQuery("#ContentPlaceHolderID_ControlId").val; jQuery("#body_label1").text;
You can see it in the view.
Labels are displayed as span tags. Therefore, if you want to select all the shortcuts:
$(document).ready(function() { $Labels = $("span"); $Labels.each(function() { alert(this.id); }); });