Home > Asp.net, Javascript, Jquery > Get Client ID using Jquery

Get Client ID using Jquery


When we are developing client side in asp.net, we need the id of the controls, but the IDs are merged with master page naming containers.

The id will look something like this

ctl00_MainContent_txtboxId
But using Jquery we can get id like this

$("#<%= <strong>txtboxId</strong>.ClientID %>")
$("[id$=_<strong> txtboxId</strong>]").attr("id")

You can also use the function

function GetClientID(id, context) {
var el = $("#" + id, context);
if (el.length < 1)
el = $("[id$=_" + id + "]", context);
return el;
}

Ex:

GetClientID("txtboxId").attr("id");

Or if I want to be specific about the container:
GetClientID("txtboxId",$("#wrapper")).attr("id");

For More Details Visit: http://www.west-wind.com/Weblog/posts/42319.aspx

Advertisements
  1. May 22, 2012 at 2:33 pm

    I’d only recommend using the first one though. An attribute selector like that would have to iterate over every element of the DOM looking for the desired ID. Even so it could give you lots of wrong data, since you can have the same ID in different user controls.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: