Ulhas Tuscano
Ulhas Tuscano

Reputation: 5620

How to find server control using jquery?

How to find server control using jquery e.g

$(".tab1").find("<%=lblTab1Heading.ClientID%>"); throws exception thrown & not caught

Upvotes: 2

Views: 24995

Answers (4)

Ibad Baig
Ibad Baig

Reputation: 2294

Though I'm too late for the answer but I guess this code will also help viewers finding it difficult to get the server control ID from JQuery

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

and how you should call it

var clientId = GetClientID("serverControlId").attr("id");
var serverControl = document.getElementById(clientId);

Upvotes: 0

BrunoLM
BrunoLM

Reputation: 100322

You missed the # for IDs

              // here
$(".tab1").find("#<%=lblTab1Heading.ClientID%>");

If for some reason it is not working with the template parser, you can use the $= selector, like:

$(".tab1").find("[id$=lblTab1Heading]");

jQuery API

Upvotes: 9

Tim Rogers
Tim Rogers

Reputation: 21713

You need a # sign in your selector. Try

$(".tab1").find("#<%=lblTab1Heading.ClientID%>");

Upvotes: 0

Tom Gullen
Tom Gullen

Reputation: 61737

Try:

var MyControl = $("#<%=lblTab1Heading.ClientID%>");

Because it has an ID, you can simply select it on it's ID which is done with the hash:

$('#ElementID')

Upvotes: 2

Related Questions